Skip to content

Commit 8321ee3

Browse files
committed
There's so much to fix :(
1 parent 3427e22 commit 8321ee3

5 files changed

Lines changed: 178 additions & 202 deletions

File tree

src/graph.rs

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ pub(crate) struct DijkstraGraph
2121
end: Option<usize>
2222
}
2323

24-
25-
2624
impl Default for DijkstraGraph
2725
{
2826
fn default() -> Self
@@ -123,8 +121,8 @@ impl DijkstraGraph
123121
}
124122
}
125123

126-
pub fn get(&self, id: usize) -> Option<DijkstraNode>
127-
{ return self.graph[id]; }
124+
pub fn get(&self, id: usize) -> &Option<DijkstraNode>
125+
{ return &self.graph[id]; }
128126

129127
pub fn start(&self) -> Option<usize>
130128
{ return self.start; }
@@ -171,9 +169,9 @@ impl DijkstraGraph
171169
for _ in 0..self.graph.len()
172170
{
173171
if self.graph[current_node].is_none() { return None; }
174-
if self.graph[current_node].unwrap().parent.is_none() { return None; }
172+
if self.graph[current_node].as_ref().unwrap().parent.is_none() { return None; }
175173

176-
current_node = self.graph[current_node].unwrap().parent.unwrap();
174+
current_node = self.graph[current_node].as_ref().unwrap().parent.unwrap();
177175

178176
path.push(current_node);
179177

@@ -183,33 +181,26 @@ impl DijkstraGraph
183181
return Some(path);
184182
}
185183

186-
pub fn points_iter(&self) -> Iter<Option<DijkstraNode>>
187-
{ return self.graph.iter(); }
184+
pub fn points(&self) -> &[Option<DijkstraNode>; 100]
185+
{ return &self.graph; }
188186

189-
pub fn lines_iter(&self) -> Iter<&(DijkstraNode, u16, DijkstraNode)>
187+
pub fn lines(&self) -> Vec<(usize, &DijkstraNode, u16, usize, &DijkstraNode)>
190188
{
191-
self.graph.iter()
192-
.filter_map(|node| *node)
193-
.map(|node| (node, node.edges))
194-
.map(|(node, edges)| edges.iter()
195-
.map(|edge| (edge.distance, edge.destination))
196-
.map(|(distance, id)| (distance, self.graph.get(id)))
197-
.filter(|(distance, option)| option.is_some())
198-
.map(|(distance, option)| (distance, option.unwrap()))
199-
.filter(|(distance, option)| option.is_some())
200-
.map(|(distance, option)| (distance, option.unwrap()))
201-
.map(|(distance, destination)| (node, distance, destination))
202-
.collect::<Vec<_>>()
203-
.iter()
204-
)
205-
.flatten()
206-
.collect::<Vec<_>>()
207-
.iter()
208-
}
189+
let mut lines_iter = vec![];
209190

210-
pub fn get_lines(&self)
211-
{
212-
todo!();
191+
for (from_id, point_option) in self.graph.iter().enumerate()
192+
{
193+
let Some(from_point) = point_option else { continue; };
194+
195+
for edge in &from_point.edges
196+
{
197+
let Some(Some(to_point)) = self.graph.get(edge.destination) else { continue; };
198+
199+
lines_iter.push((from_id, from_point, edge.distance, edge.destination, to_point));
200+
}
201+
}
202+
203+
return lines_iter;
213204
}
214205

215206
pub fn find_hovered_point(&mut self, mouse_x: f32, mouse_y: f32, radius: f32) -> Option<usize>
@@ -391,7 +382,8 @@ impl DijkstraGraph
391382
}
392383
}
393384

394-
struct DijkstraNode
385+
#[derive(Clone)]
386+
pub(crate) struct DijkstraNode
395387
{
396388
pub position: Position,
397389
parent: Option<usize>,
@@ -435,12 +427,12 @@ impl DijkstraNode
435427
pub fn set_visited(&mut self, visited: bool)
436428
{ self.visited = visited; }
437429

438-
pub fn edges(&self) -> Vec<Edge>
439-
{ return self.edges; }
430+
pub fn edges(&self) -> &Vec<Edge>
431+
{ return &self.edges; }
440432
}
441433

442-
// TODO: consider making these fields pub
443-
struct Position
434+
#[derive(Clone, Copy)]
435+
pub(crate) struct Position
444436
{
445437
pub x: f32,
446438
pub y: f32,
@@ -455,7 +447,8 @@ impl Position
455447
{ self.x = x; self.y = y; }
456448
}
457449

