intersection static method

Coordinate intersection(
  1. Coordinate p1,
  2. Coordinate p2,
  3. Coordinate q1,
  4. Coordinate q2,
)

Computes the (approximate) intersection point between two line segments using homogeneous coordinates.

Note that this algorithm is not numerically stable; i.e. it can produce intersection points which lie outside the envelope of the line segments themselves. In order to increase the precision of the calculation input points should be normalized before passing them to this routine.

@deprecated use {@link Intersection#intersection(Coordinate, Coordinate, Coordinate, Coordinate)}

Implementation

static Coordinate intersection(
    Coordinate p1, Coordinate p2, Coordinate q1, Coordinate q2) {
  // unrolled computation
  double px = p1.y - p2.y;
  double py = p2.x - p1.x;
  double pw = p1.x * p2.y - p2.x * p1.y;

  double qx = q1.y - q2.y;
  double qy = q2.x - q1.x;
  double qw = q1.x * q2.y - q2.x * q1.y;

  double x = py * qw - qy * pw;
  double y = qx * pw - px * qw;
  double w = px * qy - qx * py;

  double xInt = x / w;
  double yInt = y / w;

  if ((xInt.isNaN) || (xInt.isInfinite || yInt.isNaN) || (yInt.isInfinite)) {
    throw new RuntimeException("NotRepresentableException");
  }

  return new Coordinate(xInt, yInt);
}