/** * 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 */ LineSegment* LineSegment::project(const LineSegment *seg) const { 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); Coordinate *newp1=project(seg->p1); LineSegment *ret = new LineSegment(*newp0,*newp1); delete newp0; delete newp1; return ret; }
/*public*/ void LineSegment::project(const Coordinate& p, Coordinate& ret) const { if (p==p0 || p==p1) ret=p; double r=projectionFactor(p); ret=Coordinate(p0.x+r*(p1.x-p0.x),p0.y+r*(p1.y-p0.y)); }
/*public*/ double LineSegment::segmentFraction(const Coordinate& inputPt) const { double segFrac = projectionFactor(inputPt); if (segFrac < 0.0) segFrac = 0.0; else if (segFrac > 1.0) segFrac = 1.0; return segFrac; }
/** * 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 * The returned coordinate is a new one, you must delete it afterwards. */ Coordinate* LineSegment::closestPoint(const Coordinate& p) const { 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 new Coordinate(p0); return new Coordinate(p1); }
bool LineSegment::project(const LineSegment& seg, LineSegment& ret) const { double pf0=projectionFactor(seg.p0); double pf1=projectionFactor(seg.p1); // check if segment projects at all if (pf0>=1.0 && pf1>=1.0) return false; if (pf0<=0.0 && pf1<=0.0) return false; Coordinate newp0; project(seg.p0, newp0); Coordinate newp1; project(seg.p1, newp1); ret.setCoordinates(newp0, newp1); return true; }
//Coordinate* void LineSegment::closestPoint(const Coordinate& p, Coordinate& ret) const { double factor=projectionFactor(p); if (factor>0 && factor<1) { project(p, ret); return; } double dist0=p0.distance(p); double dist1=p1.distance(p); if (dist0<dist1) { ret=p0; return; } ret=p1; }
/** * Compute the projection of a point onto the line determined * by this line segment. * <p> * Note that the projected point * may lie outside the line segment. If this is the case, * the projection factor will lie outside the range [0.0, 1.0]. */ Coordinate* LineSegment::project(const Coordinate& p) const { if (p==p0 || p==p1) return new Coordinate(p); double r=projectionFactor(p); return new Coordinate(p0.x+r*(p1.x-p0.x),p0.y+r*(p1.y-p0.y)); }