public static double angleBetween(LineString l1, LineString l2, double tol) { LineSegment ls1 = new LineSegment( l1.getCoordinateN(l1.getNumPoints() - 2), l1.getCoordinateN(l1.getNumPoints() - 1)); LineSegment ls2 = new LineSegment(l2.getCoordinateN(0), l2.getCoordinateN(1)); return (angleBetween(ls1, ls2, tol)); }
private int findFurthestPoint(Coordinate[] pts, int i, int j, double[] maxDistance) { LineSegment seg = new LineSegment(); seg.p0 = pts[i]; seg.p1 = pts[j]; double maxDist = -1.0; int maxIndex = i; for (int k = i + 1; k < j; k++) { Coordinate midPt = pts[k]; double distance = seg.distance(midPt); if (distance > maxDist) { maxDist = distance; maxIndex = k; } } maxDistance[0] = maxDist; return maxIndex; }
private Coordinate[] generateSegmentCurve(Coordinate p0, Coordinate p1, double width0, double width1) { LineSegment seg = new LineSegment(p0, p1); double dist0 = width0 / 2; double dist1 = width1 / 2; Coordinate s0 = seg.pointAlongOffset(0, dist0); Coordinate s1 = seg.pointAlongOffset(1, dist1); Coordinate s2 = seg.pointAlongOffset(1, -dist1); Coordinate s3 = seg.pointAlongOffset(0, -dist0); Coordinate[] pts = new Coordinate[] { s0, s1, s2, s3, s0 }; return pts; }
public static double angleBetween(LineSegment l1, LineSegment l2, double tol) { // analyze slopes // TODO straight vertical lines double s1 = (l1.p1.y - l1.p0.y) / (l1.p1.x - l1.p0.x); double s2 = (l2.p1.y - l2.p0.y) / (l2.p1.x - l2.p0.x); if (Math.abs(s1 - s2) < tol) return (0); if (Math.abs(s1 + s2) < tol) return (Math.PI); // not of equal slope, transform lines so that they are tail to tip and // use the cosine law to calculate angle between // transform line segments tail to tail, originating at (0,0) LineSegment tls1 = new LineSegment( new Coordinate(0, 0), new Coordinate(l1.p1.x - l1.p0.x, l1.p1.y - l1.p0.y)); LineSegment tls2 = new LineSegment( new Coordinate(0, 0), new Coordinate(l2.p1.x - l2.p0.x, l2.p1.y - l2.p0.y)); // line segment for third side of triangle LineSegment ls3 = new LineSegment(tls1.p1, tls2.p1); double c = ls3.getLength(); double a = tls1.getLength(); double b = tls2.getLength(); return (Math.acos((a * a + b * b - c * c) / (2 * a * b))); }
int i = 0; while (i + 2 < c.length) { LineSegment ls1 = new LineSegment(c[i], c[i + 1]); LineSegment ls2 = new LineSegment(c[i + 1], c[i + 2]); double a1 = ls1.angle(); double a2 = ls2.angle(); if (!((a1 - degreesAllowable) < a1 && (a1 + degreesAllowable) > a2)) { results.error(
private Coordinate displaceFromSeg(LineSegment nearSeg, double dist) { double frac = nearSeg.projectionFactor(vertexPt); // displace away from the segment on the same side as the original point int side = nearSeg.orientationIndex(vertexPt); if (side == Orientation.RIGHT) dist = -dist; return nearSeg.pointAlongOffset(frac, dist); }
private void updatePts(Coordinate p, Coordinate seg0, Coordinate seg1) { minPts[0] = p; LineSegment seg = new LineSegment(seg0, seg1); minPts[1] = new Coordinate(seg.closestPoint(p)); }
private Coordinate displaceFromPoint(Coordinate nearPt, double dist) { LineSegment seg = new LineSegment(nearPt, vertexPt); // compute an adjustment which displaces in the direction of the nearPt-vertexPt vector // TODO: make this robust! double len = seg.getLength(); double frac = (dist + len) / len; Coordinate strPt = seg.pointAlong(frac); return strPt; }
@Override public void filter(Coordinate coord) { if(firstPt != null) { LineSegment segment = new LineSegment(firstPt, coord); segment.normalize(); segments.add(segment); } firstPt = coord; }
private double indexOfFromStart(Coordinate inputPt, double minIndex) { double minDistance = Double.MAX_VALUE; double ptMeasure = minIndex; double segmentStartMeasure = 0.0; LineSegment seg = new LineSegment(); LinearIterator it = new LinearIterator(linearGeom); while (it.hasNext()) { if (! it.isEndOfLine()) { seg.p0 = it.getSegmentStart(); seg.p1 = it.getSegmentEnd(); double segDistance = seg.distance(inputPt); double segMeasureToPt = segmentNearestMeasure(seg, inputPt, segmentStartMeasure); if (segDistance < minDistance && segMeasureToPt > minIndex) { ptMeasure = segMeasureToPt; minDistance = segDistance; } segmentStartMeasure += seg.getLength(); } it.next(); } return ptMeasure; }
public void testProjectionFactor() { // zero-length line LineSegment seg = new LineSegment(10, 0, 10, 0); assertTrue(Double.isNaN(seg.projectionFactor(new Coordinate(11, 0)))); LineSegment seg2 = new LineSegment(10, 0, 20, 0); assertTrue(seg2.projectionFactor(new Coordinate(11, 0)) == 0.1); }
private LineString voronoiSide(int idgeom, int side,GeometryFactory geometryFactory, Coordinate circumcenter) { boolean triangleCCW = isCCW(idgeom); // Create linestring to envelope LineSegment sideGeom = getTriangleSegment(idgeom, side); Vector2D direction = new Vector2D(sideGeom.p0, sideGeom.p1); direction = direction.normalize().rotate(triangleCCW ? - Math.PI / 2 : Math.PI / 2).multiply(envelope.maxExtent()); LineSegment voronoiLine = new LineSegment(circumcenter, new Coordinate(direction.getX() + circumcenter.x, direction.getY() + circumcenter.y)); Geometry lineString = voronoiLine.toGeometry(geometryFactory).intersection(geometryFactory.toGeometry(envelope)); if(lineString instanceof LineString && lineString.getLength() > epsilon) { return (LineString)lineString; } else { return null; } }
private void updateNearestLocationsLineLine(int i, Coordinate p0, Coordinate p1, FacetSequence facetSeq, int j, Coordinate q0, Coordinate q1, GeometryLocation[] locs) { LineSegment seg0 = new LineSegment(p0, p1); LineSegment seg1 = new LineSegment(q0, q1); Coordinate[] closestPt = seg0.closestPoints(seg1); locs[0] = new GeometryLocation(geom, i, new Coordinate(closestPt[0])); locs[1] = new GeometryLocation(facetSeq.geom, j, new Coordinate(closestPt[1])); }
public static void computeDistance(LineString line, Coordinate pt, PointPairDistance ptDist) { LineSegment tempSegment = new LineSegment(); Coordinate[] coords = line.getCoordinates(); for (int i = 0; i < coords.length - 1; i++) { tempSegment.setCoordinates(coords[i], coords[i + 1]); // this is somewhat inefficient - could do better Coordinate closestPt = tempSegment.closestPoint(pt); ptDist.setMinimum(closestPt, pt); } }
/** * Project a line segment onto this line segment and return the resulting * line segment. The returned line segment will be a subset of * the target line line segment. This subset may be null, if * the segments are oriented in such a way that there is no projection. * <p> * Note that the returned line may have zero length (i.e. the same endpoints). * This can happen for instance if the lines are perpendicular to one another. * * @param seg the line segment to project * @return the projected line segment, or <code>null</code> if there is no overlap */ public LineSegment project(LineSegment seg) { double pf0 = projectionFactor(seg.p0); double pf1 = projectionFactor(seg.p1); // check if segment projects at all if (pf0 >= 1.0 && pf1 >= 1.0) return null; if (pf0 <= 0.0 && pf1 <= 0.0) return null; Coordinate newp0 = project(seg.p0); if (pf0 < 0.0) newp0 = p0; if (pf0 > 1.0) newp0 = p1; Coordinate newp1 = project(seg.p1); if (pf1 < 0.0) newp1 = p0; if (pf1 > 1.0) newp1 = p1; return new LineSegment(newp0, newp1); } /**
double minFrac = -1.0; LineSegment seg = new LineSegment(); for (LinearIterator it = new LinearIterator(linearGeom); it.hasNext(); it.next()) { seg.p0 = it.getSegmentStart(); seg.p1 = it.getSegmentEnd(); double segDistance = seg.distance(inputPt); double segFrac = seg.segmentFraction(inputPt);
void checkOrientationIndex(LineSegment seg, double s0x, double s0y, double s1x, double s1y, int expectedOrient) { LineSegment seg2 = new LineSegment(s0x, s0y, s1x, s1y); int orient = seg.orientationIndex(seg2); assertTrue(orient == expectedOrient); }
boolean checkTriangleConsistent(Coordinate[] triPts, double lenFraction) { testCount++; LineSegment seg = new LineSegment(triPts[1], triPts[2]); Coordinate pt = seg.pointAlong(lenFraction);
private int findMaxPerpDistance(Coordinate[] pts, LineSegment seg, int startIndex) { double maxPerpDistance = seg.distancePerpendicular(pts[startIndex]); double nextPerpDistance = maxPerpDistance; int maxIndex = startIndex; int nextIndex = maxIndex; while (nextPerpDistance >= maxPerpDistance) { maxPerpDistance = nextPerpDistance; maxIndex = nextIndex; nextIndex = nextIndex(pts, maxIndex); nextPerpDistance = seg.distancePerpendicular(pts[nextIndex]); } // found maximum width for this segment - update global min dist if appropriate if (maxPerpDistance < minWidth) { minPtIndex = maxIndex; minWidth = maxPerpDistance; minWidthPt = pts[minPtIndex]; minBaseSeg = new LineSegment(seg); // System.out.println(minBaseSeg); // System.out.println(minWidth); } return maxIndex; }
@Override public Geometry getGeometry() { int level = level(numPts); int nPts = size(level); double scale = 1; double baseX = 0; double baseY = 0; if (extent != null) { LineSegment baseLine = getSquareBaseLine(); baseX = baseLine.minX(); baseY = baseLine.minY(); double width = baseLine.getLength(); int maxOrdinate = maxOrdinate(level); scale = width / maxOrdinate; } Coordinate[] pts = new Coordinate[nPts]; for (int i = 0; i < nPts; i++) { Coordinate pt = decode(i); double x = transform(pt.getX(), scale, baseX); double y = transform(pt.getY(), scale, baseY); pts[i] = new Coordinate(x, y); } return geomFactory.createLineString(pts); }