Galactica/crates/util/src/lib.rs

41 lines
927 B
Rust

#![warn(missing_docs)]
//! Various utilities
use anyhow::bail;
use nalgebra::Vector2;
pub mod constants;
pub mod timing;
/// Convert an angle in degrees to radians
pub fn to_radians(degrees: f32) -> f32 {
return (degrees / 360.0) * std::f32::consts::TAU;
}
/// Convert an angle in radians to degrees
pub fn to_degrees(radians: f32) -> f32 {
return (radians / std::f32::consts::TAU) * 360.0;
}
/// Compute the clockwise angle between two vectors
/// Returns a value in [-pi, pi]
pub fn clockwise_angle(a: &Vector2<f32>, b: &Vector2<f32>) -> f32 {
(a.x * b.y - b.x * a.y).atan2(a.dot(&b))
}
/// Convert a rhai error to an anyhow error.
/// We can't do this directly, since anyhow requires send + sync,
/// and we don't need send+sync on rhai.
pub fn rhai_error_to_anyhow<T, E>(r: Result<T, E>) -> anyhow::Result<T>
where
E: ToString,
{
match r {
Ok(x) => Ok(x),
Err(e) => {
bail!("{}", e.to_string())
}
}
}