intersection method

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

This method computes the actual value of the intersection point. To obtain the maximum precision from the intersection calculation, the coordinates are normalized by subtracting the minimum ordinate values (in absolute value). This has the effect of removing common significant digits from the calculation to maintain more bits of precision.

Implementation

Coordinate intersection(
    Coordinate p1, Coordinate p2, Coordinate q1, Coordinate q2) {
  Coordinate intPt = intersectionSafe(p1, p2, q1, q2);

  /*
  // TESTING ONLY
  Coordinate intPtDD = CGAlgorithmsDD.intersection(p1, p2, q1, q2);
  double dist = intPt.distance(intPtDD);
  System.out.println(intPt + " - " + intPtDD + " dist = " + dist);
  //intPt = intPtDD;
  */

  /**
   * Due to rounding it can happen that the computed intersection is
   * outside the envelopes of the input segments.  Clearly this
   * is inconsistent.
   * This code checks this condition and forces a more reasonable answer
   *
   * MD - May 4 2005 - This is still a problem.  Here is a failure case:
   *
   * LINESTRING (2089426.5233462777 1180182.3877339689, 2085646.6891757075 1195618.7333999649)
   * LINESTRING (1889281.8148903656 1997547.0560044837, 2259977.3672235999 483675.17050843034)
   * int point = (2097408.2633752143,1144595.8008114607)
   *
   * MD - Dec 14 2006 - This does not seem to be a failure case any longer
   */
  if (!isInSegmentEnvelopes(intPt)) {
//      System.out.println("Intersection outside segment envelopes: " + intPt);

    // compute a safer result
    // copy the coordinate, since it may be rounded later
    intPt = new Coordinate.fromCoordinate(nearestEndpoint(p1, p2, q1, q2));
//    intPt = CentralEndpointIntersector.getIntersection(p1, p2, q1, q2);

//      System.out.println("Segments: " + this);
//      System.out.println("Snapped to " + intPt);
//      checkDD(p1, p2, q1, q2, intPt);
  }
  if (precisionModel != null) {
    precisionModel!.makeCoordinatePrecise(intPt);
  }
  return intPt;
}