// sv-3.cc // A ZOOM PhysicsVectors tutorial example // // Tutorial example 3 for the SpaceVector and UnitVector class: // // UnitVectors // // * Constructing UnitVectors // * Using different forms of coordinates // * Changing individual components // * The coordinate axis UnitVectors // Key lines are marked by // <------------- // Get the UnitVector class #include "ZMutility/ZMenvironment.h" #include "ZMutility/FixedTypes.h" #include "PhysicsVectors/UnitVector.h" ZM_USING_NAMESPACE( zmpv ) USING( std::cout ) USING( std::endl ) // These classes themselves pull in FixedTypes, but just to // remind ourselves that we are using Float8, not double, for // 64-bit floats... #include "ZMutility/FixedTypes.h" ZM_USING_NAMESPACE( zmfxt ) // This example will use iostream output, so get that -- // in a portable way. #include "ZMutility/iostream" using std::cout; using std::endl; const Float8 PI = 3.1415926535897931; int main() { // ***** Constructing a UnitVector // Here is how to construct a unit vector from theta, phi //------------------------------------------------------- cout << "Constructing u1 from theta and phi \n"; Float8 theta = 45; Float8 phi = 30; UnitVector u1 ( theta, DEGREES, phi, DEGREES ); // <------------- // Note the use of the keyword DEGREES; this and RADIANS // can be mixed, if you wish. UnitVector u2 ( 45, DEGREES, PI/6, RADIANS ); cout << "u1 =" << u1 << endl; cout << "u2 =" << u1 << endl; // Here is how to construct a unit vector by normalizing x, y, z //-------------------------------------------------------------- cout << "Constructing u3 by normalizing cartesian coordinates \n"; Float8 x = 3; Float8 y = 4; Float8 z = 12; UnitVector u3 ( x, y, z ); // <------------- cout << "u3 =" << u3 << endl; //cout << "13 times that UnitVector is " << 13 * SpaceVector(u3) << endl; cout << "13 times that UnitVector is " << 13 * u3 << endl; // Here is how to construct a unit vector by normalizing a SpaceVector //-------------------------------------------------------------------- SpaceVector v1 (24, 6, -8); UnitVector u4(v1); // <------------- cout << "The UnitVector constructed from " << v1 << " is " << u4 << endl; // How to construct an array of UnitVectors: //------------------------------------------- // These hundred UnitVectors will be initialized to (0,0,1) = Z_HAT UnitVector uu [100]; // ***** Using different forms of coordinates // Here is how to obtain the polar coordinates of a UnitVector: //-------------------------------------------------------------- theta = u1.theta(); // <------------- phi = u1.phi(); // <------------- Float8 r = u1.r(); // <------------- // u1.r(), of course, returns 1 without having to think. cout << "u1 has r= " << r << " theta = " << theta << " phi = " << phi << endl; // Here is how to obtain the Cartesian coordinates of a UnitVector: //------------------------------------------------------------------ x = u2.x(); // <------------- y = u2.y(); // <------------- z = u2.z(); // <------------- cout << "u2 has x= " << x << " y = " << y << " z = " << z << endl; // ***** Changing individual components // Here is how to change phi or theta, keeping the other fixed //------------------------------------------------------------ cout << "u1 =" << u1 << endl; u1.setPhi( -PI/6 ); // <------------- cout << "Now u1 =" << u1 << endl; u1.setTheta( PI/3 ); // <------------- cout << "Now u1 =" << u1 << endl; // You can also use the modify/assign operators u1.setPhi( u1.phi() * -1 ); // <------------- cout << "I negated phi; now u1 =" << u1 << endl; // Here is how to change phi and theta both at once //------------------------------------------------- u2.set ( 45, DEGREES, PI, RADIANS ); // <------------- cout << "Now u2 =" << u2 << endl; // ***** The coordinate axis UnitVectors // Here is how to use X_HAT, Y_HAT and Z_HAT //------------------------------------------ UnitVector u5 = Y_HAT; // <------------- cout << "Y_HAT = " << u5 << endl; SpaceVector v3 = X_HAT - Z_HAT; // <------------- cout << "X_HAT - Z_HAT = " << v3 << endl; // Notice that the sum of two UnitVectors is a SpaceVector return 0; } /* end of main */