458-
struct Edge
450+
#[derive(Clone, Copy)]
451+
pub(crate) struct Edge
459452
{
460453
destination: usize,
461454
distance: u16,

src/main.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,16 @@ async fn main()
3939
// This is the id of the point the mouse is currently hovering over and mouse 1 is pressed
4040
let mut selected_point_id: Option<usize> = None;
4141

42-
let ui_width = 200_f32;
42+
let ui_width: f32 = 200.;
4343
let mut mode = Mode::Move;
4444

45-
let mut padding = 3_u8;
46-
let mut angle = 0.436_f32;
47-
let mut arrow_head_length = 20_f32;
48-
let mut radius = 13_f32;
49-
let mut line_length = 1_u16;
50-
let mut path_thickness = 2_f32;
51-
let mut base_point = 15_f32;
45+
let mut padding: u8 = 3;
46+
let mut angle: f32 = 0.436;
47+
let mut arrow_head_length: f32 = 20.;
48+
let mut radius: f32 = 13.;
49+
let mut line_length: u16 = 1;
50+
let mut path_thickness: f32 = 2.;
51+
let mut base_point: f32 = 15.;
5252

5353
let mut bg_color: [f32;3] = [0.25_f32, 0., 0.5];
5454
let mut path_color: [f32;3] = [0., 1., 0.];
@@ -92,7 +92,6 @@ async fn main()
9292
&mut arrow_head_length,
9393
&mut path_thickness,
9494
&mut base_point,
95-
&mut padding,
9695
&mut selected_point_id,
9796
&mut line_length,
9897
&mut path_color,
@@ -114,6 +113,9 @@ async fn main()
114113
&graph, &radius,
115114
&path_thickness,
116115
&padding,
116+
&angle,
117+
&base_point,
118+
&arrow_head_length,
117119
&hovered_point_id,
118120
&selected_point_id,
119121
&path_color,

src/tests/graph_tests.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,22 @@ fn generate_random_points_graph(amount_of_points: u8) -> DijkstraGraph
2020
#[test]
2121
fn add_some_points()
2222
{
23-
let mut graph = generate_random_points_graph(3);
23+
let graph = generate_random_points_graph(3);
2424
assert_eq!(graph.size(), 3);
2525
}
2626

2727
#[test]
2828
fn add_many_points()
2929
{
30-
let mut graph = generate_random_points_graph(50);
30+
let graph = generate_random_points_graph(50);
3131
assert_eq!(graph.size(), 50);
3232
}
3333

3434
#[test]
3535
fn max_amount_of_points()
3636
{
3737
// Creating graph and "adding" 1_000 points to it
38-
let mut graph = generate_random_points_graph(1_000);
38+
let graph = generate_random_points_graph(255);
3939
// The graph should still only have 100 points
4040
assert_eq!(graph.size(), 101);
4141
}

src/ui.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ pub(crate) fn paint_ui(
1515
arrow_head_length: &mut f32,
1616
path_thickness: &mut f32,
1717
base_point: &mut f32,
18-
padding: &mut u8,
1918
selected_point_id: &mut Option<usize>,
2019
line_length: &mut u16,
2120
path_color: &mut [f32;3],
@@ -57,10 +56,10 @@ pub(crate) fn paint_ui(
5756
ui.separator();
5857

5958
// The newlines are a hack to make all text fill up the same amount of vertical space
60-
match (&mode, selected_point_id)
59+
match (&mode, selected_point_id.is_some())
6160
{ (Mode::Move, _) => ui.label("• Left click on a point to select it.\n• Hold left click to move it around."),
62-
(Mode::Line, None) => ui.label("• Left click on a point to select it."),
63-
(Mode::Line, Some(_)) => ui.label("• Left click on another point to create a new line.\n• Right click on another point to delete an existing line."),
61+
(Mode::Line, false) => ui.label("• Left click on a point to select it."),
62+
(Mode::Line, true) => ui.label("• Left click on another point to create a new line.\n• Right click on another point to delete an existing line."),
6463
(Mode::Point, _) => ui.label("• Left click somewhere to create a point.\n• Right click on a point to delete it."),
6564
(Mode::Path, _) => ui.label("• Left click on a point to set the start.\n• Right click on a point to set the end.")
6665
};

0 commit comments

Comments
 (0)