miscmath/lib.rs
1//! # Miscmath
2//!
3//! `miscmath` is a collection of general math functions
4//! and linear algebra structures like vectors and matrices.
5
6pub mod linear;
7pub mod prelude;
8
9use rand::{
10 distributions::uniform::{SampleRange, SampleUniform},
11 thread_rng, Rng,
12};
13use std::ops::Range;
14
15///
16///
17/// # Examples
18///
19/// ```
20///
21///
22///
23/// ```
24///
25pub fn constrain(val: &mut f32, rng: &Range<f32>) {
26 *val = val.clamp(rng.start, rng.end);
27}
28
29/// Calculates and returns the factorial of the integer entered
30///
31/// # Examples
32///
33/// ```
34/// use miscmath::prelude::*;
35///
36/// let a = factorial(5);
37///
38/// assert_eq!( a, 120 );
39/// ```
40///
41pub fn factorial(fac_num: usize) -> usize {
42 if fac_num > 1 {
43 fac_num * factorial(fac_num - 1)
44 } else {
45 1
46 }
47}
48
49/// Prints out the fibonacci sequence up to the number of terms entered
50///
51/// # Examples
52///
53/// ```
54///
55///
56///
57/// ```
58///
59pub fn fibonacci() {}
60
61/// Takes a value within a given range and converts it to an equivalent value in another range
62///
63/// # Examples
64///
65/// ```
66/// use miscmath::prelude::*;
67///
68/// let a = map(0.25, 0.0..1.0, 0.0..100.0 );
69///
70/// assert!( ( a - 25.0 ) < 0.000000001 );
71/// ```
72///
73pub fn map(input: f32, in_rng: Range<f32>, out_rng: Range<f32>) -> f32 {
74 out_rng.start
75 + ((out_rng.end - out_rng.start) / (in_rng.end - in_rng.start)) * (input - in_rng.start)
76}
77
78///
79pub const DEFAULT_NOISE_SEED: usize = 0;
80
81/// Generates a random number in the range provided
82///
83/// # Examples
84///
85/// ```
86/// use miscmath::prelude::*;
87///
88/// let a = random( 0..10 );
89/// let b = random( 0.0..10.0 );
90///
91/// assert!( ( a < 10 ) && ( a >= 0 ) );
92/// assert!( (b < 10.0 ) && ( b > 0.0 ) );
93/// ```
94///
95pub fn random<T: SampleUniform>(rng: Range<T>) -> T
96where
97 Range<T>: SampleRange<T>,
98{
99 thread_rng().gen_range(rng)
100}
101
102/// Perlin Noise struct
103///
104/// # Examples
105///
106/// ```
107/// use miscmath::prelude::*;
108///
109/// let perlin = Perlin::new( DEFAULT_NOISE_SEED );
110/// ```
111///
112#[derive(Debug)]
113pub struct Perlin {
114 ///
115 pub seed: usize,
116 ///
117 permutation_table: [i16; 256],
118}
119
120impl Perlin {
121 /// Initializes and returns a new Perlin object
122 ///
123 /// # Examples
124 ///
125 /// ```
126 /// use miscmath::prelude::*;
127 ///
128 /// let perlin = Perlin::new( DEFAULT_NOISE_SEED );
129 ///
130 /// ```
131 ///
132 pub fn new(seed: usize) -> Perlin {
133 let mut perlin = Perlin {
134 seed,
135 permutation_table: [-1; 256],
136 };
137
138 /* Sets each element to a number from 0 to 256, with no repeating values */
139 for i in 0..256 {
140 /* Finds a element that hasn't been set yet to assign to i */
141 Perlin::permutation_gen(&mut perlin.permutation_table, i);
142 }
143
144 /* todo: implement the rest of the perlin noise algorithm, using the randomly generated permutation table to generate a array of interpolated
145 pseudo-random values */
146
147 perlin
148 }
149
150 // Recursive: Takes i and adds it to the permutation table at a random index, as long as that element hasn't been set yet (is still -1).
151 // If the element picked has been set already this function is called recursively until it picks a non-set element.
152 fn permutation_gen(table: &mut [i16; 256], i: i16) {
153 /* Pick a random int between 0 and 255 */
154 let index = random(0..256);
155
156 /* Base case: If a number has not been assigned to that element yet, assign it i */
157 if table[index] == -1 {
158 table[index] = i;
159 } else if i <= 256 {
160 /* Recursive case: If a number has been assigned to that element, try again */
161 Perlin::permutation_gen(table, i);
162 }
163 }
164
165 /// Gets a value between 0 and 1 using perlin Noise
166 ///
167 /// # Examples
168 ///
169 /// ```
170 ///
171 /// ```
172 ///
173 pub fn get<T>(_time: T) {
174 todo!()
175 }
176}