The polygon is given as a list of Vector2I objects (2 dimensional, integer coordinates). How can i test if a given point is inside? All implementations i found on the web fail for some trivial counter-example. It really seems to be hard to write a correct implementation. The language does not matter as i will port it myself.
相关问题
- How to determine +/- sign when calculating diagona
- Union of many (more than two) polygons without hol
- Is there a way to rotate text around (or inside) a
- Filling rectangle with points pattern
- How to add a “hole” to a Circle Polygon (Google Ma
相关文章
- Algorithm for partially filling a polygonal mesh
- Robust polygon normal calculation
- Algorithm for maximizing coverage of rectangular a
- How to make holes in a Polygon in shapely python,
- SVG circle starting point
- How to calculate end points of perpendicular line
- How to check if a polygon is empty in Shapely?
- Calculate the eccentricity of ellipses
If it is convex, a trivial way to check it is that the point is laying on the same side of all the segments (if traversed in the same order).
You can check that easily with the cross product (as it is proportional to the cosine of the angle formed between the segment and the point, those with positive sign would lay on the right side and those with negative sign on the left side).
Here is the code in Python:
The Ray Casting or Winding methods are the most common for this problem. See the Wikipedia article for details.
Also, Check out this page for a well-documented solution in C.
If the polygon is convex, then in C#, the following implements the "test if always on same side" method, and runs at most at O(n of polygon points):
The pointPolygonTest function in openCV " determines whether the point is inside a contour, outside, or lies on an edge": http://docs.opencv.org/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=pointpolygontest#pointpolygontest
the way i know is something like that.
you pick a point somewhere outside the polygon it may be far away from the geometry. then you draw a line from this point. i mean you create a line equation with these two points.
then for every line in this polygon, you check if they intersect.
them sum of number of intersected lines give you it is inside or not.
if it is odd : inside
if it is even : outside
Or from the man that wrote the book see - geometry page
Specifically this page, he discusses why winding rule is generally better than ray crossing.
edit - Sorry this isn't Jospeh O'Rourke who wrote the excellent book Computational Geometry in C, it's Paul Bourke but still a very very good source of geometry algorithms.