rayCastIntersect static method

bool rayCastIntersect(
  1. Point point,
  2. Point vertA,
  3. Point vertB
)

Ray-Casting algorithm implementation Calculate whether a horizontal ray cast eastward from point will intersect with the line between vertA and vertB Refer to https://en.wikipedia.org/wiki/Point_in_polygon for more explanation or the example comment bloc at the end of this file

Implementation

static bool rayCastIntersect(Point point, Point vertA, Point vertB) {
  final double aY = vertA.y;
  final double bY = vertB.y;
  final double aX = vertA.x;
  final double bX = vertB.x;
  final double pY = point.y;
  final double pX = point.x;

  if ((aY > pY && bY > pY) || (aY < pY && bY < pY) || (aX < pX && bX < pX)) {
    // The case where the ray does not possibly pass through the polygon edge,
    // because both points A and B are above/below the line,
    // or both are to the left/west of the starting point
    // (as the line travels eastward into the polygon).
    // Therefore we should not perform the check and simply return false.
    // If we did not have this check we would get false positives.
    return false;
  }

  // y = mx + b : Standard linear equation
  // (y-b)/m = x : Formula to solve for x

  // M is rise over run -> the slope or angle between vertices A and B.
  final double m = (aY - bY) / (aX - bX);
  // B is the Y-intercept of the line between vertices A and B
  final double b = ((aX * -1) * m) + aY;
  // We want to find the X location at which a flat horizontal ray at Y height
  // of pY would intersect with the line between A and B.
  // So we use our rearranged Y = MX+B, but we use pY as our Y value
  final double x = (pY - b) / m;

  // If the value of X
  // (the x point at which the ray intersects the line created by points A and B)
  // is "ahead" of the point's X value, then the ray can be said to intersect with the polygon.
  return x > pX;
}