Skip to content

Commit 1b8ab0a

Browse files
committed
Finishing touches
1 parent 8c72746 commit 1b8ab0a

File tree

5 files changed

+412
-369
lines changed

5 files changed

+412
-369
lines changed

src/graph.rs

Lines changed: 70 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use crate::{draw_pill, utils};
21
use macroquad::{
32
prelude::{mouse_position, Color, IVec2, Vec4, BLACK, GREEN, MAGENTA, YELLOW},
43
shapes::{draw_circle, draw_circle_lines, draw_line, draw_triangle},
@@ -11,6 +10,8 @@ use std::{
1110
ops::{Div, Mul},
1211
};
1312

13+
use crate::utils;
14+
1415
/// ### Graph
1516
///
1617
/// It contains nodes and edges connecting those nodes.
@@ -190,15 +191,15 @@ impl Graph {
190191
.iter()
191192
{
192193
if utils::is_point_in_circle(
193-
mouse_position().0,
194-
mouse_position().1,
194+
mouse_position().0 as i32,
195+
mouse_position().1 as i32,
195196
node
196197
.position
197-
.x as f32,
198+
.x,
198199
node
199200
.position
200-
.y as f32,
201-
self.radius as f32,
201+
.y,
202+
self.radius as i32,
202203
) {
203204
self.hovered_point_id = Some(*id);
204205
return Some(*id);
@@ -267,7 +268,7 @@ impl Graph {
267268

268269
// --- DIJKSTRA'S SHORTEST PATH ALGORITHM ---
269270

270-
// TOFIX: large graph doesn't give shortest path from 6 to 18
271+
// TOFIX: large graph doesn't give shortest path from 6 to 18 (point 10 seems to cause it)
271272
while !untested_nodes.is_empty() {
272273
// Remove all visited nodes
273274
untested_nodes.retain(|id| {
@@ -380,38 +381,36 @@ impl Graph {
380381
}
381382
}
382383

383-
// TODO: print arrowhead over the path
384-
// TODO: use functional pattern if possible
385384
pub fn paint_path(&self) {
386-
if let Some(path) = &self.path {
387-
for (from, to) in path
388-
.iter()
389-
.zip(
390-
path
391-
.iter()
392-
.skip(1),
393-
)
394-
{
395-
let Some(from_point) = self.points.get(from) else { continue; };
396-
let Some(to_point) = self.points.get(to) else { continue; };
385+
let Some(path) = &self.path else { return; };
397386

398-
draw_line(
399-
from_point
400-
.position
401-
.x as f32,
402-
from_point
403-
.position
404-
.y as f32,
405-
to_point
406-
.position
407-
.x as f32,
408-
to_point
409-
.position
410-
.y as f32,
411-
self.path_thickness,
412-
Color::from_vec(Vec4::new(self.path_color[0], self.path_color[1], self.path_color[2], 1.)),
413-
);
414-
}
387+
for (from, to) in path
388+
.iter()
389+
.zip(
390+
path
391+
.iter()
392+
.skip(1),
393+
)
394+
{
395+
let Some(from_point) = self.points.get(from) else { continue; };
396+
let Some(to_point) = self.points.get(to) else { continue; };
397+
398+
draw_line(
399+
from_point
400+
.position
401+
.x as f32,
402+
from_point
403+
.position
404+
.y as f32,
405+
to_point
406+
.position
407+
.x as f32,
408+
to_point
409+
.position
410+
.y as f32,
411+
self.path_thickness,
412+
Color::from_vec(Vec4::new(self.path_color[0], self.path_color[1], self.path_color[2], 1.)),
413+
);
415414
}
416415
}
417416

@@ -492,7 +491,7 @@ impl Graph {
492491
self.has_hovered_point_been_checked = false;
493492
}
494493

495-
pub fn paint_lines(&self) {
494+
pub fn paint_arrow_heads(&self) {
496495
for (line, _) in self
497496
.lines
498497
.iter()
@@ -521,6 +520,7 @@ impl Graph {
521520
.y,
522521
};
523522

523+
// Calculating the tip of the triangle that touches the node (position + (direction * (radius / length)))
524524
let arrow_head_location = IVec2 {
525525
x: to_point
526526
.position
@@ -560,7 +560,7 @@ impl Graph {
560560
.length())) as i32,
561561
};
562562

563-
// Calculating the tip of the triangle that touches the node (position + (direction * (radius / length)))
563+
/*
564564
draw_line(
565565
from_point
566566
.position
@@ -583,6 +583,7 @@ impl Graph {
583583
1.0,
584584
Color::from_vec(Vec4::new(self.line_color[0], self.line_color[1], self.line_color[2], 1.)),
585585
);
586+
*/
586587

587588
/*
588589
x1/y1 are the start of the line, x2/y2 are the end of the line where the head of the arrow should be
@@ -716,6 +717,33 @@ impl Graph {
716717
}
717718
}
718719

720+
pub fn paint_lines(&self) {
721+
for (line, _) in self
722+
.lines
723+
.iter()
724+
{
725+
let Some(from_point) = self.points.get(&line.from) else { continue; };
726+
let Some(to_point) = self.points.get(&line.to) else { continue; };
727+
728+
draw_line(
729+
from_point
730+
.position
731+
.x as f32,
732+
from_point
733+
.position
734+
.y as f32,
735+
to_point
736+
.position
737+
.x as f32,
738+
to_point
739+
.position
740+
.y as f32,
741+
1.0,
742+
Color::from_vec(Vec4::new(self.line_color[0], self.line_color[1], self.line_color[2], 1.0)),
743+
);
744+
}
745+
}
746+
719747
pub fn paint_line_lengths(&self) {
720748
for (line, length) in self
721749
.lines
@@ -768,7 +796,7 @@ impl Graph {
768796
1.0,
769797
);
770798

771-
draw_pill(
799+
utils::draw_pill(
772800
position.x as f32
773801
- text_dimensions
774802
.width
@@ -808,7 +836,7 @@ impl Graph {
808836
let text_dimensions = measure_text(text, None, 20, 1.0);
809837

810838
// A 2 pixel gap between the label and the point is hard-coded
811-
draw_pill(
839+
utils::draw_pill(
812840
position.x as f32
813841
- text_dimensions
814842
.width
@@ -852,6 +880,7 @@ impl Graph {
852880
{
853881
self.paint_lines();
854882
self.paint_path();
883+
self.paint_arrow_heads();
855884
self.paint_line_lengths();
856885
}
857886

0 commit comments

Comments
 (0)