intersection static method
Computes an intersection point between two lines using DD arithmetic. If the lines are parallel (either identical or separate) a null value is returned.
@param p1 an endpoint of line segment 1 @param p2 an endpoint of line segment 1 @param q1 an endpoint of line segment 2 @param q2 an endpoint of line segment 2 @return an intersection point if one exists, or null if the lines are parallel
Implementation
static Coordinate? intersection(
Coordinate p1, Coordinate p2, Coordinate q1, Coordinate q2) {
DD px = new DD(p1.y).selfSubtract(p2.y);
DD py = new DD(p2.x).selfSubtract(p1.x);
DD pw = new DD(p1.x)
.selfMultiply(p2.y)
.selfSubtractDD(DD(p2.x).selfMultiply(p1.y));
DD qx = new DD(q1.y).selfSubtract(q2.y);
DD qy = new DD(q2.x).selfSubtract(q1.x);
DD qw = new DD(q1.x)
.selfMultiply(q2.y)
.selfSubtractDD(new DD(q2.x).selfMultiply(q1.y));
DD x = py.multiplyDD(qw).selfSubtractDD(qy.multiplyDD(pw));
DD y = qx.multiplyDD(pw).selfSubtractDD(px.multiplyDD(qw));
DD w = px.multiplyDD(qy).selfSubtractDD(qx.multiplyDD(py));
double xInt = x.selfDivideDD(w).doubleValue();
double yInt = y.selfDivideDD(w).doubleValue();
if ((xInt.isNaN) || (xInt.isInfinite || yInt.isNaN) || (yInt.isInfinite)) {
return null;
}
return new Coordinate(xInt, yInt);
}