line.dart 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import 'package:fis_measure/interfaces/date_types/point.dart';
  2. import 'package:fis_measure/interfaces/date_types/vector.dart';
  3. class LineUtils {
  4. // 计算两条直线是否有交点
  5. static DPoint? calculateIntersection(
  6. DPoint startA,
  7. DPoint endA,
  8. DPoint startB,
  9. DPoint endB,
  10. ) {
  11. return _getIntersectionPointCloneCSharp(startA, endA, startB, endB);
  12. // final lineA = _IntersectionTempLine.fromPoints(startA, endA);
  13. // final lineB = _IntersectionTempLine.fromPoints(startB, endB);
  14. // if (lineA.isVertical && lineB.isVertical) {
  15. // // 两条直线都是垂直线,没有交点
  16. // return null;
  17. // } else if (lineA.isVertical) {
  18. // // 第一条直线是垂直线,交点的x坐标是第一条直线的x坐标
  19. // return DPoint(
  20. // lineA.intercept, lineB.slope * lineA.intercept + lineB.intercept);
  21. // } else if (lineB.isVertical) {
  22. // // 第二条直线是垂直线,交点的x坐标是第二条直线的x坐标
  23. // return DPoint(
  24. // lineB.intercept, lineA.slope * lineB.intercept + lineA.intercept);
  25. // } else if (lineA.slope == lineB.slope) {
  26. // // 斜率相同,直线平行或重合,没有交点
  27. // return null;
  28. // } else {
  29. // // 斜率不同,计算交点
  30. // double x =
  31. // (lineB.intercept - lineA.intercept) / (lineA.slope - lineB.slope);
  32. // double y = lineA.slope * x + lineA.intercept;
  33. // return DPoint(x, y);
  34. // }
  35. }
  36. static bool isPointOnLine(DPoint p1, DPoint p2, DPoint point) {
  37. // 计算向量v和w
  38. DVector v = p2 - p1;
  39. DVector w = point - p1;
  40. // 计算叉积和点积
  41. double crossProduct = v.x * w.y - v.y * w.x;
  42. double dotProduct = _dotVector(v, w);
  43. // 判断点是否在线上
  44. // 叉积接近0表示点在与线垂直的线上,点积在0到v的模的平方之间表示点在p1和p2之间
  45. return crossProduct.abs() < 1e-10 &&
  46. dotProduct >= 0 &&
  47. dotProduct <= _dotVector(v, v);
  48. }
  49. static double _dotVector(DVector a, DVector b) {
  50. return a.x * b.x + a.y * b.y;
  51. }
  52. static DPoint? _getIntersectionPointCloneCSharp(
  53. DPoint p1, DPoint p2, DPoint p3, DPoint p4) {
  54. double denom =
  55. (p4.x - p3.x) * (p2.y - p1.y) - (p2.x - p1.x) * (p4.y - p3.y);
  56. if (denom == 0) return null;
  57. double t1 =
  58. ((p3.x - p1.x) * (p3.y - p4.y) - (p3.y - p1.y) * (p3.x - p4.x)) / denom;
  59. return DPoint(p1.x + t1 * (p2.x - p1.x), p1.y + t1 * (p2.y - p1.y));
  60. }
  61. }
  62. class _IntersectionTempLine {
  63. final double slope;
  64. final double intercept;
  65. final bool isVertical;
  66. _IntersectionTempLine(this.slope, this.intercept, this.isVertical);
  67. // 根据两个端点创建直线对象
  68. factory _IntersectionTempLine.fromPoints(DPoint p1, DPoint p2) {
  69. if (p1.x == p2.x) {
  70. // 垂直线
  71. return _IntersectionTempLine(double.nan, p1.x, true);
  72. } else {
  73. // 斜率
  74. double m = (p2.y - p1.y) / (p2.x - p1.x);
  75. // y轴截距
  76. double b = p1.y - m * p1.x;
  77. return _IntersectionTempLine(m, b, false);
  78. }
  79. }
  80. }