Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: how to check whether point was inside or outside of the polygon.

  1. #1
    Newbie Newbie
    Join Date
    Nov 2018
    Posts
    2

    how to check whether point was inside or outside of the polygon.

    Click image for larger version. 

Name:	polygon3.PNG 
Views:	8 
Size:	18.9 KB 
ID:	2877I want to uses the angle to check point inside or outside, if point is inside all the angle will add up to 360, and outside was less than 360.
    Click image for larger version. 

Name:	polygon3.jpg 
Views:	8 
Size:	10.5 KB 
ID:	2878I want to know the formula for 2 point P0P1 turn into vector. so I can using Click image for larger version. 

Name:	angle.png 
Views:	10 
Size:	3.9 KB 
ID:	2879to find angle

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    3,006
    Quote Originally Posted by hellworld View Post
    I want to uses the angle to check point inside or outside
    It's more efficient (and robust) to use plane equations. For each adjacent pair of vertices [x1,y1] and [x2,y2], check the sign of
    (y1-y2)*x + (x2-x1)*y + (x1*y2-x2*y1)
    where [x,y] is the test point.

    If the point is inside the polygon, all of the values will have the same sign (whether it's positive or negative depends upon whether the vertices are in clockwise or anticlockwise order).

    This is likely to be somewhat faster than calculating lengths (each of which requires a square root) and using acos(), as well as being more numerically stable. acos() is ill-conditioned for arguments close to 1. Additionally, if the point lies very close to the edge; rounding errors can result in (ab)/(|a||b|) evaluating to something like -1.00001, which results in acos() returning NaN.

  3. #3
    Intern Contributor
    Join Date
    Sep 2004
    Posts
    95
    Just checking for same signs only works on convex polygons. If the polygon is concave you might need something like 'counting quadrants'. See: https://www.youtube.com/watch?v=f-DIsdvwJ7E

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •