# How to implement a method belonging to a polygon?

Hello, prompt please algorithm of actions when implementing the method of determining points of the polygon the Given number of revolutions. Thank you for your answer.
April 4th 20 at 00:35
April 4th 20 at 00:37
mmm. such a difficult task. a ray from a point, parallel to any axis. count how many times intersects with the sides of the polygon. if the number of intersections is even, the answer is one, if the number is odd the answer is another.

the answer is easily searched by the search engine
Thank you for your answer, but honestly I'm a little confused-that is, on the Internet only a couple of methods found. But I have already painted the algorithm, the teacher showed the pictures here, and I'm here to enter this example can't, and if you read on the Internet Records the number of revolutions does not seem consistent to what I have on the sheet  - wyatt_Mante commented on April 4th 20 at 00:40
@wyatt_Manteon a piece of paper exactly what @Everardo.Hettinger suggested. I see the formula for intersection of ray and segment. Parametrically specified 2 straight, 2 equations for their intersection. 2 unknowns t1 and t2. Restrictions specify the segment and the ray. But there is a ray in General, if we let the horizontal beam, all strongly simplified.

Another of the little tricks necessary to clarify what to do if a ray passes through a polygon vertex: consider the intersection of only the lower ends of the segments. Then this beam can count from 0 to 2 intersections with the polygon and all edge cases are properly measured (that Is, if the beam touches the vertices of the polygon or intersects the boundary at the top). Also, just ignore the horizontal cuts. - coy commented on April 4th 20 at 00:43
@coy, Thank you for your answer, but that threw another solution, is now trying to deal with it, second problem. I do not understand the truth is there on the second sheet stressed the "why" here too do not understand how to get 40 and why -1 and then also 40 but +1  - wyatt_Mante commented on April 4th 20 at 00:46
@wyatt_Mante, On the second page of the first photo and the second photo the same method - the intersection between the horizontal ray from point C segments of the polygon. The same parametric equation with t1 and t2.

The first page in the first photo is not clear that describes. I think there is masturbirovala and calculated the coordinates of the points in the new coordinate system (on the screen? 1280x960 new borders, like the screen resolution). - coy commented on April 4th 20 at 00:49
@coy, no, I'm only the second task is dismantled, and in the second photo is a continuation of the decision that highlighted in red in this photo =) - wyatt_Mante commented on April 4th 20 at 00:52
@wyatt_Mante, did Not read your question, sorry. There, apparently, consider separately the intersection of bottom-up and top-down. Type if the segment crosses the ray from the top down - it +1 turn, if the bottom up - it's -1 turnover. In the end, if polucilosi 0 lies outside. If not zero, then it lies inside. It was not "40" and "CHO" - from the Number of Revolutions.

In principle, this is a very similar solution, but then again, a lot of instances, with the intersection point. Need to watch the neighboring vertices, and where they are from beam, otherwise it is possible to incorrectly calculate the speed (If the beam just passes through the vertex, and 2 adjacent on opposite sides - that is one turn in + or -, If 2 adjacent vertices on one side of the beam, it is 0 rpm). - coy commented on April 4th 20 at 00:55
April 4th 20 at 00:39
This is a standard task of geodesy at the entry point in the polygon is solved by slanting the multiplication of two vectors.
In fact, we move all the segments of a polygon and looks which side of the segment the point lies in. Count the number of all cases when it lies to the right of the line (i.e. intersects when turning), filter out all segments that lie above or below the point. If the number on the right is even or zero, then the point in the polygon is not included, if odd, therefore lies inside the polygon.
Code is solved in five lines.

PS: found my answer two years ago)
Hello) thank you for your answer. Yes, that got rid of one more solution, now trying to deal with it, the second task  - wyatt_Mante commented on April 4th 20 at 00:42
@fiona , That's just the code not working in some cases - if the point lies outside and on the same horizontal, and only the lowest point of the polygon. Ie horizontal beam for the polygon at the vertex. In this case should be 0 or 2 intersections, and your method will count exactly one.

It's because you ignore every segment second end (in the order), and should be ignored only Nizhnee point (or just the top). - coy commented on April 4th 20 at 00:45
@coy, Yes, You are right, the error is. To fix it, short of examples in the Internet. - fiona commented on April 4th 20 at 00:48

Find more questions by tags AlgorithmsGraphics