Answer the question
In order to leave comments, you need to log in
How to find the sum of the angles of an n-polygon?
Hello!
I need to determine if a polygon is convex or not. If the sum of its angles = 180*(n-2), then it is convex, otherwise it is not.
So how can you find the sum of its angles? Thank you.
Answer the question
In order to leave comments, you need to log in
You have chosen the wrong convexity test method. Due to the error in calculating the angles, there are high chances of getting an inaccurate value, and the error during summation will accumulate.
The correct method is to check the signs of vector products of neighboring segments. For a convex polygon, all products will have the same sign, depending on the direction of the traversal.
#define sign(x) ((x) == 0 ? 0 : ((x) > 0 ? 1 : -1))
#define vmul(i,j,k) = ((x[(j)]-x[(i)])*(y[(k)]-y[(j)])-(x[(k)]-x[(j)])*(y[(j)]-y[(i)]))
bool function isConvex(double *x, double *y, int n) {
S = sign(vmul(n-2, n-1, 0));
if ((S1 = vmul(n-1, 0, 1)) != 0 && sign(S1) != S)
return false;
for (int i = 0; i < n-2; i++)
if ((S1 = vmul(i, i+1, i+2)) != 0 && sign(S1) != S )
return false;
return true;
}
If you simply determine whether a polygon is convex or not, then use one of its definitions: a
polygon without self-intersections such that each interior angle of which is not more than 180 °;
Answer: Check all angles, if each angle <= 180°, then it is a convex polygon.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question