Is there any way to know if the lines on the canvas (Path) are overlapping?

Viewed 47

I would like to know if there is any logic or function to get a bool when two lines overlap, I tried to do it myself with for cycles of each List<Offset>, however each Offset is not exact, because depending on how you draw it, you put a point and then add the line, something like that is my code to draw :

enter image description here

void _drawFromPointList(Canvas canvas, DrawnShape currentShape) {
    final Path path = Path();
    final pointList = currentShape.pointList;

    /// starts at first point
    path.moveTo(
      pointList[0].dx,
      pointList[0].dy,
    );

    /// goes through every point of the line, (starts at 1)
    for (int f = 1; f < pointList.length; f++) {
      path.lineTo(pointList[f].dx, pointList[f].dy);
    }


    /// draw the line
    canvas.drawPath(path, currentShape.paint);
  }
}
1 Answers

You can use Path.combine() to achieve that.

Should be something along the lines of:

final intersection = Path.combine(PathOperation.intersect, path1, path2);
final pathsAreIntersecting = !intersection.getBounds().isEmpty;
Related