This commit is contained in:
Pascal Serrarens 2024-03-05 10:00:56 +01:00
parent 3714507059
commit 0468031b4b
2 changed files with 12 additions and 23 deletions

View File

@ -4,6 +4,7 @@
#include "DiscreteAngle.h" #include "DiscreteAngle.h"
#include "Angle.h"
#include <math.h> #include <math.h>
template <> AngleUsing<unsigned char>::AngleUsing(unsigned char angle) { template <> AngleUsing<unsigned char>::AngleUsing(unsigned char angle) {
@ -12,19 +13,16 @@ template <> AngleUsing<unsigned char>::AngleUsing(unsigned char angle) {
return; return;
} }
// clamp the float range to -1..1 // normalize to (-180..180]
while (angle <= -180.0F) angle = Angle::Normalize(angle);
angle += 360.0F;
while (angle > 180.0F)
angle -= 360.0F;
// 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 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<unsigned char>::ToFloat() { template <> float AngleUsing<unsigned char>::ToFloat() {
float f = ((float)this->value / 255.0F) - 180.0F; float f = ((float)this->value / 255.0F) * 360.0F - 180.0F;
return f; return f;
} }
@ -34,18 +32,15 @@ template <> AngleUsing<unsigned short>::AngleUsing(unsigned short angle) {
return; return;
} }
// clamp the float range to -1..1 // normalize to (-180..180]
while (angle <= -180.0F) angle = Angle::Normalize(angle);
angle += 360.0F;
while (angle > 180.0F)
angle -= 360.0F;
// 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 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<unsigned short>::ToFloat() { template <> float AngleUsing<unsigned short>::ToFloat() {
float f = ((float)this->value / 65535.0F) - 180.0F; float f = ((float)this->value / 65535.0F) * 360.0F - 180.0F;
return f; return f;
} }

View File

@ -3,13 +3,7 @@
#include "Range.h" #include "Range.h"
// A fixed point 16-bit signed angle between (-180..180] // A fixed angle between (-180..180]
// class DiscreteAngle16 : public Range16 {
// public:
// DiscreteAngle16(float angle);
// inline DiscreteAngle16 &operator-() override;
// };
template <typename T> class AngleUsing : RangeUsing<T> { template <typename T> class AngleUsing : RangeUsing<T> {
public: public: