判断两线段是否相交
判断两线段是否相交并尝试获取交点,共线相交时,如果有重合,则仅判断相交而不计算交点;如果仅有一个端点重合,那么返回该端点作为交点。同时提供参数infinite决定是否为两无限长直线判断相交。为了应对double精度问题,不得不对一些情况做专门判断。
const bool Geo::is_intersected(const Point &point0, const Point &point1, const Point &point2, const Point &point3, Point &output, const bool infinite)
{
if (!infinite)
{
const double left0 = std::min(point0.x, point1.x), left1 = std::min(point2.x, point3.x);
const double right0 = std::max(point0.x, point1.x), right1 = std::max(point2.x, point3.x);
const double top0 = std::max(point0.y, point1.y), top1 = std::max(point2.y, point3.y);
const double bottom0 = std::min(point0.y, point1.y), bottom1 = std::min(point2.y, point3.y);
if (left0 > right1 || right0 < left1 || top0 < bottom1 || bottom0 > top1)
{
return false;
}
}
const double a0 = point1.y - point0.y,
b0 = point0.x - point1.x,
c0 = point1.x * point0.y - point0.x * point1.y;
const double a1 = point3.y - point2.y,
b1 = point2.x - point3.x,
c1 = point3.x * point2.y - point2.x * point3.y;
if (std::abs(a0 * b1 - a1 * b0) < Geo::EPSILON)
{
if (std::abs(a0 * c1 - a1 * c0) > Geo::EPSILON || std::abs(b0 * c1 - b1 * c0) > Geo::EPSILON)
{
return false;
}
if (infinite)
{
return true;
}
else
{
const double a = Geo::distance((point0 + point1) / 2, (point2 + point3) / 2) * 2;
const double b = Geo::distance(point0, point1) + Geo::distance(point2, point3);
if (a < b)
{
return true;
}
else if (a == b)
{
if (Geo::distance(point0, point2) < Geo::EPSILON ||
Geo::distance(point0, point3) < Geo::EPSILON)
{
output = point0;
}
else
{
output = point1;
}
return true;
}
else
{
return false;
}
}
}
output.x = (c1 * b0 - c0 * b1) / (a0 * b1 - a1 * b0), output.y = (c0 * a1 - c1 * a0) / (a0 * b1 - a1 * b0);
if (point0 == point2 || point0 == point3)
{
output = point0;
}
else if (point1 == point2 || point1 == point3)
{
output = point1;
}
if (Geo::is_inside(point0, point2, point3))
{
output = point0;
}
else if (Geo::is_inside(point1, point2, point3))
{
output = point1;
}
else if (Geo::is_inside(point2, point0, point1))
{
output = point2;
}
else if (Geo::is_inside(point3, point0, point1))
{
output = point3;
}
if (point0.x == point1.x)
{
output.x = point0.x;
}
else if (point2.x == point3.x)
{
output.x = point2.x;
}
if (point0.y == point1.y)
{
output.y = point0.y;
}
else if (point2.y == point3.y)
{
output.y = point2.y;
}
if (infinite)
{
return true;
}
else
{
const double left = std::max(std::min(point0.x, point1.x), std::min(point2.x, point3.x));
const double right = std::min(std::max(point0.x, point1.x), std::max(point2.x, point3.x));
const double top = std::min(std::max(point0.y, point1.y), std::max(point2.y, point3.y));
const double bottom = std::max(std::min(point0.y, point1.y), std::min(point2.y, point3.y));
return left - 5e-14 <= output.x && output.x <= right + 5e-14
&& bottom - 5e-14 <= output.y && output.y <= top + 5e-14;
}
}
const bool Geo::is_intersected(const Line &line0, const Line &line1, Point &output, const bool infinite)
{
return Geo::is_intersected(line0.front(), line0.back(), line1.front(), line1.back(), output, infinite);
}
判断两AABB矩形是否相交
该算法通常用于其他几何对象相交的快速判断。提供参数inside决定一个AABB矩形包含在另一个AABB矩形中时是否算相交。
const bool Geo::is_intersected(const AABBRect &rect0, const AABBRect &rect1, const bool inside)
{
if (rect0.empty() || rect1.empty())
{
return false;
}
if (rect0.right() < rect1.left() || rect0.left() > rect1.right() || rect0.bottom() > rect1.top() || rect0.top() < rect1.bottom())
{
return false;
}
if (inside)
{
return true;
}
else
{
return !((rect0.top() < rect1.top() && rect0.right() < rect1.right() && rect0.bottom() > rect1.bottom() && rect0.left() > rect1.left())
|| (rect1.top() < rect0.top() && rect1.right() < rect0.right() && rect1.bottom() > rect0.bottom() && rect1.left() > rect0.left()));
}
}
判断两多段线是否相交
const bool Geo::is_intersected(const Polyline &polyline0, const Polyline &polyline1)
{
if (polyline0.empty() || polyline1.empty() || !Geo::is_intersected(polyline0.bounding_rect(), polyline1.bounding_rect()))
{
return false;
}
Point point;
for (size_t i = 1, count0 = polyline0.size(); i < count0; ++i)
{
for (size_t j = 1, count1 = polyline1.size(); j < count1; ++j)
{
if (Geo::is_intersected(polyline0[i-1], polyline0[i], polyline1[j-1], polyline1[j], point))
{
return true;
}
}
}
return false;
}
判断多段线与多边形是否相交
提供inside参数决定多段线完全在多边形内部时是否算相交。
const bool Geo::is_intersected(const Polyline &polyline, const Polygon &polygon, const bool inside)
{
if (polyline.empty() || polygon.empty() || !Geo::is_intersected(polygon.bounding_rect(), polyline.bounding_rect()))
{
return false;
}
Point point;
for (size_t i = 1, count0 = polyline.size(); i < count0; ++i)
{
for (size_t j = 1, count1 = polygon.size(); j < count1; ++j)
{
if (Geo::is_intersected(polyline[i-1], polyline[i], polygon[j-1], polygon[j], point))
{
return true;
}
else if (inside && Geo::is_inside(polyline[i-1], polygon))
{
return true;
}
}
}
if (inside)
{
return Geo::is_inside(polyline.back(), polygon);
}
else
{
return false;
}
}
判断多段线与圆相交
多段线包含在圆内部也算相交。
const bool Geo::is_intersected(const Polyline &polyline, const Circle &circle)
{
for (size_t i = 0, count = polyline.size(); i < count; ++i)
{
if (Geo::distance(circle.center(), polyline[i - 1], polyline[1]) < circle.radius())
{
return true;
}
}
return false;
}
判断两多边形是否相交
提供inside参数决定一个多边形包含在另一个多边形内部是否算相交。
const bool Geo::is_intersected(const Polygon &polygon0, const Polygon &polygon1, const bool inside)
{
if (polygon0.empty() || polygon1.empty() || !Geo::is_intersected(polygon0.bounding_rect(), polygon1.bounding_rect()))
{
return false;
}
Point point;
for (size_t i = 1, count0 = polygon0.size(); i < count0; ++i)
{
for (size_t j = 1, count1 = polygon1.size(); j < count1; ++j)
{
if (Geo::is_intersected(polygon0[i-1], polygon0[i], polygon1[j-1], polygon1[j], point))
{
return true;
}
}
}
if (inside)
{
for (const Point &point : polygon0)
{
if (Geo::is_inside(point, polygon1, true))
{
return true;
}
}
for (const Point &point : polygon1)
{
if (Geo::is_inside(point, polygon0, true))
{
return true;
}
}
}
return false;
}
判断多边形与圆是否相交
提供inside参数决定多边形包含在圆内部或圆包含在多边形内部是否算相交。
const bool Geo::is_intersected(const Polygon &polygon, const Circle &circle, const bool inside)
{
for (size_t i = 1, count = polygon.size(); i < count; ++i)
{
if (Geo::distance(circle.center(), polygon[i - 1], polygon[1]) < circle.radius())
{
return true;
}
}
if (!inside)
{
return false;
}
if (Geo::is_inside(circle.center(), polygon, true) || std::any_of(polygon.begin(), polygon.end(), [&](const Geo::Point &point) { return Geo::is_inside(point, circle, true); }))
{
return true;
}
return false;
}
判断两圆是否相交
提供inside参数决定一个圆包含在另一个圆中是否算相交。
const bool Geo::is_intersected(const Circle &circle0, const Circle& circle1, const bool inside)
{
if (inside)
{
return Geo::distance(circle0.center(), circle1.center()) <= circle0.radius() + circle1.radius();
}
else
{
const double distance = Geo::distance(circle0.center(), circle1.center());
return distance <= circle0.radius() + circle1.radius() && distance >= std::abs(circle0.radius() - circle1.radius());
}
}
判断AABB矩形是
判断AABB矩形是否与有限长线段相交
线段完全在AABB矩形内也算相交。
const bool Geo::is_intersected(const AABBRect &rect, const Point &point0, const Point &point1)
{
if (is_inside(point0, rect) || is_inside(point1, rect))
{
return true;
}
const double x_max = std::max(point0.x, point1.x);
const double x_min = std::min(point0.x, point1.x);
const double y_max = std::max(point0.y, point1.y);
const double y_min = std::min(point0.y, point1.y);
if (x_max < rect.left() || x_min > rect.right() || y_max < rect.bottom() || y_min > rect.top())
{
return false;
}
else
{
if ((x_min >= rect.left() && x_max <= rect.right()) || (y_min >= rect.bottom() && y_max <= rect.top()))
{
return true;
}
else
{
const double dx = point1.x - point0.x;
const double dy = point1.y - point0.y;
const bool b0 = (rect[0].x - point0.x) * dy >= (rect[0].y - point0.y) * dx;
const bool b1 = (rect[1].x - point0.x) * dy >= (rect[1].y - point0.y) * dx;
const bool b2 = (rect[2].x - point0.x) * dy >= (rect[2].y - point0.y) * dx;
const bool b3 = (rect[3].x - point0.x) * dy >= (rect[3].y - point0.y) * dx;
return !(b0 == b1 && b1 == b2 && b2 == b3);
}
}
}
const bool Geo::is_intersected(const AABBRect &rect, const Line &line)
{
return Geo::is_intersected(rect, line.front(), line.back());
}
判断AABB矩形是否与多段线相交
多段线完全在AABB矩形内也算相交。
const bool Geo::is_intersected(const AABBRect &rect, const Polyline &polyline)
{
if (polyline.empty() || !Geo::is_intersected(rect, polyline.bounding_rect()))
{
return false;
}
for (size_t i = 1, count = polyline.size(); i < count; ++i)
{
if (is_intersected(rect, polyline[i - 1], polyline[i]))
{
return true;
}
}
return false;
}
判断AABB矩形是否与多边形相交
多边形完全在AABB矩形内或AABB矩形完全在多边形内也算相交。
const bool Geo::is_intersected(const AABBRect &rect, const Polygon &polygon)
{
if (polygon.empty() || !Geo::is_intersected(rect, polygon.bounding_rect()))
{
return false;
}
for (size_t i = 1, count = polygon.size(); i < count; ++i)
{
if (is_intersected(rect, polygon[i - 1], polygon[i]))
{
return true;
}
}
for (size_t i = 0; i < 4; ++i)
{
if (is_inside(rect[i], polygon))
{
return true;
}
}
return false;
}
判断AABB矩形是否与圆相交
圆完全在AABB矩形内或AABB矩形完全在圆内也算相交。
const bool Geo::is_intersected(const AABBRect &rect, const Circle &circle)
{
if (circle.empty() || !Geo::is_intersected(rect, circle.bounding_rect()))
{
return false;
}
if (Geo::is_inside(circle.center(), rect, true))
{
return true;
}
for (const Geo::Point &point : rect)
{
if (Geo::is_inside(point, circle, true))
{
return true;
}
}
for (size_t i = 1; i < 5; ++i)
{
if (Geo::distance(circle.center(), rect[i-1], rect[i]) <= circle.radius())
{
return true;
}
}
return false;
}
判断有限长线段是否与三角形相交
线段完全在三角形内不算相交。
const bool Geo::is_intersected(const Point &start, const Point &end, const Triangle &triangle, Point &output0, Point &output1)
{
if (Geo::is_inside(start, end, triangle) || !Geo::is_intersected(triangle.bounding_rect(), start, end))
{
return false;
}
const bool a = Geo::is_intersected(start, end, triangle[0], triangle[1], output0) ||
Geo::is_intersected(start, end, triangle[1], triangle[2], output0) ||
Geo::is_intersected(start, end, triangle[0], triangle[2], output0);
const bool b = Geo::is_intersected(start, end, triangle[0], triangle[2], output1) ||
Geo::is_intersected(start, end, triangle[1], triangle[2], output1) ||
Geo::is_intersected(start, end, triangle[0], triangle[1], output1);
return a || b;
}
const bool Geo::is_intersected(const Line &line, const Triangle &triangle, Point &output0, Point &output1)
{
return Geo::is_intersected(line.front(), line.back(), triangle, output0, output1);
}