Skip to content

Commit 18dc280

Browse files
committed
Reworked most of utils module with new graph struct
1 parent bb22539 commit 18dc280

4 files changed

Lines changed: 357 additions & 270 deletions

File tree

src/graph.rs

Lines changed: 91 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::slice::Iter;
2+
13
use crate::utils;
24

35
// TODO: add iterator over points
@@ -19,26 +21,7 @@ pub(crate) struct DijkstraGraph
1921
end: Option<usize>
2022
}
2123

22-
struct DijkstraNode
23-
{
24-
position: Position,
25-
parent: Option<usize>,
26-
distance: Option<u16>,
27-
visited: bool,
28-
edges: Vec<Edge>,
29-
}
3024

31-
struct Edge
32-
{
33-
destination: usize,
34-
distance: u16,
35-
}
36-
37-
struct Position
38-
{
39-
x: f32,
40-
y: f32,
41-
}
4225

4326
impl Default for DijkstraGraph
4427
{
@@ -121,15 +104,15 @@ impl DijkstraGraph
121104
}
122105

123106
/// Adds a line; if it already exists, the length gets updated
124-
fn add_line(&mut self, from: usize, to: usize, distance: u16)
107+
pub fn add_line(&mut self, from: usize, to: usize, distance: u16)
125108
{
126109
if from > 100 || to > 100 { return; }
127110

128111
if let Some(node) = &mut self.graph[from]
129112
{ node.edges.push(Edge { destination: to, distance }); }
130113
}
131114

132-
fn remove_line(&mut self, from: usize, to: usize)
115+
pub fn remove_line(&mut self, from: usize, to: usize)
133116
{
134117
if from > 100 || to > 100 { return; }
135118

@@ -140,25 +123,29 @@ impl DijkstraGraph
140123
}
141124
}
142125

143-
fn update_node_position(&mut self, id: usize, x: f32, y: f32)
144-
{
145-
if id > 100 { return; }
146-
if let Some(node) = &mut self.graph[id] { node.update_position(x, y); }
147-
}
126+
pub fn get(&self, id: usize) -> Option<DijkstraNode>
127+
{ return self.graph[id]; }
148128

149-
fn update_line_distance(&mut self, from: usize, to: usize, distance: u16)
150-
{
151-
if from > 100 || to > 100 { return; }
129+
pub fn get_start(&self) -> Option<usize>
130+
{ return self.start; }
152131

153-
if let Some(node) = &mut self.graph[from]
154-
{
155-
if let Some(edge) = node.edges.get(to)
156-
{ edge.update_distance(distance); }
157-
}
158-
}
132+
pub fn set_start(&mut self, start: Option<usize>)
133+
{ self.start = start; }
134+
135+
pub fn clear_start(&mut self)
136+
{ self.start = None; }
137+
138+
pub fn get_end(&self) -> Option<usize>
139+
{ return self.end; }
140+
141+
pub fn set_end(&mut self, end: Option<usize>)
142+
{ self.end = end; }
143+
144+
pub fn clear_end(&mut self)
145+
{ self.end = None; }
159146

160147
/// Returns true if the shortest path has been found
161-
fn find_shortest_path(&mut self, start: usize, end: usize) -> bool
148+
pub fn find_shortest_path(&mut self, start: usize, end: usize) -> bool
162149
{
163150
if start > 100 || end > 100 { return false; }
164151
self.start = Some(start);
@@ -168,7 +155,7 @@ impl DijkstraGraph
168155
// --- DIJKSTRA'S SHORTEST PATH ALGORITHM ---
169156
}
170157

171-
fn get_path(&self) -> Option<Vec<usize>>
158+
pub fn get_path(&self) -> Option<Vec<usize>>
172159
{
173160
if self.start.is_none() || self.end.is_none() { return None; }
174161

@@ -190,12 +177,10 @@ impl DijkstraGraph
190177
return Some(path);
191178
}
192179

193-
fn get_points(&self)
194-
{
195-
todo!();
196-
}
180+
pub fn points_iter(&self) -> Iter<Option<DijkstraNode>>
181+
{ return self.graph.iter(); }
197182

198-
fn get_lines(&self)
183+
pub fn get_lines(&self)
199184
{
200185
todo!();
201186
}
@@ -379,6 +364,15 @@ impl DijkstraGraph
379364
}
380365
}
381366

