How can I find the number of internal angles of a polygon, bigger than 180º, having only the vertices of the polygon?
For each vertex I want always the internal angle, not the external.
Thanks from Brazil.
How can I find the number of internal angles of a polygon, bigger than 180º, having only the vertices of the polygon?
For each vertex I want always the internal angle, not the external.
Thanks from Brazil.
I'm assuming this is an irregular polygon, since it would be really difficult for a regular polygon to have an internal angle greater than 180 degrees.
For each vertex, you would also need to know the two neighboring vertices. You can then turn this into a trigonometry problem, where you find the angle from the main vertex to, say, the left vertex, and add it to the angle from the main vertex to the right vertex.
For example,
tan(angle_to_left) = (v.y-left.x)/(v.y-left.y) tan(angle_to_right) = (v.y-right.x)/(v.y-right.y)
Then add the angles together.
Finally, for all angles that are greater than 180, increment a counter. After going through all of the vertices, your counter will tell you how many internal angles are greater than 180.
The problem with tangent is when x==0. If you only know the vertices of the polygon, you don't know enough unless it's a triangle since they could have any sort of connectivity.
Assuming you know the connectivity, you will then need to calculate the winding order (i.e., in what direction do the points go around the polygon?). With the winding order, you can then take the cross product of every point with its neighboring points and take the inverse sine of magnitude of that to get the angle.
You can determine the angle of two vectors simply by taking the scalar product (dot product). A useful property is that if the vectors are orthogonal, their scalar product is zero; if their angle is obtuse, the product is negative, otherwise positive. So, the steps to take are:
(x y)
to (-y x)
)(x1 * x2) + (y1 * y2)
)edit: You can find whether the vertices are ordered counter-clockwise or clockwise by using the following formula to calculate the polygon's area:
1 n-1 A = --- SUM( x(i)*y(i+1) - x(i+1)*y(i) ) 2 i=0
where n
is the number of vertices. x(n)
and y(n)
are the same as x(0)
and y(0)
(to close the polygon).
If it is positive, then the vertices are ordered counter-clockwise, otherwise clockwise.
edit: When you simplify the steps of rotation and scalar product, you arrive at the formula for the two-dimensional cross product, x1*y2 - x2*y1
. This simplifies the first steps above:
((x1 * y2) - (x2 * y1))
Sorry for the convoluted first approach.