|
|
@@ -37,18 +37,18 @@ namespace math {
|
|
|
colinear = 0, clockwise = 1, anticlockwise = 2
|
|
|
};
|
|
|
|
|
|
- orientation orient(dim2::point const & p, dim2::point const & q,
|
|
|
- dim2::point const & r) {
|
|
|
- float val = (q[1] - p[1]) * (r[0] - q[0]) - (q[0] - p[0]) * (r[1] - q[1]);
|
|
|
+ orientation orient(dim2::line const & ln, dim2::point const & pt) {
|
|
|
+ auto val = (ln.second[1] - ln.first[1]) * (pt[0] - ln.second[0]) -
|
|
|
+ (ln.second[0] - ln.first[0]) * (pt[1] - ln.second[1]);
|
|
|
|
|
|
if (val == 0) return colinear;
|
|
|
return (val > 0) ? clockwise : anticlockwise;
|
|
|
}
|
|
|
|
|
|
bool contains(dim2::line const & ln, dim2::point const & pt) {
|
|
|
- auto xs = std::minmax(ln.first[0], ln.second[0], lessinf());
|
|
|
- auto ys = std::minmax(ln.first[1], ln.second[1], lessinf());
|
|
|
- return orient(ln.first, ln.second, pt) == colinear &&
|
|
|
+ auto xs = std::minmax(ln.first[0], ln.second[0]);
|
|
|
+ auto ys = std::minmax(ln.first[1], ln.second[1]);
|
|
|
+ return orient(ln, pt) == colinear &&
|
|
|
between(pt[0], xs.first, xs.second) &&
|
|
|
between(pt[1], ys.first, ys.second);
|
|
|
}
|
|
|
@@ -68,7 +68,7 @@ namespace math {
|
|
|
int hits = 0;
|
|
|
for (auto l : segments) {
|
|
|
if (!intersects(l, ray_x(pt, l))) continue;
|
|
|
- if (orient(l.first, l.second, pt) == colinear) return contains(l, pt);
|
|
|
+ if (orient(l, pt) == colinear) return contains(l, pt);
|
|
|
++hits;
|
|
|
}
|
|
|
return (hits & 1) == 1;
|
|
|
@@ -81,10 +81,10 @@ namespace math {
|
|
|
bool intersects(dim2::line const & lhs, dim2::line const & rhs) {
|
|
|
// https://www.geeksforgeeks.org/check-if-two-given-line-segments-intersect/
|
|
|
// Find the four orientations needed for general and special cases
|
|
|
- orientation o1 = orient(lhs.first, lhs.second, rhs.first);
|
|
|
- orientation o2 = orient(lhs.first, lhs.second, rhs.second);
|
|
|
- orientation o3 = orient(rhs.first, rhs.second, lhs.first);
|
|
|
- orientation o4 = orient(rhs.first, rhs.second, lhs.second);
|
|
|
+ orientation o1 = orient(lhs, rhs.first);
|
|
|
+ orientation o2 = orient(lhs, rhs.second);
|
|
|
+ orientation o3 = orient(rhs, lhs.first);
|
|
|
+ orientation o4 = orient(rhs, lhs.second);
|
|
|
|
|
|
// General Case: Lines cross through each other
|
|
|
if (o1 != o2 && o3 != o4)
|