判断线段与圆弧的交点

最近项目中用到需要判断线段与圆弧的相交性问题,上网查找了下竟然发现没有相关的文章或代码,苦苦探索了下,想了个可行的方法,项目中使用到直线段由两个点表示,圆弧是由3个点按照逆时针的顺序给出。

基本思路如下:

1. 通过圆弧的3点可以求出圆弧所在圆的圆心与半径,从而确定圆C。

2. 我们可以先判断线段与圆C有没有交点,如果没有说明没有相交,如果有转到3。

3.如果有交点,先分别求出由圆心与圆弧起始点构成的向量A,圆心与圆弧结束点构成的向量B以及每个交点与圆心构成的向量Ci,接着求出AB的倒角 α 以及ACi的倒角 β,如果 β<=α 说明相交。

通过其中两点的中垂线交点求圆心,用圆心和其中的一个点可以求得半径。求圆心以及圆半径示意图:

FVector2D 是UE4中的2D向量类

 
  1. bool isLineCrossArc(FVector2D LineStart, FVector2D LineStop, FVector2D ArcStart, FVector2D ArcArc, FVector2D ArcStop)

  2. {

  3. //求出圆心以及半径

  4. FVector2D MidPt1, MidPt2;

  5. MidPt1 = (ArcStart + ArcArc) / 2.0;

  6. MidPt2 = (ArcArc + ArcStop) / 2.0;

  7. float K1 = -(ArcArc.X - ArcStart.X) / (ArcArc.Y - ArcStart.Y);

  8. float K2 = -(ArcStop.X - ArcArc.X) / (ArcStop.Y - ArcArc.Y);

  9. FVector2D Center;

  10. Center.X = (MidPt2.Y - MidPt1.Y - K2 * MidPt2.X + K1 * MidPt1.X) / (K1 - K2);

  11. Center.Y = MidPt1.Y + K1 * (MidPt2.Y - MidPt1.Y - K2 * MidPt2.X + K2 * MidPt1.X) / (K1 - K2);

  12. float Radius = FVector2D::Distance(Center, ArcStart);

  13. // 求出线段与圆的交点

  14. float Dis = FVector2D::Distance(LineStart, LineStop);

  15. FVector2D D = (LineStop - LineStart) / Dis;

  16. FVector2D E = Center - LineStart;

  17. float A = E.X * D.X + E.Y * D.Y;

  18. float A2 = A * A;

  19. float E2 = E.X * E.X + E.Y * E.Y;

  20. float R2 = Radius * Radius;

  21. std::vector<FVector2D> ResultPoints;

  22. if ((R2 - E2 + A2) < 0)

  23. {

  24. return false;

  25. }

  26. else

  27. {

  28. float F = FMath::Sqrt(R2 - E2 + A2);

  29. float T = A - F;

  30. if ((T > -0.000001) && (T - Dis) < 0.000001)

  31. {

  32. FVector2D IntersectionP1 = LineStart + T * D;

  33. ResultPoints.push_back(IntersectionP1);

  34. }

  35. T = A + F;

  36. if ((T > -0.000001) && (T - Dis) < 0.000001)

  37. {

  38. FVector2D IntersectionP2 = LineStart + T * D;

  39. ResultPoints.push_back(IntersectionP2);

  40. }

  41. }

  42. ArcStart = (ArcStart - Center).GetSafeNormal();

  43. float AngleStart = FMath::Acos(ArcStart.X);

  44. if (ArcStart.Y < 0.0f)

  45. {

  46. AngleStart *= -1.0f;

  47. }

  48. ArcStop = (ArcStop - Center).GetSafeNormal();

  49. float AngleEnd = FMath::Acos(ArcStop.X);

  50. if (ArcStop.Y < 0.0f)

  51. {

  52. AngleEnd *= -1.0f;

  53. }

  54. float ArcAngle = AngleEnd - AngleStart;

  55. ArcAngle > 0 ? ArcAngle = ArcAngle : ArcAngle = 2 * PI + ArcAngle;

  56. for (int i = 0; i < ResultPoints.size(); ++i)

  57. {

  58. FVector2D IntersectionP(ResultPoints[i].X, ResultPoints[i].Y);

  59. IntersectionP = (IntersectionP - Center).GetSafeNormal();

  60. float Angle = FMath::Acos(IntersectionP.X);

  61. if (IntersectionP.Y < 0.0f)

  62. {

  63. Angle *= -1.0f;

  64. }

  65. Angle = Angle - AngleStart;

  66. Angle > 0 ? Angle = Angle : Angle = 2 * PI + Angle;

  67. if (Angle < ArcAngle)

  68. return true;

  69. }

  70. }

  71. return false;

  72. }


 

