Compare commits
2 Commits
8dd01ab105
...
6d6794456e
| Author | SHA1 | Date | |
|---|---|---|---|
|
6d6794456e
|
|||
|
a60847ad6f
|
21
src/agent.rs
21
src/agent.rs
@@ -67,20 +67,19 @@ impl Agent {
|
||||
let right = buf.get_buf(xr, yr);
|
||||
|
||||
// Rotate and move logic
|
||||
let mut rng = rand::thread_rng();
|
||||
let mut direction: f32 = 0.0;
|
||||
|
||||
if (center > left) && (center > right) {
|
||||
direction = 0.0;
|
||||
let direction = if (center > left) && (center > right) {
|
||||
0.0
|
||||
} else if (center < left) && (center < right) {
|
||||
direction = *[-1.0, 1.0]
|
||||
.choose(&mut rng)
|
||||
.expect("unable to choose random direction");
|
||||
*[-1.0, 1.0]
|
||||
.choose(&mut rand::thread_rng())
|
||||
.expect("unable to choose random direction")
|
||||
} else if left < right {
|
||||
direction = 1.0;
|
||||
1.0
|
||||
} else if right < left {
|
||||
direction = -1.0;
|
||||
}
|
||||
-1.0
|
||||
} else {
|
||||
0.0
|
||||
};
|
||||
|
||||
let delta_angle = rotation_angle * direction;
|
||||
|
||||
|
||||
14
src/util.rs
14
src/util.rs
@@ -1,6 +1,6 @@
|
||||
#[inline]
|
||||
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.
|
||||
@@ -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);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user