/* University College London Dept of P&A course in C++ 3C59 | all rights reserved 1999 | | Example: Dot Product | | "Use of in-built data types to perform 3-vector dot product" | | Author: P.Clarke */ #include #include #define PI 3.14159 int main() { // Declare some variables to represent two 3-vectors float px1, py1, pz1 ; float px2, py2, pz2 ; // Set them to something px1 = 1. ; py1 = 1.5 ; pz1 = 1.6 ; px2 = -0.3 ; py2 = 1.2 ; pz2 = 1. ; // Take the dot product float dotProduct ; dotProduct = (px1 * px2) + (py1 * py2) + (pz1 * pz2) ; // Calculate the magnitude of each vector float mag1, mag2 ; float mag1sq, mag2sq ; mag1sq = (px1 * px1) + (py1 * py1) + (pz1 * pz1) ; mag2sq = (px2 * px2) + (py2 * py2) + (pz2 * pz2) ; mag1 = sqrt( mag1sq ) ; mag2 = sqrt( mag2sq ) ; // Calculate the cosine of the angle between the vectors, // and then find the angle itself float cosTheta, theta ; cosTheta = dotProduct / mag1 / mag2 ; theta = acos( cosTheta ) ; // Finally print it out std::cout << "TTTTTTTTTTTTTTTT 3C59 example TTTTTTTTTTTTTTTT" << std::endl ; std::cout << " dprod1: The angle between the vectors is " << theta << " radians " << std::endl ; std::cout << "LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL" << std::endl ; // Finish by sending back the number 1 to indicate success return 1 ; }