contains method

bool contains(
  1. Point point
)

Returns true if the Point is on the line

Example:

LineString([Coordinate(1, 2), Coordinate(3, 4)]).contains(Point(Coordinate(2, 3))); // true
LineString([Coordinate(1, 2), Coordinate(3, 4)]).contains(Point(Coordinate(2, 4))); // false

Implementation

bool contains(Point point) {
  if (slope == null) {
    // If the line is vertical, check if the x-coordinate of the point is equal to the x-coordinate
    return point.coordinate.x == coordinates.first.x &&
        point.coordinate.y >= coordinates.first.y &&
        point.coordinate.y <= coordinates.last.y;
  } else {
    // Calculate the y-intercept of the line
    final double yIntercept =
        coordinates.first.y - (slope! * coordinates.first.x);

    // Check if the point is on the line by plugging the x-coordinate of the point
    // into the equation of the line and checking if the result is equal to the y-coordinate
    // of the point
    final bool isOnLine =
        (slope! * point.coordinate.x + yIntercept) == point.coordinate.y;
    return isOnLine;
  }
}