pub mod linear;
pub mod prelude;
use rand::{
distributions::uniform::{SampleRange, SampleUniform},
thread_rng, Rng,
};
use std::ops::Range;
pub fn constrain(val: &mut f32, rng: &Range<f32>) {
*val = val.clamp(rng.start, rng.end);
}
pub fn factorial(fac_num: usize) -> usize {
if fac_num > 1 {
fac_num * factorial(fac_num - 1)
} else {
1
}
}
pub fn fibonacci() {}
pub fn map(input: f32, in_rng: Range<f32>, out_rng: Range<f32>) -> f32 {
out_rng.start
+ ((out_rng.end - out_rng.start) / (in_rng.end - in_rng.start)) * (input - in_rng.start)
}
pub const DEFAULT_NOISE_SEED: usize = 0;
pub fn random<T: SampleUniform>(rng: Range<T>) -> T
where
Range<T>: SampleRange<T>,
{
thread_rng().gen_range(rng)
}
#[derive(Debug)]
pub struct Perlin {
pub seed: usize,
permutation_table: [i16; 256],
}
impl Perlin {
pub fn new(seed: usize) -> Perlin {
let mut perlin = Perlin {
seed,
permutation_table: [-1; 256],
};
for i in 0..256 {
Perlin::permutation_gen(&mut perlin.permutation_table, i);
}
perlin
}
fn permutation_gen(table: &mut [i16; 256], i: i16) {
let index = random(0..256);
if table[index] == -1 {
table[index] = i;
} else if i <= 256 {
Perlin::permutation_gen(table, i);
}
}
pub fn get<T>(_time: T) {
todo!()
}
}