如果2线传递给函数,怎么可以推断他们是否会相交?每行都以2个CPoint
对象的形式 - 总共有4个点。以下是我想到的。它计算每条线的斜率和Y轴截距,并从中计算出交点。然后检查交点是否位于段上;如果是,则返回false;如果不是,即它们不相交,则返回true。 虽然,它显示了很多问题。有没有更简单,更有效的方法来做到这一点?
注意:我不是很确定哪些数据类型用于斜率和Y轴截距。
bool CShortestPathFinderDoc::edgeTest(CPoint P,CPoint P2,CPoint E,CPoint E2)
{
bool status=true;
double m1,m2; //slopes
double b1,b2; //y-intercepts
double y,x; //intersection point
m1=((double)P.y-P2.y)/((double)P.x-P2.x);
m2=((double)E.y - E2.y)/((double)E.x - E2.x);
if(m1 == m2) //if lines are colinear
return true;
b1=P.y-(m1*P.x) // Get the..
b2=E->y - (m2*E.x); // Y-intercepts.
x=(b2-b1)/(m1-m2);
y=m1*x + b1; //x,y is the intersection point!!!
if((x<P2.x && x>P.x)) //if intersection point lies on line!!!!!
{
if(P2.y > P.y)
if(y<P2.y && y>P.y)
status=false;
if(P2.y < P.y)
if(y<P.y && y>P2.y)
status=false;
}
return status;
}