isPointOnLineSegment method

bool isPointOnLineSegment(
  1. Offset p,
  2. Offset a,
  3. Offset b
)

Implementation

bool isPointOnLineSegment(Offset p, Offset a, Offset b) {
  // Calculate the cross product

  if (a.dx == b.dx && a.dy == b.dy) {
    return false;
  }
  double crossProduct =
      (p.dy - a.dy) * (b.dx - a.dx) - (p.dx - a.dx) * (b.dy - a.dy);

  // Check if the point is collinear with the line segment
  if (crossProduct.abs() > 1e-10) {
    return false;
  }

  // Check if the point is within the bounds of the line segment
  double dotProduct =
      (p.dx - a.dx) * (b.dx - a.dx) + (p.dy - a.dy) * (b.dy - a.dy);
  if (dotProduct < 0) {
    return false;
  }

  double squaredLengthBA =
      (b.dx - a.dx) * (b.dx - a.dx) + (b.dy - a.dy) * (b.dy - a.dy);
  if (dotProduct > squaredLengthBA) {
    return false;
  }

  return true;
}