11#include " triangle.h"
22
3+ #include < compare>
4+
35#include " line.h"
46
57namespace Geom
@@ -14,20 +16,27 @@ double Triangle::area() const
1416
1517double Triangle::distance (const Point &point) const
1618{
17- const auto A = points[2 ] - points[1 ];
18- const auto B = points[0 ] - points[1 ];
19- const auto C = points[2 ] - points[0 ];
20- const auto dA = Geom::Line{points[1 ], points[2 ]}.distance (point);
21- const auto dB = Geom::Line{points[0 ], points[1 ]}.distance (point);
22- const auto dC = Geom::Line{points[2 ], points[0 ]}.distance (point);
23- if (const auto double_area = fabs (A ^ B);
24- !Math::Floating::is_zero (double_area)
25- && Math::AddTolerance (
26- A.abs () * dA + B.abs () * dB + C.abs () * dC)
27- == double_area) {
19+ auto rot_dir = [](const Point &a, const Point &b, const Point &c)
20+ {
21+ return std::weak_order (0.0 ,
22+ (b.x - a.x ) * (c.y - a.y ) - (b.y - a.y ) * (c.x - a.x ));
23+ };
24+
25+ using std::is_gteq;
26+ using std::is_lteq;
27+ using std::is_neq;
28+ if (auto r1 = rot_dir (points[0 ], points[1 ], point),
29+ r2 = rot_dir (points[1 ], points[2 ], point),
30+ r3 = rot_dir (points[2 ], points[0 ], point);
31+ (is_neq (r1) || is_neq (r2) || is_neq (r3))
32+ && ((is_lteq (r1) && is_lteq (r2) && is_lteq (r3))
33+ || (is_gteq (r1) && is_gteq (r2) && is_gteq (r3))))
2834 return 0.0 ;
29- }
30- return std::min ({dA, dB, dC}, Math::Floating::less);
35+
36+ return std::min ({Line{points[0 ], points[1 ]}.distance (point),
37+ Line{points[1 ], points[2 ]}.distance (point),
38+ Line{points[2 ], points[0 ]}.distance (point)},
39+ Math::Floating::less);
3140}
3241
3342}
0 commit comments