由于项目中涉及到的几何运算比较多,cgal库又比较大,后面项目中使用了Wykobi这个库,因此最后的代码为

 
  1. bool MathUtil::isLineCrossArc(FVector2D LineStart, FVector2D LineStop, FVector2D ArcStart, FVector2D ArcArc, FVector2D ArcStop)

  2. {

  3. //求圆与线段的交点

  4. wykobi::point2d<float> A = wykobi::make_point<float>(LineStart.X, LineStart.Y);

  5. wykobi::point2d<float> B = wykobi::make_point<float>(LineStop.X, LineStop.Y);

  6. wykobi::segment<float, 2> AB = wykobi::make_segment<float>(A, B);

  7. wykobi::point2d<float> P1 = wykobi::make_point<float>(ArcStart.X, ArcStart.Y);

  8. wykobi::point2d<float> P2 = wykobi::make_point<float>(ArcArc.X, ArcArc.Y);

  9. wykobi::point2d<float> P3 = wykobi::make_point<float>(ArcStop.X, ArcStop.Y);

  10. std::vector<wykobi::point2d<float>> Result;

  11. wykobi::circle<float> Circle = wykobi::make_circle(P1, P2, P3);

  12. FVector2D Center(Circle.x, Circle.y);

  13. wykobi::intersection_point(AB, Circle, std::back_inserter(Result));

  14. ArcStart = (ArcStart - Center).GetSafeNormal();

  15. float AngleStart = FMath::Acos(ArcStart.X);

  16. if (ArcStart.Y < 0.0f)

  17. {

  18. AngleStart *= -1.0f;

  19. }

  20. ArcStop = (ArcStop - Center).GetSafeNormal();

  21. float AngleEnd = FMath::Acos(ArcStop.X);

  22. if (ArcStop.Y < 0.0f)

  23. {

  24. AngleEnd *= -1.0f;

  25. }

  26. float ArcAngle = AngleEnd - AngleStart;

  27. ArcAngle > 0 ? ArcAngle = ArcAngle : ArcAngle = 2 * PI + ArcAngle;

  28. for (int i = 0; i < Result.size(); ++i)

  29. {

  30. FVector2D IntersectionP(Result[i].x, Result[i].y);

  31. IntersectionP = (IntersectionP - Center).GetSafeNormal();

  32. float Angle3 = FMath::Acos(IntersectionP.X);

  33. if (IntersectionP.Y < 0.0f)

  34. {

  35. Angle3 *= -1.0f;

  36. }

  37. Angle3 = Angle3 - AngleStart;

  38. Angle3 > 0 ? Angle3 = Angle3 : Angle3 = 2 * PI + Angle3;

  39. if (Angle3 < ArcAngle)

  40. return true;

  41. }

  42. return false;

  43. }

 

(73条消息) 判断线段与圆弧的交点_tiantian2226的博客-CSDN博客

The Computational Geometry Algorithms Library (cgal.org)

Wykobi Computational Geometry Library

 

(36条消息) 判断线段与圆弧的交点_木牛的博客-CSDN博客

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值