use comfy::*; #[derive(Debug)] pub struct Grid { pub nodes: Vec, pub connections: Vec<(usize, usize)> } impl Default for Grid { fn default() -> Self { Grid::load() //Just for testing purposes } } impl Grid { pub fn new(nodes: Vec, connections: Vec<(usize, usize)>) -> Self { Grid { nodes, connections } } fn load() -> Self { let mut grid = Self { nodes: vec![ vec2(10.0, 0.0), vec2(0.0, 0.0), vec2(0.0, 10.0), vec2(-10.0, 0.0), vec2(10.0, 10.0), ], connections: vec![(0, 1), (1, 2), (1, 3), (0, 4), (0, 5), (5, 1), (6, 7)] }; grid.sanitize(); grid } fn sanitize(&mut self) { let mut len = self.nodes.len(); let connections = self .connections .iter() .filter(|(conn_i1, conn_i2)| { if conn_i1 >= &mut len || conn_i2 >= &mut len { error!("Connection in grid not possible {:?}", (conn_i1, conn_i2)); false } else { true } }) .map(|(conn_i1, conn_i2)| (*conn_i1, *conn_i2)) .collect(); self.connections = connections; } pub fn draw(&self) { //Draw Grid for node in &self.nodes { draw_circle(*node, 0.25, BLUE, 0); } for (conn_i1, conn_i2) in &self.connections { let node_1 = self.nodes.get(*conn_i1); let node_2 = self.nodes.get(*conn_i2); if node_1.is_none() || node_2.is_none() { error!("Connection in grid not available {:?}", (conn_i1, conn_i2)); continue; } draw_line(*node_1.unwrap(), *node_2.unwrap(), 0.1, BLUE, 0); } } }