diff --git a/board-builder-gui/src/main.rs b/board-builder-gui/src/main.rs index fbbae6e..e2c9f3b 100644 --- a/board-builder-gui/src/main.rs +++ b/board-builder-gui/src/main.rs @@ -7,7 +7,7 @@ use image::DynamicImage; use rfd::FileDialog; -use board_builder::{ Board, Node, CoordTransformer, read_board_from_file, write_board_to_file, should_wrap_horizontal }; +use board_builder::{ Board, Node, CoordTransformer, read_board_from_file, write_board_to_file }; use std::path::Path; use std::collections::HashSet; @@ -174,11 +174,11 @@ impl BoardBuilderApp { let stroke = Stroke { width: 1.0, color: Color32::BLACK }; for edge in &node.edges { let other_node = &board.nodes[edge]; - if board.config.horizontal_wrapping && should_wrap_horizontal(node, other_node) { + if board.config.horizontal_wrapping && node.should_wrap_horizontal(other_node) { let mut nodes = [node, other_node]; nodes.sort_by(|a, b| a.x.partial_cmp(&b.x).unwrap()); let [left_node, right_node] = nodes; - let y_mid = (left_node.y + right_node.y) / 2.0; + let y_mid = left_node.y_mid(right_node); painter.line_segment( [view.inv_xform(0.0, y_mid), view.inv_xform(left_node.x, left_node.y)], stroke diff --git a/src/lib.rs b/src/lib.rs index cc314e4..742e70a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,6 +23,22 @@ pub struct Node { pub edges: HashSet, pub labels: HashMap, } + +impl Node { + pub fn should_wrap_horizontal(&self, other: &Node) -> bool { + let mut xs = [self.x, other.x]; + xs.sort_by(|a, b| a.partial_cmp(b).unwrap()); + xs[0] + (1.0 - xs[1]) < xs[1] - xs[0] + } + + pub fn y_mid(&self, other: &Node) -> f32 { + let mut coords = [(self.x, self.y), (other.x, other.y)]; + coords.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap()); + let [(x1, y1), (x2, y2)] = coords; + y1 - x1 * (y1 - y2) / (x1 + 1.0 - x2) + } +} + #[derive(Serialize, Deserialize, Debug, Default)] #[serde(default)] pub struct Board { @@ -104,12 +120,6 @@ fn dist_sq((x0, y0): (f32, f32), (x1, y1): (f32, f32)) -> f32 { (x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0) } -pub fn should_wrap_horizontal(node1: &Node, node2: &Node) -> bool { - let mut xs = [node1.x, node2.x]; - xs.sort_by(|a, b| a.partial_cmp(b).unwrap()); - xs[0] + (1.0 - xs[1]) < xs[1] - xs[0] -} - use std::io; use std::io::{ Write, Read, Cursor }; use std::fs::File;