r

求P点到AB线的距离,图中的C为求值过程中的点,方便程序理解
// 来段伪代码
Vector2 pos_P = P.GetLocation();
Vector2 pos_A = A.GetLocation();
Vector2 pos_B = B.GetLocation();
Vector2 vector_AP = pos_P - pos_A;
Vector2 vector_AB = pos_B - pos_A;
// AB线段的长度
float dis_AB = Vector2::Dot(vector_AB, vector_AB);
// AC与AB长度的比例
float ratio_Of_AC_to_AB = Vector2::Dot(vector_AP, vector_AB) / dis_AB;
Vector2 vector_AC = ratio_Of_AC_to_AB * vector_AB;
Vector2 vector_PC = vector_AP - vector_AC;
// 求得P到C的距离
float dis_CP = Vector2::Dot(vector_PC, vector_PC);
return dis_PC;
这篇博客介绍了如何使用向量运算求解一点P到线段AB的距离。通过获取P、A、B三点的位置坐标,计算向量AP、AB,并利用点积求得比例关系,进一步得到点C的位置,最后计算点P到C的距离,即为所求。伪代码详细展示了整个计算过程。
32万+

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



