Skip to content

Commit

Permalink
feat: draw lines between the stations
Browse files Browse the repository at this point in the history
  • Loading branch information
CalliEve committed Jun 12, 2024
1 parent 6e3df80 commit 4e93fbf
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 4 deletions.
64 changes: 62 additions & 2 deletions src/algorithm/objects/line.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use wasm_bindgen::JsValue;
use web_sys::CanvasRenderingContext2d;

use super::{Drawable, Station};
Expand All @@ -23,8 +24,67 @@ impl Line {

impl Drawable for Line {
fn draw(&self, canvas: &CanvasRenderingContext2d, square_size: u32) {
for station in self.get_stations() {
station.draw(canvas, square_size)
let offset = square_size as f64 / 3.0 - 2.0;
let stations = self.get_stations();

canvas.set_line_width(2.0);
canvas.set_stroke_style(&JsValue::from_str("black"));
canvas.begin_path();

if stations.len() > 1 {
for (start_station, end_station) in stations.iter().zip(stations.iter().skip(1)) {
let (from_x, from_y) =
station_corner_closest(start_station, end_station, square_size, offset);
canvas.move_to(from_x, from_y);

let (to_x, to_y) =
station_corner_closest(end_station, start_station, square_size, offset);
canvas.line_to(to_x, to_y);
}
} else if stations.len() == 1 {
let (station_x, station_y) = stations[0].get_canvas_pos(square_size);

canvas.move_to(station_x - offset, station_y);
canvas.line_to(station_x - (square_size as f64 - offset), station_y);

canvas.move_to(station_x + offset, station_y);
canvas.line_to(station_x + (square_size as f64 - offset), station_y);
}

canvas.stroke();
}
}

fn station_corner_closest(
station: &Station,
neighbor: &Station,
square_size: u32,
offset: f64,
) -> (f64, f64) {
let (station_x, station_y) = station.get_canvas_pos(square_size);
let (neighbor_x, neighbor_y) = neighbor.get_canvas_pos(square_size);

if station_x == neighbor_x {
if station_y > neighbor_y {
(station_x, station_y - offset) // below
} else {
(station_x, station_y + offset) // above
}
} else if station_x > neighbor_x {
if station_y == neighbor_y {
(station_x - offset, station_y) // left
} else if station_y > neighbor_y {
(station_x - offset, station_y - offset) // below left
} else {
(station_x - offset, station_y + offset) // above left
}
} else {
if station_y == neighbor_y {
(station_x + offset, station_y) // right
} else if station_y > neighbor_y {
(station_x + offset, station_y - offset) // below right
} else {
(station_x + offset, station_y + offset) // above right
}
}
}
3 changes: 3 additions & 0 deletions src/algorithm/objects/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ impl Map {

impl Drawable for Map {
fn draw(&self, canvas: &web_sys::CanvasRenderingContext2d, square_size: u32) {
for station in self.get_stations() {
station.draw(canvas, square_size)
}
for line in self.lines.iter() {
line.draw(canvas, square_size)
}
Expand Down
8 changes: 6 additions & 2 deletions src/algorithm/objects/station.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ impl Station {
self.pos.set(pos);
}

pub fn get_canvas_pos(&self, square_size: u32) -> (f64, f64) {
calc_canvas_loc(self.get_pos(), square_size)
}

pub fn clone_non_ref(&self) -> Self {
Self {
id: self.id,
Expand All @@ -60,7 +64,7 @@ impl Station {

impl Drawable for Station {
fn draw(&self, canvas: &CanvasRenderingContext2d, square_size: u32) {
let canvas_pos = calc_canvas_loc(self.get_pos(), square_size);
let canvas_pos = self.get_canvas_pos(square_size);

canvas.set_line_width(4.0);
if self.is_ghost {
Expand All @@ -72,7 +76,7 @@ impl Drawable for Station {
.arc(
canvas_pos.0,
canvas_pos.1,
(square_size / 3) as f64,
square_size as f64 / 3.0,
0.0,
2.0 * f64::consts::PI,
)
Expand Down
2 changes: 2 additions & 0 deletions src/components/sidebar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,7 @@ fn testmap() -> Map {
Station::new((30, 20)),
]));

map.add_line(Line::new(vec![Station::new((7, 5))]));

map
}

0 comments on commit 4e93fbf

Please sign in to comment.