Determine if a point is within the polygon [1] Ray method thinking

Source: Internet
Author: User
Original source: http://www.html-js.com/article/1517

This article is based on a microblog that I saw a few days ago HTTP://WEIBO.COM/1057676857/ADPFWWFSV

I don't agree with the PO Master's remarks. I have studied chemistry, I have not studied the course of computer science, but intuitively I think this should also be a kind of common problem in computer graphics. And I think this does not need how high-end computer expertise, as long as the middle school mathematics not all back to the teacher, it should be able to give at least one solution. Let's try to solve the problem.

For example, I just scribble a polygon and a dot, and now I'm going to give a general way to tell if the point is inside the polygon (don't let me see with the naked eye ...) )。

The first thought of a solution is to do a ray from this point, calculate it with the polygon boundary of the number of intersections, if the number of intersections is odd, then point inside the polygon, otherwise point in the polygon outside.

The conclusion is simple, but how did it come about? Here is a brief explanation.

First, for any closed curve in the plane, we can intuitively think that the curve divides the plane into two parts, inside and outside, where "inside" is what we call the polygon area.

Based on this understanding, for any straight line in the plane, we can draw the following conclusions: straight through the polygon boundary, there are only two cases: into the polygon or out of the polygon. Without considering non-Euclidean space, it is impossible for a straight line to enter the polygon again from the inside, or to cross the polygon again from the outside, i.e. the case of crossing the boundary twice in a row must be paired. A straight line can extend infinitely, and the area bounded by a closed curve is limited, so the last crossing of a polygon boundary must be a polygon that reaches the outside.

Now back to our original topic. If we make a ray from a given point, we can draw the following two conclusions: If the point is inside the polygon, the first Ray crossing the boundary must be a polygon. If the point is outside the polygon, the first time the ray crosses the boundary it must be into the polygon.

Summing up these conclusions, we can conclude that when a ray crosses a polygon boundary by an even number of times, all the odd (including the last) crosses are pierced, so all the odd times (including the first one) are crossed for piercing, thus inferring the point outside the polygon. When a ray crosses a polygon boundary by an odd number of times, all the odd times (including the first and last) crosses are pierced, thus inferring the point within the polygon.

Here, we have learned the idea of the solution, you can try to write a realization. Some specific problems and boundary conditions in the implementation of the algorithm, the next time to write, the drawing has been painted enough ...

PostScript: Given this solution, I simply searched, originally this algorithm is called the Ray method (ray casting) or the odd-even rules (even odd rule), is an already widely used algorithm. It is also intended to introduce another way to solve this problem through the number of revolution (winding, a concept of topology).

–p.s.–

This series total three: Ray method thought Ray Method realizes the Revolution number method

Finish

This site column is original, reproduced please indicate the source (with the front of the word stew) and the text of the explicit link (http://www.html-js.com/article/1517), the site and the author reserves the right to request to delete the article at any time.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.