/** Returns the distance from this segment to point qq. */ float Cline::d2point(Cpoint *qq) { float t1, iPx, iPy; Cpoint iPoint; Cpoint *ww = new Cpoint((qq->get_xx()-aa.get_xx()),(qq->get_yy()-aa.get_yy())); //ww is the two coordinate vector going from aa to qq if (vv.scalar(ww)<=0) { delete ww; iPoint = &aa; //aa is the closest point to qq return (aa.d2point(qq)); } ww->set_point((qq->get_xx()-bb.get_xx()),(qq->get_yy()-bb.get_yy())); //ww is the two coordinate vector going from bb to qq if (vv.scalar(ww)>=0) { delete ww; iPoint = &bb;//bb is the closest point to qq return (bb.d2point(qq)); } delete ww; t1=(qq->get_xx()-aa.get_xx())*(bb.get_xx()-aa.get_xx())+(qq->get_yy()-aa.get_yy())*(bb.get_yy()-aa.get_yy()); t1=t1/(length*length); //sets iP as normal projection iPx=aa.get_xx()+t1*vv.get_xx(); iPy=aa.get_yy()+t1*vv.get_yy(); iPoint.set_point(iPx,iPy); return (iPoint.d2point(qq)); }
/** Returns the distance from this segment to point qq. Evaluate first if distance is from one of the limiting points (aa or bb). If not, evaluate distance from the segment. Sets to *iPoint the closest point of the segment to qq. */ int Cline::d2point(const Cpoint & qq, Cpoint & iPoint, float & dist) const { //float t1, iPx, iPy; float dotProd; //cout << endl << endl << "************* d2point *************" << endl; //cout << "qq=" << qq << endl; //ww is the two coordinate vector going from bb to qq Cpoint ww((qq.get_xx()-bb.get_xx()),(qq.get_yy()-bb.get_yy())); //check if BB is the closest point dotProd = vv.scalar(ww); if (dotProd >= 0) { iPoint=bb;//bb is the closest point to qq dist = bb.d2point(qq); return iBB; } //ww is the two coordinate vector going from aa to qq ww.set_point((qq.get_xx()-aa.get_xx()),(qq.get_yy()-aa.get_yy())); //check if AA is the closest point dotProd = vv.scalar(ww); if (dotProd <= 0) { iPoint = aa; //aa is the closest point to qq dist = aa.d2point(qq); return iAA; } //point between AA and BB //compute projection of ww onto vv dotProd = dotProd/(length*length); iPoint.set_point(aa.get_xx()+dotProd*vv.get_xx(), aa.get_yy()+dotProd*vv.get_yy()); /* cout << "iPoint=" << iPoint << endl; Cline auxLine1(aa, iPoint); Cline auxLine2(iPoint, bb); cout << "SEGMENT!" << endl; printLine(); cout << "\tdir1=" << getDirection() << endl; cout << "auxLine1(aa, iPoint)=" << endl; auxLine1.printLine(); cout << "\tdir1=" << auxLine1.getDirection() << endl; cout << "auxLine2(iPoint, bb)=" << endl; auxLine2.printLine(); cout << "\tdir2=" << auxLine2.getDirection() << endl; */ dist = iPoint.d2point(qq); return iAB; }