/** * Computes the distance between this line segment and another segment. * * @return the distance to the other segment */ public double distance(LineSegment ls) { return Distance.segmentToSegment(p0, p1, ls.p0, ls.p1); }
public void testRandomDisjointCollinearSegments() throws Exception { int n = 1000000; int failCount = 0; for (int i = 0; i < n; i++) { //System.out.println(i); Coordinate[] seg = randomDisjointCollinearSegments(); if (0 == Distance.segmentToSegment(seg[0], seg[1], seg[2], seg[3])) { /* System.out.println("FAILED! - " + WKTWriter.toLineString(seg[0], seg[1]) + " - " + WKTWriter.toLineString(seg[2], seg[3])); */ failCount++; } } System.out.println("# failed = " + failCount + " out of " + n); }
private double computeDistanceLineLine(FacetSequence facetSeq, GeometryLocation[] locs) { // both linear - compute minimum segment-segment distance double minDistance = Double.MAX_VALUE; for (int i = start; i < end - 1; i++) { Coordinate p0 = pts.getCoordinate(i); Coordinate p1 = pts.getCoordinate(i + 1); for (int j = facetSeq.start; j < facetSeq.end - 1; j++) { Coordinate q0 = facetSeq.pts.getCoordinate(j); Coordinate q1 = facetSeq.pts.getCoordinate(j + 1); double dist = Distance.segmentToSegment(p0, p1, q0, q1); if (dist < minDistance) { minDistance = dist; if (locs != null) updateNearestLocationsLineLine(i, p0, p1, facetSeq, j, q0, q1, locs); if (minDistance <= 0.0) return minDistance; } } } return minDistance; }
public void testDistanceLineLineDisjointCollinear() { assertEquals(1.999699, Distance.segmentToSegment( new Coordinate(0,0), new Coordinate(9.9, 1.4), new Coordinate(11.88, 1.68), new Coordinate(21.78, 3.08)), 0.000001); } }
private void computeMinDistance(LineString line0, LineString line1, GeometryLocation[] locGeom) { if (line0.getEnvelopeInternal().distance(line1.getEnvelopeInternal()) > minDistance) return; Coordinate[] coord0 = line0.getCoordinates(); Coordinate[] coord1 = line1.getCoordinates(); // brute force approach! for (int i = 0; i < coord0.length - 1; i++) { for (int j = 0; j < coord1.length - 1; j++) { double dist = Distance.segmentToSegment( coord0[i], coord0[i + 1], coord1[j], coord1[j + 1] ); if (dist < minDistance) { minDistance = dist; LineSegment seg0 = new LineSegment(coord0[i], coord0[i + 1]); LineSegment seg1 = new LineSegment(coord1[j], coord1[j + 1]); Coordinate[] closestPt = seg0.closestPoints(seg1); locGeom[0] = new GeometryLocation(line0, i, closestPt[0]); locGeom[1] = new GeometryLocation(line1, j, closestPt[1]); } if (minDistance <= terminateDistance) return; } } }