Получите пересечение по
// each pair of points decides a line
// return their intersection point
Point intersection(const Point& l11, const Point& l12, const Point& l21, const Point& l22)
{
double a1 = l12.y-l11.y, b1 = l11.x-l12.x;
double c1 = a1*l11.x + b1*l11.y;
double a2 = l22.y-l21.y, b2 = l21.x-l22.x;
double c2 = a2*l21.x + b2*l21.y;
double det = a1*b2-a2*b1;
assert(fabs(det) > EPS);
double x = (b2*c1-b1*c2)/det;
double y = (a1*c2-a2*c1)/det;
return Point(x,y);
}
// here is the point class
class Point
{
public:
double x;
double y;
public:
Point(double xx, double yy)
: x(xx), y(yy)
{}
double dist(const Point& p2) const
{
return sqrt((x-p2.x)*(x-p2.x) + (y-p2.y)*(y-p2.y));
}
};
Тогда вы можете просто вызвать p2.dist (p5), чтобы получить расстояние.