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))); }
public SplitSegment(LineSegment seg) { this.seg = seg; segLen = seg.getLength(); }
private double segmentNearestMeasure(LineSegment seg, Coordinate inputPt, double segmentStartMeasure) { // found new minimum, so compute location distance of point double projFactor = seg.projectionFactor(inputPt); if (projFactor <= 0.0) return segmentStartMeasure; if (projFactor <= 1.0) return segmentStartMeasure + projFactor * seg.getLength(); // projFactor > 1.0 return segmentStartMeasure + seg.getLength(); } }
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; }
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))); }
private Coordinate computePoint(LineSegment seg, double dist) { double dx = seg.p1.x - seg.p0.x; double dy = seg.p1.y - seg.p0.y; double len = seg.getLength(); Coordinate pt = new Coordinate(dist * dx / len, dist * dy / len); pm.makePrecise(pt); return pt; }
public Geometry getGeometry() { int level = recursionLevelForSize(numPts); LineSegment baseLine = getSquareBaseLine(); Coordinate[] pts = getBoundary(level, baseLine.getCoordinate(0), baseLine.getLength()); return geomFactory.createPolygon( geomFactory.createLinearRing(pts), null); }
/** * A basic strategy for finding split points when nothing extra is known about the geometry of * the situation. * * @param seg the encroached segment * @param encroachPt the encroaching point * @return the point at which to split the encroached segment */ public Coordinate findSplitPoint(Segment seg, Coordinate encroachPt) { LineSegment lineSeg = seg.getLineSegment(); double segLen = lineSeg.getLength(); double midPtLen = segLen / 2; SplitSegment splitSeg = new SplitSegment(lineSeg); Coordinate projPt = projectedSplitPoint(seg, encroachPt); /** * Compute the largest diameter (length) that will produce a split segment which is not * still encroached upon by the encroaching point (The length is reduced slightly by a * safety factor) */ double nonEncroachDiam = projPt.distance(encroachPt) * 2 * 0.8; // .99; double maxSplitLen = nonEncroachDiam; if (maxSplitLen > midPtLen) { maxSplitLen = midPtLen; } splitSeg.setMinimumLength(maxSplitLen); splitSeg.splitAt(projPt); return splitSeg.getSplitPoint(); }
/** * Densifies a coordinate sequence. * * @param pts * @param distanceTolerance * @return the densified coordinate sequence */ private static Coordinate[] densifyPoints(Coordinate[] pts, double distanceTolerance, PrecisionModel precModel) { LineSegment seg = new LineSegment(); CoordinateList coordList = new CoordinateList(); for (int i = 0; i < pts.length - 1; i++) { seg.p0 = pts[i]; seg.p1 = pts[i + 1]; coordList.add(seg.p0, false); double len = seg.getLength(); int densifiedSegCount = (int) (len / distanceTolerance) + 1; if (densifiedSegCount > 1) { double densifiedSegLen = len / densifiedSegCount; for (int j = 1; j < densifiedSegCount; j++) { double segFract = (j * densifiedSegLen) / len; Coordinate p = seg.pointAlong(segFract); precModel.makePrecise(p); coordList.add(p, false); } } } coordList.add(pts[pts.length - 1], false); return coordList.toCoordinateArray(); }
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; }
@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); }
@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(level, 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); }