impl Display for Agent
This commit is contained in:
parent
7f5fef2590
commit
a927cfe6fa
32
src/agent.rs
32
src/agent.rs
@ -5,6 +5,8 @@ use crate::{
|
|||||||
|
|
||||||
use rand::{seq::SliceRandom, Rng};
|
use rand::{seq::SliceRandom, Rng};
|
||||||
use std::f32::consts::TAU;
|
use std::f32::consts::TAU;
|
||||||
|
use fastapprox::faster::{cos, sin};
|
||||||
|
use std::fmt::{Display, Formatter};
|
||||||
|
|
||||||
// A single Physarum agent. The x and y positions are continuous, hence we use floating point numbers instead of integers.
|
// A single Physarum agent. The x and y positions are continuous, hence we use floating point numbers instead of integers.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@ -16,6 +18,20 @@ pub struct Agent {
|
|||||||
pub i: usize,
|
pub i: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Display for Agent {
|
||||||
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"{{\n (x,y): ({},{})\n angle: {}\n population id: {}, i: {} }}",
|
||||||
|
self.x,
|
||||||
|
self.y,
|
||||||
|
self.angle,
|
||||||
|
self.population_id,
|
||||||
|
self.i,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Agent {
|
impl Agent {
|
||||||
// Construct a new agent with random parameters.
|
// Construct a new agent with random parameters.
|
||||||
pub fn new<R: Rng + ?Sized>(width: usize, height: usize, id: usize, rng: &mut R, i: usize) -> Self {
|
pub fn new<R: Rng + ?Sized>(width: usize, height: usize, id: usize, rng: &mut R, i: usize) -> Self {
|
||||||
@ -40,16 +56,16 @@ impl Agent {
|
|||||||
..
|
..
|
||||||
} = grid.config;
|
} = grid.config;
|
||||||
|
|
||||||
let xc = self.x + fastapprox::faster::cos(self.angle) * sensor_distance;
|
let xc = self.x + cos(self.angle) * sensor_distance;
|
||||||
let yc = self.y + fastapprox::faster::sin(self.angle) * sensor_distance;
|
let yc = self.y + sin(self.angle) * sensor_distance;
|
||||||
|
|
||||||
let agent_add_sens = self.angle + sensor_angle;
|
let agent_add_sens = self.angle + sensor_angle;
|
||||||
let agent_sub_sens = self.angle - sensor_angle;
|
let agent_sub_sens = self.angle - sensor_angle;
|
||||||
|
|
||||||
let xl = self.x + fastapprox::faster::cos(agent_sub_sens) * sensor_distance;
|
let xl = self.x + cos(agent_sub_sens) * sensor_distance;
|
||||||
let yl = self.y + fastapprox::faster::sin(agent_sub_sens) * sensor_distance;
|
let yl = self.y + sin(agent_sub_sens) * sensor_distance;
|
||||||
let xr = self.x + fastapprox::faster::cos(agent_add_sens) * sensor_distance;
|
let xr = self.x + cos(agent_add_sens) * sensor_distance;
|
||||||
let yr = self.y + fastapprox::faster::sin(agent_add_sens) * sensor_distance;
|
let yr = self.y + sin(agent_add_sens) * sensor_distance;
|
||||||
|
|
||||||
// We sense from the buffer because this is where we previously combined data from all the grid.
|
// We sense from the buffer because this is where we previously combined data from all the grid.
|
||||||
let center = grid.get_buf(xc, yc);
|
let center = grid.get_buf(xc, yc);
|
||||||
@ -74,11 +90,11 @@ impl Agent {
|
|||||||
|
|
||||||
self.angle = wrap(self.angle + delta_angle, TAU);
|
self.angle = wrap(self.angle + delta_angle, TAU);
|
||||||
self.x = wrap(
|
self.x = wrap(
|
||||||
self.x + step_distance * fastapprox::faster::cos(self.angle),
|
self.x + step_distance * cos(self.angle),
|
||||||
width as f32,
|
width as f32,
|
||||||
);
|
);
|
||||||
self.y = wrap(
|
self.y = wrap(
|
||||||
self.y + step_distance * fastapprox::faster::sin(self.angle),
|
self.y + step_distance * sin(self.angle),
|
||||||
height as f32,
|
height as f32,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user