diff --git a/poly2d/p2d_attrib.c b/poly2d/p2d_attrib.c index 42ece8c..f4edf5d 100644 --- a/poly2d/p2d_attrib.c +++ b/poly2d/p2d_attrib.c @@ -12,6 +12,7 @@ #include +#include #include #include @@ -27,7 +28,7 @@ static double angle_3(const struct v2d *a, const struct v2d *b, const struct v2d *c) { double ax, ay, bx, by; - double aa, bb; + double cs, aa, bb; double cross, angle; ax = b->x - a->x; @@ -42,8 +43,13 @@ static double angle_3(const struct v2d *a, const struct v2d *b, aa = hypot(ax, ay); bb = hypot(bx, by); - angle = acos((ax * bx + ay * by) / aa / bb) / M_PI * 180.0; - + cs = (ax * bx + ay * by) / aa / bb; + if (cs <= -1) + angle = 180; + else if (cs >= 1) + angle = 0; + else + angle = acos(cs) / M_PI * 180.0; return cross >= 0 ? angle : -angle; }