367+
struct DijkstraNode
368+
{
369+
position: Position,
370+
parent: Option<usize>,
371+
distance: Option<u16>,
372+
visited: bool,
373+
edges: Vec<Edge>,
374+
}
375+
382376
impl DijkstraNode
383377
{
384378
fn new(x: f32, y: f32) -> Self
@@ -393,25 +387,74 @@ impl DijkstraNode
393387
}
394388
}
395389

396-
pub fn update_position(&mut self, x: f32, y: f32)
397-
{ self.position.update(x, y); }
390+
pub fn position(&self) -> Position
391+
{ return self.position; }
392+
393+
pub fn parent(&self) -> Option<usize>
394+
{ return self.parent; }
395+
396+
pub fn set_parent(&mut self, parent: Option<usize>)
397+
{ self.parent = parent; }
398+
399+
pub fn distance(&self) -> Option<u16>
400+
{ return self.distance; }
401+
402+
pub fn set_distance(&mut self, distance: Option<u16>)
403+
{ self.distance = distance; }
404+
405+
pub fn visited(&self) -> bool
406+
{ return self.visited; }
407+
408+
pub fn set_visited(&mut self, visited: bool)
409+
{ self.visited = visited; }
410+
411+
pub fn edges(&self) -> Vec<Edge>
412+
{ return self.edges; }
413+
}
414+
415+
// TODO: consider making these fields pub
416+
struct Position
417+
{
418+
x: f32,
419+
y: f32,
398420
}
399421

400422
impl Position
401423
{
402-
pub fn get_x(&self) -> f32
424+
pub fn x(&self) -> f32
403425
{ return self.x; }
404426

405-
pub fn get_y(&self) -> f32
427+
pub fn set_x(&mut self, x: f32)
428+
{ self.x = x; }
429+
430+
pub fn y(&self) -> f32
406431
{ return self.y; }
407432

408-
pub fn update(&mut self, x: f32, y: f32)
433+
pub fn set_y(&mut self, y: f32)
434+
{ self.y = y; }
435+
436+
pub fn get(&self) -> (f32, f32)
437+
{ return (self.x, self.y); }
438+
439+
pub fn set(&mut self, x: f32, y: f32)
409440
{ self.x = x; self.y = y; }
410441
}
411442

443+
struct Edge
444+
{
445+
destination: usize,
446+
distance: u16,
447+
}
448+
412449
impl Edge
413450
{
414-
pub fn update_distance(&mut self, distance: u16)
451+
pub fn destination(&self) -> usize
452+
{ return self.destination; }
453+
454+
pub fn distance(&self) -> u16
455+
{ return self.distance; }
456+
457+
pub fn set_distance(&mut self, distance: u16)
415458
{ self.distance = distance; }
416459
}
417460

src/main.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,15 @@ async fn main()
6666
screen_width() - (ui_width + (3_f32 * radius)),
6767
screen_height() - (2_f32 * radius),
6868
)
69-
{ utils::handle_mouse_input(&mode, &mut graph); }
69+
{
70+
utils::handle_mouse_input(
71+
&mode,
72+
&mut graph,
73+
&mut hovered_point_id,
74+
&mut selected_point_id,
75+
&mut line_length
76+
);
77+
}
7078

7179
// --- GUI ---
7280
// TODO: style the GUI
@@ -81,7 +89,16 @@ async fn main()
8189
graph.print_path();
8290
}
8391

84-
graph.paint_graph();
92+
utils::paint_graph(
93+
&graph, &radius,
94+
&path_thickness,
95+
&padding,
96+
&hovered_point_id,
97+
&selected_point_id,
98+
&path_color,
99+
&line_color,
100+
&point_color
101+
);
85102

86103
draw();
87104

src/ui.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use egui_macroquad::{
55
};
66
use macroquad::time::get_fps;
77

8+
// TODO: make option to switch to hexagons for points
9+
810
pub(crate) fn paint_ui(mode: &mut Mode, graph: &mut DijkstraGraph, mut bg_color: &mut [f32; 3]) {
911
ui(|egui_context| {
1012
// Disabling all shadows

0 commit comments

Comments
 (0)