/* University College London Dept of P&A course in C++ 3C59 | all rights reserved 1999 | | Example: Dot Product | | "Use of functions and in-built data types to perform 3-vector dot product" | | Author: P.Clarke */ #include #include //.................................. // Here is the dot product function float dotProduct( float x1, float y1, float z1, float x2, float y2, float z2 ) { return (x1*x2) + (y1*y2) + (z1*z2) ; } //.................................. // Here is the magnitude function float magnitude( float x, float y, float z ) { return sqrt( (x*x) + (y*y) + (z*z) ) ; } //........................................... // Now the main program to use the functions 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 prod ; prod = dotProduct( px1, py1, pz1, px2, py2, pz2 ) ; // Calculate the magnitude of each vector float mag1, mag2 ; mag1 = magnitude( px1, py1, pz1 ) ; mag2 = magnitude( px2, py2, pz2 ) ; // Calculate the cosine of the angle between the vectors, // and then find the angle itself float cosTheta, theta ; cosTheta = prod / mag1 / mag2 ; theta = acos( cosTheta ) ; // .... of course we could have written a function to do this // .... last bit , why dont you... // Finally print it out std::cout << "TTTTTTTTTTTTTTTT 3C59 example TTTTTTTTTTTTTTTT" << std::endl ; std::cout << " dprod2: This is the new (slightly) improved version " << std::endl; std::cout << " The angle is " << theta << " radians " << std::endl ; std::cout << "LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL" << std::endl ; return 1 ; }