FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Whether a point belongs to a polygon
Posts: 1392
Joined: Mon May 14, 2007 09:49 AM
Whether a point belongs to a polygon
Posted: Mon May 01, 2023 04:39 PM
Hi,

There is a polygon (aPlg:={{X,Y},{X,Y},,,{X,Y}}). How can I determine that a point (aPnt={X,Y}) belongs to this polygon?
I have tried several algorithms, but they all give the wrong result :(
Posts: 1772
Joined: Thu Sep 05, 2019 05:32 AM
Re: Whether a point belongs to a polygon
Posted: Mon May 01, 2023 05:20 PM
hi,

do you want to know if Point X,Y is "in" Polygon :?:

if yes try this
Code (fw): Select all Collapse
FUNCTION IsPointInPolygon(x, y, poly)
   LOCAL isInside := .F.
   LOCAL numVertices := Len(poly)
   
   FOR i:=1 TO numVertices
      LOCAL j := Mod(i, numVertices) + 1
      
      IF ((poly[i][2] > y) <> (poly[j][2] > y)) AND (x < (poly[j][1] - poly[i][1]) * (y - poly[i][2]) / (poly[j][2] - poly[i][2]) + poly[i][1])
         isInside := .NOT. isInside
      ENDIF
   NEXT
   
RETURN isInside
greeting,

Jimmy
Posts: 1392
Joined: Mon May 14, 2007 09:49 AM
Re: Whether a point belongs to a polygon
Posted: Mon May 01, 2023 06:27 PM
Jimmy, thank you for your help! I tried it. It looks like everything is correct. :D

Continue the discussion