From 4e93fbfcf842e6635b5dea674149fddd55c7dacb Mon Sep 17 00:00:00 2001 From: Calli Evers Date: Wed, 12 Jun 2024 16:19:28 +0200 Subject: [PATCH] feat: draw lines between the stations --- src/algorithm/objects/line.rs | 64 +++++++++++++++++++++++++++++++- src/algorithm/objects/map.rs | 3 ++ src/algorithm/objects/station.rs | 8 +++- src/components/sidebar.rs | 2 + 4 files changed, 73 insertions(+), 4 deletions(-) diff --git a/src/algorithm/objects/line.rs b/src/algorithm/objects/line.rs index 588c6a1..4f4244c 100644 --- a/src/algorithm/objects/line.rs +++ b/src/algorithm/objects/line.rs @@ -1,3 +1,4 @@ +use wasm_bindgen::JsValue; use web_sys::CanvasRenderingContext2d; use super::{Drawable, Station}; @@ -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 } } } diff --git a/src/algorithm/objects/map.rs b/src/algorithm/objects/map.rs index b358098..bfea9fa 100644 --- a/src/algorithm/objects/map.rs +++ b/src/algorithm/objects/map.rs @@ -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) } diff --git a/src/algorithm/objects/station.rs b/src/algorithm/objects/station.rs index 4914bef..099673c 100644 --- a/src/algorithm/objects/station.rs +++ b/src/algorithm/objects/station.rs @@ -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, @@ -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 { @@ -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, ) diff --git a/src/components/sidebar.rs b/src/components/sidebar.rs index af065bc..19eb57b 100644 --- a/src/components/sidebar.rs +++ b/src/components/sidebar.rs @@ -37,5 +37,7 @@ fn testmap() -> Map { Station::new((30, 20)), ])); + map.add_line(Line::new(vec![Station::new((7, 5))])); + map }