From bb43856c9e0ead5679fe4a95184eb03c4039f52a Mon Sep 17 00:00:00 2001 From: UStuej <45504535+UStuej@users.noreply.github.com> Date: Wed, 2 Nov 2022 00:00:38 -0500 Subject: [PATCH] Implement radians conversions for ByteAngle (#149) Adds `from_radians` function and `to_radians` method for `ByteAngle`. --- src/protocol/byte_angle.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/protocol/byte_angle.rs b/src/protocol/byte_angle.rs index 41a7d12..26df73e 100644 --- a/src/protocol/byte_angle.rs +++ b/src/protocol/byte_angle.rs @@ -1,3 +1,4 @@ +use std::f32::consts::TAU; use std::io::Write; use crate::protocol::{Decode, Encode}; @@ -11,9 +12,17 @@ impl ByteAngle { ByteAngle((f.rem_euclid(360.0) / 360.0 * 256.0).round() as u8) } + pub fn from_radians(f: f32) -> ByteAngle { + ByteAngle((f.rem_euclid(TAU) / TAU * 256.0).round() as u8) + } + pub fn to_degrees(self) -> f32 { self.0 as f32 / 256.0 * 360.0 } + + pub fn to_radians(self) -> f32 { + self.0 as f32 / 256.0 * TAU + } } impl Encode for ByteAngle {