MATH: Normalize the angle only if/when requested.

This commit is contained in:
Giulio Camuffo 2011-09-30 17:42:26 +02:00
parent 846afc03a8
commit ba10e07422
2 changed files with 34 additions and 67 deletions

View File

@ -29,32 +29,47 @@
namespace Math {
Angle::Angle(float degrees, float low) :
_degrees(degrees), _rangeLow(low) {
crop();
Angle::Angle(float degrees) :
_degrees(degrees) {
}
Angle::Angle(const Angle &a) :
_degrees(a._degrees), _rangeLow(a._rangeLow) {
_degrees(a._degrees) {
}
Angle &Angle::normalize(float low) {
_degrees = getDegrees(low);
return *this;
}
void Angle::setDegrees(float degrees) {
_degrees = degrees;
crop();
}
void Angle::setRadians(float radians) {
_degrees = radianToDegree(radians);
crop();
}
float Angle::getDegrees() const {
return _degrees;
}
float Angle::getRadians() const {
return degreeToRadian(_degrees);
return degreeToRadian(getDegrees());
}
float Angle::getDegrees(float low) const {
return _degrees - _rangeLow + low;
float degrees = _degrees;
if (degrees >= low + 360.f) {
float x = floor((degrees - low) / 360.f);
degrees -= 360.f * x;
} else if (degrees < low) {
float x = floor((degrees - low) / 360.f);
degrees -= 360.f * x;
}
return degrees;
}
float Angle::getRadians(float low) const {
@ -76,7 +91,6 @@ float Angle::getTangent() const {
Angle &Angle::operator=(const Angle &a) {
_degrees = a._degrees;
_rangeLow = a._rangeLow;
return *this;
}
@ -111,8 +125,8 @@ Angle &Angle::operator-=(float degrees) {
return *this;
}
Angle Angle::fromRadians(float radians, float low) {
return Angle(radianToDegree(radians), low);
Angle Angle::fromRadians(float radians) {
return Angle(radianToDegree(radians));
}
Angle Angle::arcCosine(float x) {
@ -139,26 +153,10 @@ Angle Angle::arcTangent2(float y, float x) {
return a;
}
void Angle::setRange(float low) {
_rangeLow = low;
crop();
}
void Angle::crop() {
if (_degrees >= _rangeLow + 360.f) {
int x = (int)(_degrees - _rangeLow) / 360.f;
_degrees -= 360.f * x;
}
if (_degrees < _rangeLow) {
int x = (int)(_degrees + _rangeLow) / 360.f;
_degrees -= 360.f * x;
}
}
}
Common::Debug &operator<<(Common::Debug dbg, const Math::Angle &a) {
dbg.nospace() << "Angle(" << a.getDegrees() << ")";
dbg.nospace() << "Angle(" << a.getDegrees(-180) << ")";
return dbg.space();
}

View File

@ -39,9 +39,8 @@ public:
* Construct an angle object.
*
* \param degrees The angle, in degrees. Default is 0.
* \param low The lower bound of the range, in degrees. Default is -180.
*/
Angle(float degrees = 0, float low = -180);
Angle(float degrees = 0);
/**
* Construct and angle object, copying an already existing one.
*
@ -49,17 +48,17 @@ public:
*/
Angle(const Angle &a);
void setDegrees(float degrees);
void setRadians(float radians);
/**
* Sets the range of the angle.
* Normalize the angle in a [x; x + 360] range and return the object.
*
* \param low The lower bound of the range, in degrees.
*/
void setRange(float low);
Angle &normalize(float low);
inline float getDegrees() const { return _degrees; }
void setDegrees(float degrees);
void setRadians(float radians);
float getDegrees() const;
float getRadians() const;
/**
@ -90,9 +89,8 @@ public:
* Build an angle object.
*
* \param radians The angle, in radians.
* \param low The lower bound of the range, in degrees. Default is -180.
*/
static Angle fromRadians(float radians, float low = -180);
static Angle fromRadians(float radians);
static Angle arcCosine(float x);
static Angle arcSine(float x);
@ -100,10 +98,7 @@ public:
static Angle arcTangent2(float y, float x);
private:
void crop();
float _degrees;
float _rangeLow;
};
@ -114,23 +109,10 @@ inline Angle operator-(const Angle &a) {
inline Angle operator+(const Angle &a1, const Angle &a2) {
return Angle(a1.getDegrees() + a2.getDegrees());
}
inline Angle operator+(const Angle &a1, float degrees) {
return Angle(a1.getDegrees() + degrees);
}
inline Angle operator+(float degrees, const Angle &a2) {
return Angle(degrees + a2.getDegrees());
}
inline Angle operator-(const Angle &a1, const Angle &a2) {
return Angle(a1.getDegrees() - a2.getDegrees());
}
inline Angle operator-(const Angle &a1, float degrees) {
return Angle(a1.getDegrees() - degrees);
}
inline Angle operator-(float degrees, const Angle &a2) {
return Angle(degrees - a2.getDegrees());
}
inline Angle operator*(const Angle &a1, float f) {
return Angle(a1.getDegrees() * f);
@ -160,23 +142,10 @@ inline bool operator!=(const Angle &a1, const Angle &a2) {
inline bool operator<(const Angle &a1, const Angle &a2) {
return a1.getDegrees() < a2.getDegrees();
}
inline bool operator<(const Angle &a1, float degrees) {
return a1.getDegrees() < degrees;
}
inline bool operator<(float degrees, const Angle &a2) {
return degrees < a2.getDegrees();
}
inline bool operator>(const Angle &a1, const Angle &a2) {
return a1.getDegrees() > a2.getDegrees();
}
inline bool operator>(const Angle &a1, float degrees) {
return a1.getDegrees() > degrees;
}
inline bool operator>(float degrees, const Angle &a2) {
return degrees > a2.getDegrees();
}
}