blockworx_editor/widget/
edge.rs1use crate::grid::px_point;
2use blockworx_geom::Pos2;
3
4pub use crate::presentation::RouteEdge;
7
8pub trait RouteEdgeExt {
11 fn distance(&self, pos: Pos2) -> (f32, f32);
16 fn length(&self) -> f32;
18}
19
20impl RouteEdgeExt for RouteEdge {
21 fn distance(&self, pos: Pos2) -> (f32, f32) {
22 let start: Pos2 = px_point(self.start);
23 let end: Pos2 = px_point(self.end);
24 let line_vec = end - start;
25 let line_len = line_vec.length();
26 if line_len == 0.0 {
27 return (0.0, (pos - start).length());
28 }
29 let t = ((pos - start).dot(line_vec) / line_len.powi(2)).clamp(0.0, 1.0);
30 let projection = start + line_vec * t;
31 let linear_distance = line_len * t;
32 (linear_distance, (pos - projection).length())
33 }
34 fn length(&self) -> f32 {
35 (px_point(self.end) - px_point(self.start)).length()
36 }
37}