Whether a point belongs to a polygon

Whether a point belongs to a polygon

Postby Natter » Mon May 01, 2023 4: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 :(
Natter
 
Posts: 1182
Joined: Mon May 14, 2007 9:49 am

Re: Whether a point belongs to a polygon

Postby Jimmy » Mon May 01, 2023 5:20 pm

hi,

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

if yes try this
Code: Select all  Expand view
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
User avatar
Jimmy
 
Posts: 1699
Joined: Thu Sep 05, 2019 5:32 am
Location: Hamburg, Germany

Re: Whether a point belongs to a polygon

Postby Natter » Mon May 01, 2023 6:27 pm

Jimmy, thank you for your help! I tried it. It looks like everything is correct. :D
Natter
 
Posts: 1182
Joined: Mon May 14, 2007 9:49 am


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 39 guests