Skip to content

Commit 3427e22

Browse files
committed
Adjusted UI && tests; Reworked most utils functions into functional style
1 parent 18dc280 commit 3427e22

5 files changed

Lines changed: 195 additions & 135 deletions

File tree

src/graph.rs

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -126,30 +126,36 @@ impl DijkstraGraph
126126
pub fn get(&self, id: usize) -> Option<DijkstraNode>
127127
{ return self.graph[id]; }
128128

129-
pub fn get_start(&self) -> Option<usize>
129+
pub fn start(&self) -> Option<usize>
130130
{ return self.start; }
131131

132-
pub fn set_start(&mut self, start: Option<usize>)
133-
{ self.start = start; }
132+
pub fn set_start(&mut self, start: usize)
133+
{
134+
if start > 100 { return; }
135+
136+
self.start = Some(start);
137+
}
134138

135139
pub fn clear_start(&mut self)
136140
{ self.start = None; }
137141

138-
pub fn get_end(&self) -> Option<usize>
142+
pub fn end(&self) -> Option<usize>
139143
{ return self.end; }
140144

141-
pub fn set_end(&mut self, end: Option<usize>)
142-
{ self.end = end; }
145+
pub fn set_end(&mut self, end: usize)
146+
{
147+
if end > 100 { return; }
148+
149+
self.end = Some(end);
150+
}
143151

144152
pub fn clear_end(&mut self)
145153
{ self.end = None; }
146154

