最近项目中用到需要判断线段与圆弧的相交性问题,上网查找了下竟然发现没有相关的文章或代码,苦苦探索了下,想了个可行的方法,项目中使用到直线段由两个点表示,圆弧是由3个点按照逆时针的顺序给出。
基本思路如下:
1. 通过圆弧的3点可以求出圆弧所在圆的圆心与半径,从而确定圆C。
2. 我们可以先判断线段与圆C有没有交点,如果没有说明没有相交,如果有转到3。
3.如果有交点,先分别求出由圆心与圆弧起始点构成的向量A,圆心与圆弧结束点构成的向量B以及每个交点与圆心构成的向量Ci,接着求出AB的倒角 α 以及ACi的倒角 β,如果 β<=α 说明相交。
通过其中两点的中垂线交点求圆心,用圆心和其中的一个点可以求得半径。求圆心以及圆半径示意图:
FVector2D 是UE4中的2D向量类
bool isLineCrossArc(FVector2D LineStart, FVector2D LineStop, FVector2D ArcStart, FVector2D ArcArc, FVector2D ArcStop)
{
//求出圆心以及半径
FVector2D MidPt1, MidPt2;
MidPt1 = (ArcStart + ArcArc) / 2.0;
MidPt2 = (ArcArc + ArcStop) / 2.0;
float K1 = -(ArcArc.X - ArcStart.X) / (ArcArc.Y - ArcStart.Y);
float K2 = -(ArcStop.X - ArcArc.X) / (ArcStop.Y - ArcArc.Y);
FVector2D Center;
Center.X = (MidPt2.Y - MidPt1.Y - K2 * MidPt2.X + K1 * MidPt1.X) / (K1 - K2);
Center.Y = MidPt1.Y + K1 * (MidPt2.Y - MidPt1.Y - K2 * MidPt2.X + K2 * MidPt1.X) / (K1 - K2);
float Radius = FVector2D::Distance(Center, ArcStart);
// 求出线段与圆的交点
float Dis = FVector2D::Distance(LineStart, LineStop);
FVector2D D = (LineStop - LineStart) / Dis;
FVector2D E = Center - LineStart;
float A = E.X * D.X + E.Y * D.Y;
float A2 = A * A;
float E2 = E.X * E.X + E.Y * E.Y;
float R2 = Radius * Radius;
std::vector<FVector2D> ResultPoints;
if ((R2 - E2 + A2) < 0)
{
return false;
}
else
{
float F = FMath::Sqrt(R2 - E2 + A2);
float T = A - F;
if ((T > -0.000001) && (T - Dis) < 0.000001)
{
FVector2D IntersectionP1 = LineStart + T * D;
ResultPoints.push_back(IntersectionP1);
}
T = A + F;
if ((T > -0.000001) && (T - Dis) < 0.000001)
{
FVector2D IntersectionP2 = LineStart + T * D;
ResultPoints.push_back(IntersectionP2);
}
}
ArcStart = (ArcStart - Center).GetSafeNormal();
float AngleStart = FMath::Acos(ArcStart.X);
if (ArcStart.Y < 0.0f)
{
AngleStart *= -1.0f;
}
ArcStop = (ArcStop - Center).GetSafeNormal();
float AngleEnd = FMath::Acos(ArcStop.X);
if (ArcStop.Y < 0.0f)
{
AngleEnd *= -1.0f;
}
float ArcAngle = AngleEnd - AngleStart;
ArcAngle > 0 ? ArcAngle = ArcAngle : ArcAngle = 2 * PI + ArcAngle;
for (int i = 0; i < ResultPoints.size(); ++i)
{
FVector2D IntersectionP(ResultPoints[i].X, ResultPoints[i].Y);
IntersectionP = (IntersectionP - Center).GetSafeNormal();
float Angle = FMath::Acos(IntersectionP.X);
if (IntersectionP.Y < 0.0f)
{
Angle *= -1.0f;
}
Angle = Angle - AngleStart;
Angle > 0 ? Angle = Angle : Angle = 2 * PI + Angle;
if (Angle < ArcAngle)
return true;
}
}
return false;
}
由于项目中涉及到的几何运算比较多,cgal库又比较大,后面项目中使用了Wykobi这个库,因此最后的代码为
bool MathUtil::isLineCrossArc(FVector2D LineStart, FVector2D LineStop, FVector2D ArcStart, FVector2D ArcArc, FVector2D ArcStop)
{
//求圆与线段的交点
wykobi::point2d<float> A = wykobi::make_point<float>(LineStart.X, LineStart.Y);
wykobi::point2d<float> B = wykobi::make_point<float>(LineStop.X, LineStop.Y);
wykobi::segment<float, 2> AB = wykobi::make_segment<float>(A, B);
wykobi::point2d<float> P1 = wykobi::make_point<float>(ArcStart.X, ArcStart.Y);
wykobi::point2d<float> P2 = wykobi::make_point<float>(ArcArc.X, ArcArc.Y);
wykobi::point2d<float> P3 = wykobi::make_point<float>(ArcStop.X, ArcStop.Y);
std::vector<wykobi::point2d<float>> Result;
wykobi::circle<float> Circle = wykobi::make_circle(P1, P2, P3);
FVector2D Center(Circle.x, Circle.y);
wykobi::intersection_point(AB, Circle, std::back_inserter(Result));
ArcStart = (ArcStart - Center).GetSafeNormal();
float AngleStart = FMath::Acos(ArcStart.X);
if (ArcStart.Y < 0.0f)
{
AngleStart *= -1.0f;
}
ArcStop = (ArcStop - Center).GetSafeNormal();
float AngleEnd = FMath::Acos(ArcStop.X);
if (ArcStop.Y < 0.0f)
{
AngleEnd *= -1.0f;
}
float ArcAngle = AngleEnd - AngleStart;
ArcAngle > 0 ? ArcAngle = ArcAngle : ArcAngle = 2 * PI + ArcAngle;
for (int i = 0; i < Result.size(); ++i)
{
FVector2D IntersectionP(Result[i].x, Result[i].y);
IntersectionP = (IntersectionP - Center).GetSafeNormal();
float Angle3 = FMath::Acos(IntersectionP.X);
if (IntersectionP.Y < 0.0f)
{
Angle3 *= -1.0f;
}
Angle3 = Angle3 - AngleStart;
Angle3 > 0 ? Angle3 = Angle3 : Angle3 = 2 * PI + Angle3;
if (Angle3 < ArcAngle)
return true;
}
return false;
}
(73条消息) 判断线段与圆弧的交点_tiantian2226的博客-CSDN博客