12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- import 'package:fis_measure/interfaces/date_types/point.dart';
- class LineUtils {
- // 计算两条直线是否有交点
- static DPoint? calculateIntersection(
- DPoint startA,
- DPoint endA,
- DPoint startB,
- DPoint endB,
- ) {
- final lineA = _IntersectionTempLine.fromPoints(startA, endA);
- final lineB = _IntersectionTempLine.fromPoints(startB, endB);
- if (lineA.isVertical && lineB.isVertical) {
- // 两条直线都是垂直线,没有交点
- return null;
- } else if (lineA.isVertical) {
- // 第一条直线是垂直线,交点的x坐标是第一条直线的x坐标
- return DPoint(
- lineA.intercept, lineB.slope * lineA.intercept + lineB.intercept);
- } else if (lineB.isVertical) {
- // 第二条直线是垂直线,交点的x坐标是第二条直线的x坐标
- return DPoint(
- lineB.intercept, lineA.slope * lineB.intercept + lineA.intercept);
- } else if (lineA.slope == lineB.slope) {
- // 斜率相同,直线平行或重合,没有交点
- return null;
- } else {
- // 斜率不同,计算交点
- double x =
- (lineB.intercept - lineA.intercept) / (lineA.slope - lineB.slope);
- double y = lineA.slope * x + lineA.intercept;
- return DPoint(x, y);
- }
- }
- }
- class _IntersectionTempLine {
- final double slope;
- final double intercept;
- final bool isVertical;
- _IntersectionTempLine(this.slope, this.intercept, this.isVertical);
- // 根据两个端点创建直线对象
- factory _IntersectionTempLine.fromPoints(DPoint p1, DPoint p2) {
- if (p1.x == p2.x) {
- // 垂直线
- return _IntersectionTempLine(double.nan, p1.x, true);
- } else {
- // 斜率
- double m = (p2.y - p1.y) / (p2.x - p1.x);
- // y轴截距
- double b = p1.y - m * p1.x;
- return _IntersectionTempLine(m, b, false);
- }
- }
- }
|