CRelAngle: Make member functions constexpr where applicable

Many of these member functions can be made constexpr, given their
manipulation of a simple internal float.
This commit is contained in:
Lioncash 2019-09-03 16:32:48 -04:00
parent ef23d36916
commit a88e50e296
1 changed files with 32 additions and 33 deletions

View File

@ -12,87 +12,86 @@ class CRelAngle {
float angle = 0.f; float angle = 0.f;
public: public:
static float MakeRelativeAngle(float angle) { static float MakeRelativeAngle(float angle) noexcept {
float ret = angle - std::trunc(angle / (2.f * M_PIF)) * (2.f * M_PIF); float ret = angle - std::trunc(angle / (2.f * M_PIF)) * (2.f * M_PIF);
if (ret < 0.f) if (ret < 0.f)
ret += 2.f * M_PIF; ret += 2.f * M_PIF;
return ret; return ret;
} }
constexpr CRelAngle() = default; constexpr CRelAngle() noexcept = default;
constexpr CRelAngle(float angle) : angle(angle) {} constexpr CRelAngle(float angle) noexcept : angle(angle) {}
CRelAngle& operator=(float ang) { constexpr CRelAngle(const CRelAngle&) noexcept = default;
constexpr CRelAngle& operator=(float ang) noexcept {
angle = ang; angle = ang;
return *this; return *this;
} }
CRelAngle& operator=(const CRelAngle& ang) { constexpr CRelAngle& operator=(const CRelAngle& ang) noexcept = default;
angle = ang.angle;
return *this;
}
float asDegrees() const { return radToDeg(angle); } constexpr float asDegrees() const noexcept { return radToDeg(angle); }
float asRadians() const { return angle; } constexpr float asRadians() const noexcept { return angle; }
float arcCosine() const { return std::acos(angle); } float arcCosine() const noexcept { return std::acos(angle); }
static CRelAngle FromDegrees(float angle) { static constexpr CRelAngle FromDegrees(float angle) noexcept {
CRelAngle ret; CRelAngle ret;
ret.angle = degToRad(angle); ret.angle = degToRad(angle);
return ret; return ret;
} }
operator float() const { return angle; } constexpr operator float() const noexcept { return angle; }
static CRelAngle FromRadians(float angle) { return CRelAngle(angle); } static constexpr CRelAngle FromRadians(float angle) noexcept { return CRelAngle(angle); }
bool operator<(const CRelAngle& other) const { return angle < other.angle; } constexpr bool operator<(const CRelAngle& other) const noexcept { return angle < other.angle; }
CRelAngle& operator+=(const CRelAngle& other) { constexpr CRelAngle& operator+=(const CRelAngle& other) noexcept {
angle = angle + other.angle; angle += other.angle;
return *this; return *this;
} }
CRelAngle& operator+=(float r) { constexpr CRelAngle& operator+=(float r) noexcept {
angle = angle + r; angle += r;
return *this; return *this;
} }
CRelAngle& operator-=(const CRelAngle& other) { constexpr CRelAngle& operator-=(const CRelAngle& other) noexcept {
angle = angle - other.angle; angle -= other.angle;
return *this; return *this;
} }
CRelAngle& operator-=(float r) { constexpr CRelAngle& operator-=(float r) noexcept {
angle = angle - r; angle -= r;
return *this; return *this;
} }
CRelAngle& operator*=(const CRelAngle& other) { constexpr CRelAngle& operator*=(const CRelAngle& other) noexcept {
angle = angle * other.angle; angle *= other.angle;
return *this; return *this;
} }
CRelAngle& operator*=(float r) { constexpr CRelAngle& operator*=(float r) noexcept {
angle = angle * r; angle *= r;
return *this; return *this;
} }
CRelAngle& operator/=(const CRelAngle& other) { constexpr CRelAngle& operator/=(const CRelAngle& other) noexcept {
angle = angle / other.angle; angle /= other.angle;
return *this; return *this;
} }
CRelAngle& operator/=(float r) { constexpr CRelAngle& operator/=(float r) noexcept {
angle = angle / r; angle /= r;
return *this; return *this;
} }
void makeRel() { angle = MakeRelativeAngle(angle); } void makeRel() noexcept { angle = MakeRelativeAngle(angle); }
CRelAngle asRel() const { return CRelAngle(MakeRelativeAngle(angle)); } CRelAngle asRel() const noexcept { return CRelAngle(MakeRelativeAngle(angle)); }
}; };
} // namespace zeus } // namespace zeus