/* University College London dept of physics course in C++ 3C59 | all rights reserved 1999 | | Example: Dot Product | | "First First use of ThreeVector class | Used to make 3-vector dot product simpler " | | Author: P.Clarke */ #include #include #include "../util/ThreeVector_firstgo.h" ///....................................... // Here is the new dot product function float dotProduct( ThreeVector v, ThreeVector w ) { return (v.px*w.px) + (v.py*w.py) + (v.pz*w.pz) ; } //...................................... // Here is the new magnitude function float magnitude( ThreeVector v ) { return sqrt( (v.px*v.px) + (v.py*v.py) + (v.pz*v.pz) ) ; } //.............. // Main program int main() { // Declare some 3-vectors ThreeVector vec1 ; ThreeVector vec2 ; // Set them to something vec1.px = 1. ; vec1.py = 1.5 ; vec1.pz = 1.6 ; vec2.px = -0.3 ; vec2.py = 1.2 ; vec2.pz = 1. ; // .... wouldnt it be a good idea to write an initialise(..) // .... funtion to do the above...... // Take the dot product float prod ; prod = dotProduct( vec1, vec2 ) ; // Calculate the magnitude of each vector float mag1, mag2 ; mag1 = magnitude( vec1 ) ; mag2 = magnitude( vec2 ) ; // 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 ) ; // Finally print it out std::cout << "TTTTTTTTTTTTTTTT 3C59 example TTTTTTTTTTTTTTTT" << std::endl ; std::cout << " dprod3: This is the (slightly more) improved version " << std::endl ; std::cout << " The angle is " << theta << " radians " << std::endl ; std::cout << "LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL" << std::endl ; return 1 ; }