CAngle.h
//-----------------------------------------------------------------------------
// $Header: /tmp_mnt/asis/offline/ceres/cool/project/RCS/CAngle.h,v 2.2 1996/12/13 16:12:40 messer Exp $
//
// COOL Program Library
// Copyright (C) CERES collaboration, 1996
//
// CAngle keeps an angle within the range of [-pi, pi]
// units are radian, hence operators ++ and -- do not make
// a lot of sense...
//
//-----------------------------------------------------------------------------
#ifndef CANGLE_H
#define CANGLE_H
#include <math.h>
#include <float.h>
#include "cool.h"
class CAngle {
public:
CAngle(); // default constructors
CAngle(const CAngle& val); // copy constructor
CAngle(double); // construct new angle with given value
double degree(); // return angle in degrees [0,360]
operator double() const { return phi; }
CAngle operator= (double); // member functions for operators
CAngle operator+= (CAngle);
CAngle operator-= (CAngle);
CAngle operator*= (double);
CAngle operator/= (double);
int operator== (const CAngle&) const;
friend CAngle operator+ (CAngle, CAngle);
friend CAngle operator- (CAngle, CAngle);
friend CAngle operator* (CAngle, double);
friend CAngle operator/ (CAngle, double);
friend CAngle average(CAngle, CAngle);
private:
double phi;
};
//
// Constructors for angles
//
inline CAngle::CAngle()
{
phi = 0.; // default: set to 0
}
inline CAngle::CAngle(const CAngle& val)
{
phi = val.phi;
}
inline CAngle::CAngle(double val)
{
phi = val;
if (fabs(phi) > Pi) phi = atan2(sin(phi), cos(phi));
}
//
// Return angle in degrees [0, 360]
//
inline double CAngle::degree()
{
double val = phi;
if (val < 0) val = TwoPi + val;
return val*180./Pi;
}
//
// Here follow the member functions for the operators ==, !=, =, +=, -=, *=, /=
//
inline CAngle CAngle::operator= (double val)
{
phi = val;
if (fabs(phi) > Pi) phi = atan2(sin(phi), cos(phi));
return *this;
}
inline CAngle CAngle::operator+= (CAngle val)
{
phi += val.phi;
if (fabs(phi) > Pi) phi = atan2(sin(phi), cos(phi));
return *this;
}
inline CAngle CAngle::operator-= (CAngle val)
{
phi -= val.phi;
if (fabs(phi) > Pi) phi = atan2(sin(phi), cos(phi));
return *this;
}
inline CAngle CAngle::operator*= (double val)
{
phi *= val;
if (fabs(phi) > Pi) phi = atan2(sin(phi), cos(phi));
return *this;
}
inline CAngle CAngle::operator/= (double val)
{
phi /= val;
if (fabs(phi) > Pi) phi = atan2(sin(phi), cos(phi));
return *this;
}
inline int CAngle::operator== (const CAngle& a) const
{
return (fabs(CAngle(phi-a.phi)) < FLT_EPSILON);
}
//
// The friends for operator +, -, *, /
//
inline CAngle operator+ (CAngle angle, CAngle val)
{
double res = angle.phi + val.phi;
if (fabs(res) > Pi) res = atan2(sin(res), cos(res));
return res;
}
inline CAngle operator- (CAngle angle, CAngle val)
{
double res = angle.phi - val.phi;
if (fabs(res) > Pi) res = atan2(sin(res), cos(res));
return res;
}
inline CAngle operator* (CAngle angle, double val)
{
double res = angle.phi * val;
if (fabs(res) > Pi) res = atan2(sin(res), cos(res));
return res;
}
inline CAngle operator/ (CAngle angle, double val)
{
double res = angle.phi / val;
if (fabs(res) > Pi) res = atan2(sin(res), cos(res));
return res;
}
inline CAngle average(CAngle a, CAngle b)
{
return a+(b-a)/2.;
}
#endif /* CANGLE_H */