contains method

bool contains(
  1. Point point
)

Returns whether or not the LinearRing contains the Point. Uses the Ray Casting algorithm.

Example:

LinearRing ring = LinearRing([
  Coordinate(0, 0),
  Coordinate(0, 1),
  Coordinate(1, 1),
  Coordinate(1, 0),
  Coordinate(0, 0),
]);
print(ring.contains(Point(Coordinate(0.5, 0.5)))); // true
print(ring.contains(Point(Coordinate(0.5, 1.5)))); // false

Implementation

bool contains(Point point) {
  Coordinate testCoord = coordinates.last;
  bool c = false;
  for (Coordinate coord in coordinates) {
    // for each vertex in the polygon,
    if (point.lat == coord.latitude && point.lng == coord.longitude) {
      // the point is a vertex, so return true
      return true;
    } else if (coord.latitude > point.lat != testCoord.latitude > point.lat) {
      // if the point is to the right of the current polygon edge
      double slope = (point.lng - coord.longitude) *
              (testCoord.latitude - coord.latitude) -
          (testCoord.longitude - coord.longitude) *
              (point.lat - coord.latitude);
      if (slope == 0) {
        // if the point is exactly on the current polygon edge
        return true;
      } else if ((slope < 0) != testCoord.latitude < coord.latitude) {
        // if the point is to the right of the current polygon edge
        c = !c;
      }
    }
    testCoord = coord;
  }
  return c;
}