Compare commits

...

2 Commits

Author SHA1 Message Date
6d6794456e improve wrap function 2025-03-27 23:50:01 -04:00
a60847ad6f test + cleanup agent direction code 2025-03-27 23:47:46 -04:00
2 changed files with 23 additions and 12 deletions

View File

@@ -67,20 +67,19 @@ impl Agent {
let right = buf.get_buf(xr, yr); let right = buf.get_buf(xr, yr);
// Rotate and move logic // Rotate and move logic
let mut rng = rand::thread_rng(); let direction = if (center > left) && (center > right) {
let mut direction: f32 = 0.0; 0.0
if (center > left) && (center > right) {
direction = 0.0;
} else if (center < left) && (center < right) { } else if (center < left) && (center < right) {
direction = *[-1.0, 1.0] *[-1.0, 1.0]
.choose(&mut rng) .choose(&mut rand::thread_rng())
.expect("unable to choose random direction"); .expect("unable to choose random direction")
} else if left < right { } else if left < right {
direction = 1.0; 1.0
} else if right < left { } else if right < left {
direction = -1.0; -1.0
} } else {
0.0
};
let delta_angle = rotation_angle * direction; let delta_angle = rotation_angle * direction;

View File

@@ -1,6 +1,6 @@
#[inline] #[inline]
pub fn wrap(x: f32, max: f32) -> f32 { pub fn wrap(x: f32, max: f32) -> f32 {
x - max * ((x > max) as i32 as f32 - (x < 0.0_f32) as i32 as f32) x - max * ((x > max) as i32 - x.is_sign_negative() as i32) as f32
} }
/// Truncate x and y and return a corresponding index into the data slice. /// Truncate x and y and return a corresponding index into the data slice.
@@ -11,3 +11,15 @@ pub const fn index(width: usize, height: usize, x: f32, y: f32) -> usize {
let j = (y + height as f32) as usize & (height - 1); let j = (y + height as f32) as usize & (height - 1);
j * width + i j * width + i
} }
#[cfg(test)]
mod test {
use super::*;
#[test]
fn wrap_test() {
assert_eq!(wrap(1.1, 1.0), 0.100000024); // floating point weirdness
assert_eq!(wrap(0.5, 1.0), 0.5);
assert_eq!(wrap(-1.0, 2.0), 1.0);
}
}