Horizontal Wrapping

This commit is contained in:
Dane Johnson 2022-06-08 11:42:36 -05:00
parent 67ff5dd0df
commit dbd370f53c
2 changed files with 19 additions and 9 deletions

View File

@ -7,7 +7,7 @@ use image::DynamicImage;
use rfd::FileDialog; 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::path::Path;
use std::collections::HashSet; use std::collections::HashSet;
@ -174,11 +174,11 @@ impl BoardBuilderApp {
let stroke = Stroke { width: 1.0, color: Color32::BLACK }; let stroke = Stroke { width: 1.0, color: Color32::BLACK };
for edge in &node.edges { for edge in &node.edges {
let other_node = &board.nodes[edge]; 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]; let mut nodes = [node, other_node];
nodes.sort_by(|a, b| a.x.partial_cmp(&b.x).unwrap()); nodes.sort_by(|a, b| a.x.partial_cmp(&b.x).unwrap());
let [left_node, right_node] = nodes; 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( painter.line_segment(
[view.inv_xform(0.0, y_mid), view.inv_xform(left_node.x, left_node.y)], [view.inv_xform(0.0, y_mid), view.inv_xform(left_node.x, left_node.y)],
stroke stroke

View File

@ -23,6 +23,22 @@ pub struct Node {
pub edges: HashSet<usize>, pub edges: HashSet<usize>,
pub labels: HashMap<String, String>, pub labels: HashMap<String, String>,
} }
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)] #[derive(Serialize, Deserialize, Debug, Default)]
#[serde(default)] #[serde(default)]
pub struct Board { 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) (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;
use std::io::{ Write, Read, Cursor }; use std::io::{ Write, Read, Cursor };
use std::fs::File; use std::fs::File;