Is the diagonal of the polygon inside or outside?
I have three consecutive polyangular points, say p1, p2, p3. Now I would like to know if the orthogonal between p1 and p3 is inside the polyangular or outside the polyangular.
I do this by taking three vectors v1, v2 and v3. And the point before point p1 in a polyangular, let's say p0.
v1 = (p0 - p1)
v2 = (p2 - p1)
v3 = (p3 - p1)
For this question, I am using the method shown in the accepted answer to this question. This is for counterclockwise only. What if my glasses are clockwise.
I also know that my whole polyangular is clockwise or counterclockwise. And accordingly I choose vectors v1 and v2. But still I am getting some problems. I am showing one case where I have problems.
This polyangular is counterclockwise. and it starts from the origin of v1 and v2.
a source to share
Since your points are sequential, you can solve this problem by checking the orientation of the triangle p1 p2 p3. If the orientation is the same as the polygon, then the diagonal is on the inside and outside.
To determine the orientation of a triangle, the simplest way is to calculate 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 gives you information about which side of the polygon the diagonal lies on. Obviously, the polygon can still intersect the diagonal at later points.
a source to share
In principle, the diagonal could be completely inside, completely outside, both inside and outside, and possibly overlapping one or more edges in all three cases. This makes it not entirely trivial to define what you want.
Mathematically, there really isn't much difference between the inside and the outside, except for such small details as the outside, which has an infinite area. (At least for a two-dimensional plane, on the sphere inside and outside the playgon does not stand out sharply.)
You also have subqueries regarding the ordering of the polygon edges. The easiest way is to sum all the angles between adjacent edges in order. This will add up to N * (pi / 2). For polygons CCW, N is positive.
[edit] Once you know the direction, and if you don't have any of the difficult cases listed above, the question is simple. The angle p0-p1-p2 is less than the angle p0-p1-p3. Therefore, the edge p1-p3 lies at least partially outside the polygon. And if it doesn't cross the other edge, it obviously lies entirely outside the polygon.
a source to share
The angle between any two vectors is
alpha = acos(v1.x * v2.x + v1.y * v2.y)
Since you now have an 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
It's not very complete, but you should get the idea.
EDIT : It won't work if the polygon overlaps as MSalters pointed out.
a source to share
