////////////////////////////////////////////////////////////////////////
// //
// Closest Point On A Line //
// //
////////////////////////////////////////////////////////////////////////
float
Distance(D3DXVECTOR3 vPoint1, D3DXVECTOR3 vPoint2 )
{
float Vt = vPoint1 - vPoint2;
float fLength = D3DXLenght( Vt );
return fLength;
}
D3DXVECTOR3 ClosestPointOnLine( D3DXVECTOR3 vA, D3DXVECTOR3 vB,
D3DXVECTOR3 vPoint )
{
// vA-vB are the two points for our line.
// vPoint is our point.
vABDir = vB - vA;
D3DXVec3Normalize( &vABDir, &vABDir );
float d = Distance(vA, vB);
// Distance between A and vPoint
D3DXVECTOR3 v1 = vPoint - vA;
float t = Dot(vABDir, v1);
if( t<=0 )
return vA;
if( t>= d)
return vB;
D3DXVECTOR3 v3 = vABDir * t;
D3DXVECTOR3 vClosestPoint = vA + v3;
return vClosestPoint;
} |