diff --git a/DiscreteAngle.cpp b/DiscreteAngle.cpp index 2ce3a55..0a0e4cd 100644 --- a/DiscreteAngle.cpp +++ b/DiscreteAngle.cpp @@ -4,6 +4,7 @@ #include "DiscreteAngle.h" +#include "Angle.h" #include template <> AngleUsing::AngleUsing(unsigned char angle) { @@ -12,19 +13,16 @@ template <> AngleUsing::AngleUsing(unsigned char angle) { return; } - // clamp the float range to -1..1 - while (angle <= -180.0F) - angle += 360.0F; - while (angle > 180.0F) - angle -= 360.0F; + // normalize to (-180..180] + angle = Angle::Normalize(angle); - // map (-180..180] to 1..256 + // map (-180..180] to (0..255], which is equivaluent to 1..255 // This means that range value 0 is not used - this->value = (unsigned char)((angle + 180.0F) * 255.0F); + this->value = (unsigned char)((angle + 180.0F) / 360.0F * 255.0F); } template <> float AngleUsing::ToFloat() { - float f = ((float)this->value / 255.0F) - 180.0F; + float f = ((float)this->value / 255.0F) * 360.0F - 180.0F; return f; } @@ -34,18 +32,15 @@ template <> AngleUsing::AngleUsing(unsigned short angle) { return; } - // clamp the float range to -1..1 - while (angle <= -180.0F) - angle += 360.0F; - while (angle > 180.0F) - angle -= 360.0F; + // normalize to (-180..180] + angle = Angle::Normalize(angle); - // map (-180..180] to 1..256 + // map (-180..180] to (0..65535] which is equivalent to 1..65535 // This means that range value 0 is not used - this->value = (unsigned short)((angle + 180.0F) * 65535.0F); + this->value = (unsigned short)((angle + 180.0F) / 360.0F * 65535.0F); } template <> float AngleUsing::ToFloat() { - float f = ((float)this->value / 65535.0F) - 180.0F; + float f = ((float)this->value / 65535.0F) * 360.0F - 180.0F; return f; } \ No newline at end of file diff --git a/DiscreteAngle.h b/DiscreteAngle.h index f056e7d..0112584 100644 --- a/DiscreteAngle.h +++ b/DiscreteAngle.h @@ -3,13 +3,7 @@ #include "Range.h" -// A fixed point 16-bit signed angle between (-180..180] -// class DiscreteAngle16 : public Range16 { -// public: -// DiscreteAngle16(float angle); - -// inline DiscreteAngle16 &operator-() override; -// }; +// A fixed angle between (-180..180] template class AngleUsing : RangeUsing { public: