Check if point is inside a convex polygon - I need an example for a formular

886 Views Asked by At

Can I find out if the location of a point is inside or outside a convex polygon with the below formular?

D = (x2 - x1) * (yp - y1) - (xp - x1) * (y2 - y1)

I assume when result of D > 0, the point location is outside and if D < 0, the point is inside the polygon?

If this is correct, can someone show me how the formular will look with this example of a rectangle?
width 100px
height 40px y any example values x any example values

1

There are 1 best solutions below

0
On BEST ANSWER

D = (x2 - x1) * (yp - y1) - (xp - x1) * (y2 - y1)
This is a formula to know which side of the oriented line (x1,y1),(x2,y2) the point (xp,yp) lies on. Just by looking at the sign of D.

To know if a point (xp,yp) is inside a polygon you must use this formula with all segments of the polygon. If for all of them D has the same sign then the point is inside.
Notice that "the same sign for D" check is due to you run through the segments clockwise or counter-clockwise, so the sign of D changes.

Another method is defining any line passing through (xp,yp), say you use point (xt,yt). Then intersect this line with all segments with this criterium:

  • An intersection out of segment boundaries is considered as not an intersection.
  • If the intersection is not in the direction (xp.yp) to (xt,yt) then is also considered as a not intersection.
  • If the intersection is in a vertex between two segments then count this intersection only once.

Then count how many intersections you found. An even number means that the point is outside the polygon.
This method is valid also for non-convex polygons.