/** * 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); } /**
/** * Computes a split point which is the projection of the encroaching point on the segment * * @param seg * @param encroachPt * @return a split point on the segment */ public static Coordinate projectedSplitPoint(Segment seg, Coordinate encroachPt) { LineSegment lineSeg = seg.getLineSegment(); Coordinate projPt = lineSeg.project(encroachPt); return projPt; } }
/** * Computes the closest point on this line segment to another point. * @param p the point to find the closest point to * @return a Coordinate which is the closest point on the line segment to the point p */ public Coordinate closestPoint(Coordinate p) { double factor = projectionFactor(p); if (factor > 0 && factor < 1) { return project(p); } double dist0 = p0.distance(p); double dist1 = p1.distance(p); if (dist0 < dist1) return p0; return p1; } /**
/** * Gets a {@link LineString} which is a minimum diameter * * @return a {@link LineString} which is a minimum diameter */ public LineString getDiameter() { computeMinimumDiameter(); // return empty linestring if no minimum width calculated if (minWidthPt == null) return inputGeom.getFactory().createLineString(); Coordinate basePt = minBaseSeg.project(minWidthPt); return inputGeom.getFactory().createLineString(new Coordinate[] { basePt, minWidthPt } ); }