use super::particle::{Particle, ParticleIndex};
#[derive(Clone, Debug, Default)]
pub struct Node {
position: Option<[f64; 2]>,
velocity: [f64; 2],
fixed: bool,
}
impl Node {
#[inline(always)]
pub fn position(mut self, x: f64, y: f64) -> Self {
self.position = Some([x, y]);
self
}
#[inline(always)]
pub fn fixed_position(mut self, x: f64, y: f64) -> Self {
self = self.position(x, y);
self.fixed = true;
self
}
pub(super) fn build_with_pos(
self,
index: ParticleIndex,
pos_fn: impl FnMut() -> [f64; 2],
) -> Particle {
let [x, y] = self.position.unwrap_or_else(pos_fn);
let [vx, vy] = self.velocity;
Particle {
x,
y,
vx,
vy,
index,
fx: if self.fixed { Some(x) } else { None },
fy: if self.fixed { Some(y) } else { None },
}
}
}
impl From<[f64; 2]> for Node {
fn from(position: [f64; 2]) -> Self {
Self {
position: Some(position),
velocity: Default::default(),
fixed: false,
}
}
}