bool PrimitiveLine::Intersection(const PrimitiveLine &other, Coordinate &intersection) const { // compute "line vector" Coordinate myP = points_[1]-points_[0]; // compute normal vector Coordinate myN(-myP.GetYAsFloat(), myP.GetXAsFloat()); assert(myP*myN == 0); myN /= myP.length(); // get starting and ending point of other line Coordinate r = other.GetStartingPoint(); Coordinate s = other.GetEndingPoint(); // are lines parallel? float value = myN * (s-r); if(-0.001 <= value && value <= 0.001) { return false; } // calculate "d" float d = myN * GetStartingPoint(); // calculate parameter of intersection float t = (d-myN*r)/(myN * (s-r)); // calculate intersection intersection = r + t*(s-r); return true; }
float PrimitiveLine::Distance(const Coordinate &point) const { // compute "line vector" Coordinate p = points_[1]-points_[0]; // compute normal vector Coordinate n(-p.GetYAsFloat(), p.GetXAsFloat()); assert(p*n == 0); n /= p.length(); // compute distance float dist = n*(point-points_[0]); return fabs(dist); }