// TwoSpacePoint.cpp #include "TwoSpacePoint.h" #include #include #ifndef DEFECT_NO_STDLIB_NAMESPACES #ifndef DEFECT_CMATH_NOT_STD using std::fabs; using std::cos; using std::sin; using std::sqrt; using std::atan2; #endif using std::ostream; using std::endl; #endif //********************************************************************** // Statics //********************************************************************** // Return if two doubles are close enough to be considered equal. bool TwoSpacePoint::equal(double x1, double x2) { double maxdif = 2.0*std::numeric_limits::epsilon(); double num = 2.0*(x2-x1); double den = x2 + x1; double dif = fabs(num/den); return dif <= maxdif; } //********************************************************************** // Member functions. //********************************************************************** // Constructor. // Initial point is the origin with phi = theta = 0. TwoSpacePoint::TwoSpacePoint( ) { _x = _y = 0.0; _xy = 0.0; _phi = 0.0; } //********************************************************************** // Destructor. TwoSpacePoint::~TwoSpacePoint( ) { } //********************************************************************** // return cos(phi) double TwoSpacePoint::cos_phi( ) const { if ( _xy ) return _x/_xy; return cos(_phi); } //********************************************************************** // return sin(phi) double TwoSpacePoint::sin_phi( ) const { if ( _xy ) return _y/_xy; return sin(_phi); } //********************************************************************** // Construct a Cartesian point. CartesianTwoPoint::CartesianTwoPoint(double x, double y) { _x = x; _y = y; _xy = sqrt(_x*_x+_y*_y); _phi = atan2(_y,_x); } //********************************************************************** // Construct a cylindrical point. CylindricalTwoPoint::CylindricalTwoPoint(double r, double phi) { _xy = r; _phi = phi; _x = r*cos(phi); _y = r*sin(phi); } //********************************************************************** // Output stream operator. ostream& operator<<(ostream & stream, const TwoSpacePoint& spt) { stream << " x: " << spt.x() << endl; stream << " y: " << spt.y() << endl; stream << " rxy: " << spt.rxy() << endl; stream << " phi: " << spt.phi(); return stream; } //********************************************************************** // Free functions. //********************************************************************** // Equality. bool operator==(const TwoSpacePoint& lhs, const TwoSpacePoint& rhs) { return TwoSpacePoint::equal( lhs.x(), rhs.x() ) && TwoSpacePoint::equal( lhs.y(), rhs.y() ); } //********************************************************************** // Inequality. bool operator!=(const TwoSpacePoint& lhs, const TwoSpacePoint& rhs) { return ! (lhs == rhs); } //********************************************************************** // Return the distance between two space points. double distance(const TwoSpacePoint spt1, const TwoSpacePoint spt2) { double dx = spt2.x() - spt1.x(); double dy = spt2.y() - spt1.y(); return sqrt( dx*dx + dy*dy ); } //**********************************************************************