求一个空间的点到直线的距离最效率的求法

cut 发布于 2013/04/26 15:49
阅读 166
收藏 0
inline Float euler_distance_dot_to_line(const pecker_vector3& line_pointA,const pecker_vector3& line_pointB,const pecker_vector3& pointC)
{
	pecker_vector3 lineAB = line_pointB - line_pointA;
	pecker_vector3 lineAC = pointC - line_pointA;
	// 方法1:
	// lineAB dot  lineAC = |lineAB| |lineAC| * cos<AB,AC> 
	// normalAB = lineAB/|lineAB|;
	//
	// projAB = normalAB * (|lineAC| * cos<AB,AC>) 
	//              =  normalAB * (lineAB dot  lineAC / |lineAB|) 
	//              = (lineAB/|lineAB|)*(lineAB dot  lineAC / |lineAB|)
	//             =  (lineAB dot  lineAC)  / (|lineAB|*|lineAB|) * lineAB 
	//     
	// d*d + |projAB| * |projAB| = |lineAC| * |lineAC|
	// d = sqrt(|lineAC| * |lineAC| - |projAB| * |projAB|);
	Float dotAC_AB = lineAC.dot(lineAB);
	Float sqrt_value = lineAC.length_square() - (dotAC_AB * dotAC_AB / lineAB.length_square());	
         if (sqrt_value >= 0)
	{
		return sqrt(sqrt_value);
	}
	else
	{
		return -1.0f;
	}

	// 方法2:
	// | lineAB cross lineAC | = |lineAB| * |lineAC| * sin<AB,AC>
	// d = |lineAC| * sin<AB,AC> 
	// | lineAB cross lineAC | = |lineAB| * d
	// d = | lineAB cross lineAC | / |lineAB|
	//return (lineAC.cross(lineAB)).length() / lineAB.length();
}
自己写了个,感觉不是很效率,而且不知道对错,求指教
加载中
返回顶部
顶部