以下代码来自 用例CAAMthBase ,属于 CAAMathematics.edu 框架,路径如下:
Windows InstallRootDirectory\CAAMathematics.edu\CAAMthBase.m
Unix InstallRootDirectory/CAAMathematics.edu/CAAMthBase.m/
CATMathPoint O, A(20. ,10. ,0.) ; // Default constructor, O is (0.,0.,0.)
CATMathVector u(10., 20. ,0.);
u.Normalize(); // Normalize u;
// H: Orthogonal projection of A on the line (O,u):
// Use the operators
// A-O is a vector, (A-O)*u the dot product
//
CATMathVector OA = A - O ;
CATMathPoint H = O + ( OA*u ) * u;
// Computes the normal of the two vectors (A-O) and u: ^ is the cross product
CATMathVector n = OA ^ u;
//
// Another way to project to get H:
// use the Project method of the CATMathLine class.
//
CATMathLine line(O,u);
CATMathPoint projection;
line.Project( A , projection );
// Returns the distance between the two computed points.
//If non nul, it is an error.
if ( H.SquareDistanceTo( projection ) != 0. ) return (1);
// Outputs the coordinates of the projected points
double aCoord[3];
H.GetCoord( aCoord );
cout << "coordinates of the projected point : "
<< aCoord[0] << "\t"
<< aCoord[1] << "\t"
<< aCoord[2] << endl;
注意 这一句:
CATMathPoint H = O + ( OA*u ) * u;
网友评论