Back to index

PHAngle.h

 
//----------------------------------------------------------------------------- 
//  $Header: 
// 
//  Purpose : A angle class (-pi:pi) 
// 
//  PHAngle keeps an angle within the range of [-pi, pi] 
//  units are radian, hence operators ++ and -- do not make 
//  a lot of sense... 
// 
//  Adapted by: Federica Messer  
//----------------------------------------------------------------------------- 
#ifndef PHANGLE_H  
#define PHANGLE_H 
 
#include <math.h> 
#include <float.h> 
#include <iostream.h> 
#include "phool.h" 
 
class PHAngle { 
public: 
   PHAngle();                                 // default constructors 
   PHAngle(const PHAngle& val);                // copy constructor 
   PHAngle(double);                           // construct new angle with given value 
    
   double degree();                          // return angle in degrees [0,360] 
    
   operator double() const { return phi; } 
   
   PHAngle operator= (double);                // member functions for operators 
   PHAngle operator+= (PHAngle); 
   PHAngle operator-= (PHAngle); 
   PHAngle operator*= (double); 
   PHAngle operator/= (double); 
   int    operator== (const PHAngle&) const; 
    
   friend PHAngle operator+ (PHAngle, PHAngle); 
   friend PHAngle operator- (PHAngle, PHAngle); 
   friend PHAngle operator* (PHAngle, double); 
   friend PHAngle operator/ (PHAngle, double); 
    
   friend PHAngle average(PHAngle, PHAngle); 
   
private:  
   double phi; 
}; 
 
 
// 
// Constructors for angles 
// 
 
inline PHAngle::PHAngle()  
{ 
   phi = 0.;   // default: set to 0 
} 
 
inline PHAngle::PHAngle(const PHAngle& val)  
{ 
   phi = val.phi; 
} 
 
inline PHAngle::PHAngle(double val)  
{ 
   phi = val; 
   if (fabs(phi) > Pi) phi = atan2(sin(phi), cos(phi)); 
} 
 
 
// 
//  Return angle in degrees [0, 360] 
// 
 
inline double PHAngle::degree() 
{ 
   double val = phi; 
   if (val < 0) val = TwoPi + val; 
   return val*180./Pi; 
} 
 
 
// 
// Here follow the member functions for the operators ==, !=, =, +=, -=, *=, /= 
// 
 
inline PHAngle PHAngle::operator= (double val)  
{ 
   phi = val; 
   if (fabs(phi) > Pi) phi = atan2(sin(phi), cos(phi)); 
   return *this; 
} 
 
inline PHAngle PHAngle::operator+= (PHAngle val)  
{ 
   phi += val.phi; 
   if (fabs(phi) > Pi) phi = atan2(sin(phi), cos(phi)); 
   return *this; 
} 
 
inline PHAngle PHAngle::operator-= (PHAngle val)  
{ 
   phi -= val.phi; 
   if (fabs(phi) > Pi) phi = atan2(sin(phi), cos(phi)); 
   return *this; 
} 
 
inline PHAngle PHAngle::operator*= (double val)  
{ 
   phi *= val; 
   if (fabs(phi) > Pi) phi = atan2(sin(phi), cos(phi)); 
   return *this; 
} 
 
inline PHAngle PHAngle::operator/= (double val)  
{ 
   phi /= val; 
   if (fabs(phi) > Pi) phi = atan2(sin(phi), cos(phi)); 
   return *this; 
} 
 
inline int PHAngle::operator== (const PHAngle& a) const 
{ 
   return (fabs(PHAngle(phi-a.phi)) < FLT_EPSILON); 
} 
 
// 
// The friends for operator +, -, *, / 
// 
 
inline PHAngle operator+ (PHAngle angle, PHAngle val)  
{ 
   double res = angle.phi + val.phi; 
   if (fabs(res) > Pi) res = atan2(sin(res), cos(res)); 
   return res; 
} 
 
inline PHAngle operator- (PHAngle angle, PHAngle val)  
{ 
   double res = angle.phi - val.phi; 
   if (fabs(res) > Pi) res = atan2(sin(res), cos(res)); 
   return res; 
} 
 
inline PHAngle operator* (PHAngle angle, double val)  
{ 
   double res = angle.phi * val; 
   if (fabs(res) > Pi) res = atan2(sin(res), cos(res)); 
   return res; 
} 
 
inline PHAngle operator/ (PHAngle angle, double val)  
{ 
   double res = angle.phi / val; 
   if (fabs(res) > Pi) res = atan2(sin(res), cos(res)); 
   return res; 
} 
 
inline PHAngle average(PHAngle a, PHAngle b) 
{ 
   return a+(b-a)/2.; 
} 
 
#endif /* PHANGLE_H */ 
 
 

Back to index