2

I have three consecutive points of polygon, say p1,p2,p3. Now I wanted to know whether the orthogonal between p1 and p3 is inside the polygon or outside the polygon.

I am doing it by taking three vectors v1,v2 and v3. And the point before the point p1 in polygon say p0.
v1 = (p0 - p1)
v2 = (p2 - p1)
v3 = (p3 - p1)

With reference to this question, I am using the method shown in the accepted answer of that question. It is only for counterclockwise. What if my points are clockwise.

I am also knowing my whole polygon is clockwise or counterclockwise. And accordingly I select the vectors v1 and v2. But still I am getting some problem. I am showing one case where I am getting problem.

alt text

This polygon is counterclockwise. and It is starting from the origin of v1 and v2.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Himadri
  • 2,922
  • 5
  • 32
  • 56
  • Your choice of points is very confusing. They aren't in sequential order around the polygon, and the formula for v1 doesn't match v2 and v3. I'm not pointing these out just to complain; it may be where your problem lies. – Marcelo Cantos May 12 '10 at 07:04
  • 1
    What do you mean with 'the orthogonal between p1 and p3'? Do you mean the diagonal of p1 and p3 (as the diagram suggests)? – Michael Ulm May 12 '10 at 07:07
  • I mean line segment between p1 and p3. These are not points of my choice. I got them after some processing. And v1,v2 and v3 are correctly shown. – Himadri May 12 '10 at 07:26
  • @Michael Yes I mean diagonal of p1 and p3 – Himadri May 12 '10 at 07:34

3 Answers3

2

Basically, a diagonal can be fully inside, fully outside, both inside and outside, and possibly overlapping one or more edges in all three cases. This makes it not entirely trivial to determine what you need.

From a mathematical side, there is actually not that much difference between the inside and the outside, except for such small details as the outside having infinite area. (At least for a 2D plane; on a sphere the inside and outside of a plygon are not sharply distinguished.)

You also have a subquestion about the ordering of your polygon edges. The easiest way is to sum all angles between adjacent edges in order. This will add up to N*(pi/2). For CCW polygons, N is positive.

[edit] Once you know the direction, and if you have none of the hard cases listed above, the question is easy. The angle p0-p1-p2 is smaller than the angle p0-p1-p3. Hence, the edge p1-p3 lies at least partially outside the polygon. And if it crosses no other edge, it obviously lies fully outside the polygon.

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • Yes, that is my problem that there is not much difference between inside and outside. I wanted to access inside part but in some cases It gives me wrong result. Please help me. And ordering of edges problem is solved. Thanks. – Himadri May 12 '10 at 08:15
  • ohhhhhhhh but I am having such hard cases shown above. It is a small example. I can get harder cases.. – Himadri May 12 '10 at 09:15
  • To solve the harder cases, determine whether your "diagonal" p1-p3 crosses any edge. Algorithms to detect line segment crossings are pretty trivial to find, e.g. http://local.wasp.uwa.edu.au/~pbourke/geometry/lineline2d/. – MSalters May 12 '10 at 11:08
  • Yes, I had done it but the only problem is with the diagonal completely outside of polygon. – Himadri May 12 '10 at 11:27
2

Since your points are cnosecutive, you can solve this problem by checking the orientation of the triangle p1 p2 p3. If the orientation is the same as the one of the polygon, then the diagonal is in the inside, else on the outside.

To determine the orientation of the triangle, the simplest way is to compute the signed area and check the sign. Compute

p1.x * p2.y + p2.x * p3.y + p3.x * p1.y - p2.x * p1.y - p3.x * p2.y - p1.x * p3.y

If the sign of this value is positive, the orientation is counterclockwise. If the sign is negative, the orientation is clockwise.

To be precise, the above method only gives you information on which side of the polygon the diagonal lies. Obviously, the polygon can still intersect the diagonal at later points.

Michael Ulm
  • 802
  • 1
  • 7
  • 11
  • http://www.cgafaq.info/wiki/Polygon_Area shows some different equation than yours. – Himadri May 12 '10 at 09:08
  • Actually, it is basically the same formula. The one I gave above is the version for three points and ignoring constants (since we only are interested in the sign). – Michael Ulm May 12 '10 at 10:48
  • I didn't use the exact method you show.But I got a hint from this answer. Thanks. – Himadri May 13 '10 at 04:47
0

Angle between any two vectors is

alpha = acos(v1.x * v2.x + v1.y * v2.y)

Since you now can have the angle between

v1 and v3 = alpha1; v1 and v2 = alpha2; 

You can check if alpha2 is inside alpha1:

function normalize(a):
    if a > 2 * pi then a - 2 * pi
    else if a < 2 * pi then a + 2 * pi
    else a

alpha1 = normalize(alpha1)
alpha2 = normalize(alpha2)

if (alpha2 < alpha1) then is_between
else is_not_between

This is not very complete, but you should get the idea.

EDIT: it won't work if the polygon is overlapping as MSalters noted.

TautrimasPajarskas
  • 2,686
  • 1
  • 32
  • 40