B4J Question Check Point in Polygone With some tolerance

tufanv

Expert
Licensed User
Longtime User
Hello,

I am using this code found in this topic post 6 : https://www.b4x.com/android/forum/threads/geo-zone-determination-point-in-polygon.53929/#post-427417

B4X:
Sub CheckPointInPolygone(polx() As Double, poly() As Double, x As Double, y As Double) As Boolean
    Dim x1, y1, x2, y2, D As Double
    Dim i, ni As Int
 
    ni = 0
    x1 = polx(0)
    y1 = poly(0)
    For i = 1 To polx.Length
        If i < polx.Length Then
            x2 = polx(i)
            y2 = poly(i)
        Else
            x2 = polx(0)     ' checks the last line
            y2 = poly(0)
        End If
      
      
        If y >= Min(y1, y2) Then
            If y <= Max(y1, y2) Then
                If x <= Max(x1, x2) Then
                    If (x = x1 And y = y1) Or (x = x1 And x = x2) Then ' checks vertices and vertical lines
                        Return True
                    End If
                    If y1 <> y2 Then
                        D = (y - y1) * (x2 - x1) / (y2 - y1) + x1
                        If x1 = x2 Or x <= D Then
                            ni = ni + 1
                        End If
                    End If
                End If
            End If
        End If
        x1 = x2
        y1 = y2
    Next
 
    If ni Mod 2 = 0 Then
        Return False
    Else
        Return True
    End If
End Sub

I need to add some tolerance for the points near the boundry. Is it possible to add that to this code somehow ?

Thanks
 

klaus

Expert
Licensed User
Longtime User
The routines are general geometry and work with Double numbers.
The units are whatever value you give them, but they must be coherent.
If the coordinates of polx and poly are meters the result is in meters.
If you use miles the result is in miles.
 
Upvote 0
Top