147155
/// Returns true if the shortest path has been found
148-
pub fn find_shortest_path(&mut self, start: usize, end: usize) -> bool
156+
pub fn find_shortest_path(&mut self) -> bool
149157
{
150-
if start > 100 || end > 100 { return false; }
151-
self.start = Some(start);
152-
self.end = Some(end);
158+
if self.start.is_none() || self.end.is_none() { return false; }
153159

154160
todo!();
155161
// --- DIJKSTRA'S SHORTEST PATH ALGORITHM ---
@@ -180,6 +186,27 @@ impl DijkstraGraph
180186
pub fn points_iter(&self) -> Iter<Option<DijkstraNode>>
181187
{ return self.graph.iter(); }
182188

189+
pub fn lines_iter(&self) -> Iter<&(DijkstraNode, u16, DijkstraNode)>
190+
{
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+
}
209+
183210
pub fn get_lines(&self)
184211
{
185212
todo!();
@@ -366,7 +393,7 @@ impl DijkstraGraph
366393

367394
struct DijkstraNode
368395
{
369-
position: Position,
396+
pub position: Position,
370397
parent: Option<usize>,
371398
distance: Option<u16>,
372399
visited: bool,
@@ -415,24 +442,12 @@ impl DijkstraNode
415442
// TODO: consider making these fields pub
416443
struct Position
417444
{
418-
x: f32,
419-
y: f32,
445+
pub x: f32,
446+
pub y: f32,
420447
}
421448

422449
impl Position
423450
{
424-
pub fn x(&self) -> f32
425-
{ return self.x; }
426-
427-
pub fn set_x(&mut self, x: f32)
428-
{ self.x = x; }
429-
430-
pub fn y(&self) -> f32
431-
{ return self.y; }
432-
433-
pub fn set_y(&mut self, y: f32)
434-
{ self.y = y; }
435-
436451
pub fn get(&self) -> (f32, f32)
437452
{ return (self.x, self.y); }
438453

src/main.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ mod graph;
22
mod ui;
33
mod utils;
44

5-
use crate::utils::Mode;
65
use egui_macroquad::draw;
76
use graph::*;
87
use macroquad::prelude::*;
@@ -20,12 +19,19 @@ fn window_configuration() -> Conf
2019
};
2120
}
2221

22+
#[derive(PartialEq, Eq, Clone, Copy)]
23+
pub(crate) enum Mode {
24+
Move,
25+
Point,
26+
Line,
27+
Path,
28+
}
29+
2330
// FIX: path from 6 to 18 is wrong, there exist a much shorter one (point 10 seems to cause that somehow)
2431
#[macroquad::main(window_configuration)]
2532
async fn main()
2633
{
2734
let mut graph = DijkstraGraph::new();
28-
let mut path: Option<Vec<u8>> = None;
2935
// let mut start: Option<usize> = None;
3036
// let mut end: Option<usize> = None;
3137
// This is the id of the point that the mouse is currently hovering over
@@ -78,7 +84,22 @@ async fn main()
7884

7985
// --- GUI ---
8086
// TODO: style the GUI
81-
ui::paint_ui(&mut mode, &mut graph, &mut bg_color);
87+
ui::paint_ui(
88+
&mut mode,
89+
&mut graph,
90+
&mut radius,
91+
&mut angle,
92+
&mut arrow_head_length,
93+
&mut path_thickness,
94+
&mut base_point,
95+
&mut padding,
96+
&mut selected_point_id,
97+
&mut line_length,
98+
&mut path_color,
99+
&mut line_color,
100+
&mut point_color,
101+
&mut bg_color
102+
);
82103

83104
// ! dbg
84105
if is_key_pressed(KeyCode::P)

src/tests/graph_tests.rs

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ fn shortest_path_small()
5858
{
5959
let mut graph = DijkstraGraph::new();
6060
graph.insert_small_graph();
61-
assert!(graph.find_shortest_path(2, 0));
61+
graph.set_start(2);
62+
graph.set_end(0);
63+
assert!(graph.find_shortest_path());
6264

6365
let should_path_1 = vec![2, 3, 4, 0];
6466
let should_path_2 = vec![2, 5, 7, 0];
@@ -122,7 +124,10 @@ fn shortest_path_small()
122124

123125
let should_path = vec![6, 7, 0, 1, 2, 8];
124126

125-
assert!(graph.find_shortest_path(6, 8));
127+
graph.set_start(6);
128+
graph.set_end(8);
129+
130+
assert!(graph.find_shortest_path());
126131

127132
match graph.get_path()
128133
{
@@ -150,7 +155,10 @@ fn shortest_path_medium()
150155
let should_path_2 = vec![3, 10, 7, 4, 0];
151156
let should_path_3 = vec![3, 11, 8, 4, 0];
152157

153-
graph.find_shortest_path(3, 0);
158+
graph.set_start(3);
159+
graph.set_end(0);
160+
161+
graph.find_shortest_path();
154162

155163
match graph.get_path()
156164
{
@@ -175,7 +183,10 @@ fn shortest_path_medium()
175183
let should_path_4 = vec![3, 11, 7, 4, 1];
176184
let should_path_5 = vec![3, 11, 7, 1];
177185

178-
graph.find_shortest_path(3, 1);
186+
graph.set_start(3);
187+
graph.set_end(1);
188+
189+
graph.find_shortest_path();
179190

180191
match graph.get_path()
181192
{
@@ -206,7 +217,10 @@ fn shortest_path_medium()
206217
let should_path_1 = vec![3, 10, 7, 5, 2];
207218
let should_path_2 = vec![3, 11, 7, 5, 2];
208219

209-
graph.find_shortest_path(3, 2);
220+
graph.set_start(3);
221+
graph.set_end(2);
222+
223+
graph.find_shortest_path();
210224

211225
match graph.get_path()
212226
{
@@ -232,7 +246,10 @@ fn shortest_path_large()
232246

233247
let should_path = vec![5, 6, 4, 1, 2, 7, 3, 0, 8];
234248

235-
graph.find_shortest_path(5, 8);
249+
graph.set_start(5);
250+
graph.set_end(8);
251+
252+
graph.find_shortest_path();
236253

237254
match graph.get_path()
238255
{
@@ -284,7 +301,10 @@ fn start_and_end_are_within_graph()
284301

285302
let should_path = vec![10, 7, 6, 4];
286303

287-
graph.find_shortest_path(10, 4);
304+
graph.set_start(10);
305+
graph.set_end(4);
306+
307+
graph.find_shortest_path();
288308

289309
match graph.get_path()
290310
{
@@ -305,7 +325,10 @@ fn no_possible_path()
305325
let mut graph = DijkstraGraph::new();
306326
graph.insert_small_graph();
307327

308-
assert!(!graph.find_shortest_path(0, 2));
328+
graph.set_start(0);
329+
graph.set_end(2);
330+
331+
assert!(!graph.find_shortest_path());
309332
assert!(graph.get_path().is_none());
310333
}
311334

@@ -332,7 +355,10 @@ fn disconnected_graph()
332355
graph.add_line(7, 6, 20);
333356
graph.add_line(2, 4, 20);
334357

335-
graph.find_shortest_path(2, 0);
358+
graph.set_start(2);
359+
graph.set_end(0);
360+
361+
graph.find_shortest_path();
336362

337363
assert!(graph.get_path().is_none());
338364
}
@@ -371,7 +397,10 @@ fn cyclical_valid_path()
371397

372398
let should_path = vec![3, 7, 6, 5, 4];
373399

374-
graph.find_shortest_path(3, 4);
400+
graph.set_start(3);
401+
graph.set_end(4);
402+
403+
graph.find_shortest_path();
375404

376405
match graph.get_path()
377406
{

0 commit comments

Comments
 (0)