artyversary/crates/utils/src/lib.rs

57 lines
1.3 KiB
Rust
Raw Normal View History

2021-08-15 19:34:50 +00:00
pub mod color;
2021-09-18 11:49:13 +00:00
pub mod curves;
2021-08-27 22:20:17 +00:00
pub mod drawing;
2021-08-15 19:32:47 +00:00
pub mod record;
2021-08-24 18:41:36 +00:00
pub mod sequences;
2021-08-17 14:25:34 +00:00
use nannou::prelude::*;
/// Maps the sine of v to (out_min, out_max)
pub fn map_sin(v: f32, out_min: f32, out_max: f32) -> f32 {
map_range(v.sin(), -1.0, 1.0, out_min, out_max)
}
2021-08-18 21:31:00 +00:00
/// Maps the cosine of v to (out_min, out_max)
pub fn map_cos(v: f32, out_min: f32, out_max: f32) -> f32 {
map_range(v.cos(), -1.0, 1.0, out_min, out_max)
}
2021-08-17 14:25:34 +00:00
pub trait Vec2Extension {
fn atan2(self) -> f32;
2021-08-22 14:40:06 +00:00
fn yy(self) -> Self;
fn yx(self) -> Self;
fn xx(self) -> Self;
2021-08-17 14:25:34 +00:00
}
impl Vec2Extension for Vec2 {
fn atan2(self) -> f32 {
self.x.atan2(self.y)
}
2021-08-22 14:40:06 +00:00
fn yy(self) -> Self {
vec2(self.y, self.y)
}
fn yx(self) -> Self {
vec2(self.y, self.x)
}
fn xx(self) -> Self {
vec2(self.x, self.x)
}
2021-08-17 14:25:34 +00:00
}
2021-08-24 18:41:36 +00:00
pub trait Tup2Extension {
fn to_vec2(self) -> Vec2;
}
impl Tup2Extension for (f32, f32) {
fn to_vec2(self) -> Vec2 {
self.into()
}
}
2021-09-03 16:59:19 +00:00
pub fn vec2_range(min: f32, max: f32) -> Vec2 {
vec2(random_range(min, max), random_range(min, max))
}
2021-09-08 16:36:37 +00:00
pub fn ivec2_range(min: i32, max: i32) -> IVec2 {
ivec2(random_range(min, max), random_range(min, max))
}
2021-09-18 11:49:13 +00:00
/// returns a random vector in the unit circle
pub fn vec2_circ() -> Vec2 {
random_range(0., TAU).sin_cos().into()
}