#include #include #include "Vector_3d.hpp" ///////////////////////////////////////////////// // This file contains all function definitions // // - All function declarations are in // // the file Vector_3d.hpp // ///////////////////////////////////////////////// ////// // These query the arguments, but do not change them ////// // Calculate the inner product of u and v. double inner( Vector_3d const &u, Vector_3d const &v ) { return u.x_*v.x_ + u.y_*v.y_ + u.z_*v.z_; } // Calculate the norm (length) of u. double norm( Vector_3d const &u ) { return std::sqrt( inner( u, u ) ); } // Calculate u + v. // - Notice that by lining up the values that // are initializing the new vector, it is // easy to see the values are correct. Vector_3d add( Vector_3d const &u, Vector_3d const &v ) { return Vector_3d{ u.x_ + v.x_, u.y_ + v.y_, u.z_ + v.z_ }; } // Calculate a*u for a scalar 'a'. Vector_3d mul( double const a, Vector_3d const &u ) { return Vector_3d{ a*u.x_, a*u.y_, a*u.z_ }; } // Calculate u*a for a scalar 'a'. Vector_3d mul( Vector_3d const &u, double const a ) { return mul( a, u ); } // Calculate -u. Vector_3d negate( Vector_3d const &u ) { return mul( -1.0, u ); } // Calculate u - v. Vector_3d sub( Vector_3d const &u, Vector_3d const &v ) { return add( u, negate( v ) ); } // Calculate (1/a)*u for a non-zero scalar 'a'. Vector_3d div( Vector_3d const &u, double const a ) { assert( a != 0.0 ); return mul( 1.0/a, u ); } // Calculate the projection of u onto v. Vector_3d proj( Vector_3d const &u, Vector_3d const &v ) { // The inner product of v and itself is 0 if and // only if v is zero, and the projection onto the // zero vector is the zero vector. double inner_v_v{ inner( v, v ) }; if ( inner_v_v == 0.0 ) { return v; } else { return mul( inner( v, u )/inner_v_v, v ); } } // Calculate the cross product u x v. Vector_3d cross( Vector_3d const &u, Vector_3d const &v ) { return Vector_3d{ u.y_*v.z_ - u.z_*v.y_, u.z_*v.x_ - u.x_*v.z_, u.x_*v.y_ - u.y_*v.x_ }; } // Determine whether u and v are equal. bool equal( Vector_3d const &u, Vector_3d const &v ) { return (u.x_ == v.x_) && (u.y_ == v.y_) && (u.z_ == v.z_); } // Return a string representation of u. std::string to_string( Vector_3d const &u ) { return "(" + std::to_string( u.x_ ) + ", " + std::to_string( u.y_ ) + ", " + std::to_string( u.z_ ) + ")"; } ////// // These modify the first vector passed ////// // Normalize u if it is not the zero vector. void normalize( Vector_3d &u ) { double norm_u{ norm( u ) }; if ( norm_u != 0.0 ) { div_by( u, norm_u ); } } // Add v to u. void add_to( Vector_3d &u, Vector_3d const &v ) { u.x_ += v.x_; u.y_ += v.y_; u.z_ += v.z_; } // Subtract v from u. void sub_from( Vector_3d &u, Vector_3d const &v ) { u.x_ -= v.x_; u.y_ -= v.y_; u.z_ -= v.z_; } // Multiply u by the scalar a. void mul_by( Vector_3d &u, double const a ) { u.x_ *= a; u.y_ *= a; u.z_ *= a; } // Divide u by the non-zero scalar a. void div_by( Vector_3d &u, double const a ) { assert( a != 0.0 ); u.x_ /= a; u.y_ /= a; u.z_ /= a; }