intersectsWithSegment method

bool intersectsWithSegment(
  1. Point<num> ip1,
  2. Point<num> ip2
)

Calculate if this stroke intersects with a segment defined by start ip1 and end ip2 point.

Implementation

bool intersectsWithSegment(Point ip1, Point ip2) {
  bool intersectionFound = false;
  Point p1;
  Point p2;

  // Iterate over all lines in the path and stop immediately if
  // an intersection is found.
  if (pointCount > 1) {
    // we have a line
    for (int pointIndex = 0;
        pointIndex < pointCount - 1 && !intersectionFound;
        pointIndex++) {
      if (pointAt(pointIndex).x <= pointAt(pointIndex + 1).x) {
        p1 = pointAt(pointIndex);
        p2 = pointAt(pointIndex + 1);
      } else {
        p2 = pointAt(pointIndex);
        p1 = pointAt(pointIndex + 1);
      }
      intersectionFound = PencilStroke.segmentIntersection(ip1, ip2, p1, p2);
    }
  } else {
    // It's a point. Here wp1 and wp2 are the same. In order to have a
    // line for intersection calculation we create a little virtual
    // cross.
    const epsilon = 2.0;
    p1 = pointAt(0);
    p2 = Point(p1.x + epsilon, p1.y + epsilon);
    final wp3 = Point(p1.x + epsilon, p1.y - epsilon);
    final wp4 = Point(p1.x - epsilon, p1.y + epsilon);
    final wp5 = Point(p1.x - epsilon, p1.y - epsilon);
    intersectionFound = PencilStroke.segmentIntersection(ip1, ip2, p1, p2);
    intersectionFound |= PencilStroke.segmentIntersection(ip1, ip2, p1, wp3);
    intersectionFound |= PencilStroke.segmentIntersection(ip1, ip2, p1, wp4);
    intersectionFound |= PencilStroke.segmentIntersection(ip1, ip2, p1, wp5);
  }

  return intersectionFound;
}