C++之两点不平行线段求交点

// 获取交点
bool CCommonFunction::getCross(AcGePoint3d& line1_pt1, AcGePoint3d& line1_pt2, AcGePoint3d& line2_pt1,AcGePoint3d& line2_pt2, AcGePoint3d& pt)
{

//判断是否共线

if(CCommonFunction::IsOnLine(line1_pt1,line1_pt2,line2_pt2))
pt = line2_pt1;

double p0_x = line1_pt1.x;
double p0_y = line1_pt1.y;
double p1_x = line1_pt2.x;
double p1_y = line1_pt2.y;
double p2_x = line2_pt1.x;
double p2_y = line2_pt1.y;
double p3_x = line2_pt2.x;
double p3_y = line2_pt2.y;

double s1_x, s1_y, s2_x, s2_y;
s1_x = p1_x - p0_x; s1_y = p1_y - p0_y;
s2_x = p3_x - p2_x; s2_y = p3_y - p2_y;

double s, t;
s = (-s1_y * (p0_x - p2_x) + s1_x * (p0_y - p2_y)) / (-s2_x * s1_y + s1_x * s2_y);
t = ( s2_x * (p0_y - p2_y) - s2_y * (p0_x - p2_x)) / (-s2_x * s1_y + s1_x * s2_y);

if (s >= 0 && s <= 1 && t >= 0 && t <= 1)
{
pt.x = p0_x + (t * s1_x);

pt.y = p0_y + (t * s1_y);
pt.z = 0;
return true;
}

return false;
}

posted @ 2020-07-01 17:23  久龄  阅读(52)  评论(0编辑  收藏  举报