给定平面上两条线段LineA与LineB,以及对应的端点坐标,计算出两条线段的交点
LineA (LineA_Pt1, LineA_Pt2) LineB(LineB_Pt1, LineB_Pt2)
// 仅适用于2D PointXY
templete<Point>
Point GetInterSectionPt(const Point &LineA_Pt1, const Point &LineA_Pt2,
const Point &LineB_Pt1, const Point &LineB_Pt2)
{
Point LineA_dir = LineA_Pt2 - LineA_Pt1;
LineA_dir.Normalize();
Point dir1 = LineB_Pt1 - LineA_Pt1;
Point dir2 = LineB_Pt2 - LineA_Pt1;
Double dProj1 = sqrt(pow(dir1.GetLength(), 2) - pow(dir1*LineA_dir, 2));
Double dProj2 = sqrt(pow(dir2.GetLength(), 2) - pow(dir2*LineA_dir, 2));
Point InterSection = (LineB_Pt1 * dProj2 + LineB_Pt2 * dProj1) / (dProj1 + dProj2);
return InterSection;
}

本文介绍了一种计算平面内两条线段交点的方法,通过解析几何原理,利用向量运算和投影概念,准确地求解出任意两条线段的交点坐标。
264

被折叠的 条评论
为什么被折叠?



