Q
Q
Qubert2014-03-22 11:05:29
C++ / C#
Qubert, 2014-03-22 11:05:29

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

4 answer(s)
R
Rsa97, 2014-03-22
@Qubert

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;
}

Control for the coincidence of points (P i = P i+1 ) or (P i = P i+2 ) add yourself.

O
OnYourLips, 2014-03-22
@OnYourLips

Calculate angles by coordinates and use the formula.

T
Teivaz, 2014-03-22
@Teivaz

It will be convex even if one angle is greater than 180 degrees.

G
Grox, 2014-03-22
@Grox

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 question

Ask a Question

731 491 924 answers to any question