Reputation: 543
We have 2 lines (A and B - see picture below). Line A starts at p1 and ends at p2 Line B starts at p3 and ends at p4.
Point p5 is the point that line A intersects line B if extended.
How do I find out the distance between p2 and p5?
EDIT: To be more clear, how do I find point p5?
Best Regards!
Upvotes: 0
Views: 1216
Reputation: 76
Get the intersection by
// 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));
}
};
Then you can just call p2.dist(p5) to get the distance.
Upvotes: 0
Reputation: 472
Use the point slope form to create two equations. Then solve them simultaneously and use the distance formula to find distance.
Upvotes: 2