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;