Implement radians conversions for ByteAngle (#149)

Adds `from_radians` function and `to_radians` method for `ByteAngle`.
This commit is contained in:
UStuej 2022-11-02 00:00:38 -05:00 committed by GitHub
parent ff136c2a4a
commit bb43856c9e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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 {