2012-02-19 8 views
1

2行(AとB - 下の図を参照)があります。ラインAは、p1で始まり、p2で終わります。ラインBは、p3で始まり、p4で終わります。2D内の2本の線間の距離

ポイントp5は、ラインAがラインBと交差する点です。

p2とp5の距離を調べるにはどうすればよいですか?

EDIT: をより明確にするために、どのように私は点P5を見つけるのですか?

よろしく!

+0

p5を見つけたり、p2とp5の距離を見つけるのに問題がありますか? –

+0

申し訳ありませんが、p5 – vgr

答えて

0

は、次に、あなただけの距離を取得するためにp2.dist(P5)を呼び出すことができます

// 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)); 
    } 
}; 

で交差点を取得します。

関連する問題