scummvm/math/angle.cpp
2011-09-30 17:42:26 +02:00

163 lines
3.2 KiB
C++

/* Residual - A 3D game interpreter
*
* Residual is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
* $URL$
* $Id$
*/
#include "common/streamdebug.h"
#include "math/angle.h"
#include "math/utils.h"
namespace Math {
Angle::Angle(float degrees) :
_degrees(degrees) {
}
Angle::Angle(const Angle &a) :
_degrees(a._degrees) {
}
Angle &Angle::normalize(float low) {
_degrees = getDegrees(low);
return *this;
}
void Angle::setDegrees(float degrees) {
_degrees = degrees;
}
void Angle::setRadians(float radians) {
_degrees = radianToDegree(radians);
}
float Angle::getDegrees() const {
return _degrees;
}
float Angle::getRadians() const {
return degreeToRadian(getDegrees());
}
float Angle::getDegrees(float low) const {
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 {
float d = getDegrees(low);
return degreeToRadian(d);
}
float Angle::getCosine() const {
return cosf(getRadians());
}
float Angle::getSine() const {
return sinf(getRadians());
}
float Angle::getTangent() const {
return tanf(getRadians());
}
Angle &Angle::operator=(const Angle &a) {
_degrees = a._degrees;
return *this;
}
Angle &Angle::operator=(float degrees) {
setDegrees(degrees);
return *this;
}
Angle &Angle::operator+=(const Angle &a) {
setDegrees(_degrees + a._degrees);
return *this;
}
Angle &Angle::operator+=(float degrees) {
setDegrees(_degrees + degrees);
return *this;
}
Angle &Angle::operator-=(const Angle &a) {
setDegrees(_degrees - a._degrees);
return *this;
}
Angle &Angle::operator-=(float degrees) {
setDegrees(_degrees - degrees);
return *this;
}
Angle Angle::fromRadians(float radians) {
return Angle(radianToDegree(radians));
}
Angle Angle::arcCosine(float x) {
Angle a;
a.setRadians(acosf(x));
return a;
}
Angle Angle::arcSine(float x) {
Angle a;
a.setRadians(asinf(x));
return a;
}
Angle Angle::arcTangent(float x) {
Angle a;
a.setRadians(atanf(x));
return a;
}
Angle Angle::arcTangent2(float y, float x) {
Angle a;
a.setRadians(atan2f(y, x));
return a;
}
}
Common::Debug &operator<<(Common::Debug dbg, const Math::Angle &a) {
dbg.nospace() << "Angle(" << a.getDegrees(-180) << ")";
return dbg.space();
}