1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
//! Direction-agnostic orientation system for diagram rendering.
//!
//! Provides abstractions for all four diagram orientations (TD, LR, BT, RL)
//! using a unified coordinate system.
//!
//! # Concepts
//!
//! - **Primary axis**: Flow direction (vertical for TD/BT, horizontal for LR/RL)
//! - **Secondary axis**: Branching direction (perpendicular to primary)
//! - **Advance/Retreat**: Movement along primary axis in flow direction
use crate::graph::Direction;
use crate::style::StyleChars;
/// Represents the two axes in a 2D layout
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Axis {
Horizontal,
Vertical,
}
/// Orientation-aware coordinate system
pub struct OrientedCoords {
pub primary: Axis,
pub secondary: Axis,
pub direction: Direction,
}
impl OrientedCoords {
/// Create a new oriented coordinate system based on diagram direction
pub fn new(direction: Direction) -> Self {
match direction {
Direction::TD | Direction::TB => Self {
primary: Axis::Vertical,
secondary: Axis::Horizontal,
direction,
},
Direction::LR => Self {
primary: Axis::Horizontal,
secondary: Axis::Vertical,
direction,
},
Direction::RL => Self {
primary: Axis::Horizontal,
secondary: Axis::Vertical,
direction,
},
Direction::BT => Self {
primary: Axis::Vertical,
secondary: Axis::Horizontal,
direction,
},
}
}
/// Get the primary axis value from x,y coordinates
pub fn primary_coord(&self, x: usize, y: usize) -> usize {
match self.primary {
Axis::Horizontal => x,
Axis::Vertical => y,
}
}
/// Get the secondary axis value from x,y coordinates
pub fn secondary_coord(&self, x: usize, y: usize) -> usize {
match self.secondary {
Axis::Horizontal => x,
Axis::Vertical => y,
}
}
/// Set a coordinate on the primary axis
pub fn set_primary(&self, x: &mut usize, y: &mut usize, value: usize) {
match self.primary {
Axis::Horizontal => *x = value,
Axis::Vertical => *y = value,
}
}
/// Set a coordinate on the secondary axis
pub fn set_secondary(&self, x: &mut usize, y: &mut usize, value: usize) {
match self.secondary {
Axis::Horizontal => *x = value,
Axis::Vertical => *y = value,
}
}
/// Get the appropriate edge character for the primary axis
pub fn primary_edge_char(&self, style: &StyleChars) -> char {
match self.primary {
Axis::Horizontal => style.edge_h,
Axis::Vertical => style.edge_v,
}
}
/// Get the appropriate edge character for the secondary axis
pub fn secondary_edge_char(&self, style: &StyleChars) -> char {
match self.secondary {
Axis::Horizontal => style.edge_h,
Axis::Vertical => style.edge_v,
}
}
/// Get the appropriate arrow character for the end of flow
pub fn arrow_end(&self, style: &StyleChars) -> char {
match self.direction {
Direction::TD | Direction::TB => style.arrow_down,
Direction::LR => style.arrow_right,
Direction::RL => style.arrow_left,
Direction::BT => style.arrow_up,
}
}
/// Get the appropriate junction character for a branch point
/// where flow splits from primary to secondary axis
pub fn junction_branch(&self, style: &StyleChars) -> char {
match self.direction {
Direction::TD | Direction::TB => style.junction_down, // ┬ (branches down)
Direction::LR => style.junction_right, // ├ (branches right)
Direction::RL => style.junction_left, // ┤ (branches left)
Direction::BT => style.junction_up, // ┴ (branches up)
}
}
/// Get the appropriate junction character for a merge point
/// where flow converges from secondary to primary axis
pub fn junction_merge(&self, style: &StyleChars) -> char {
match self.direction {
Direction::TD | Direction::TB => style.junction_up, // ┴ (merges up)
Direction::LR => style.junction_left, // ┤ (merges left)
Direction::RL => style.junction_right, // ├ (merges right)
Direction::BT => style.junction_down, // ┬ (merges down)
}
}
/// Get corner character for turning from primary to secondary axis
/// at the start position
pub fn corner_start_to_secondary(&self, going_before: bool, style: &StyleChars) -> char {
match (self.direction, going_before) {
(Direction::TD | Direction::TB, true) => style.corner_ur, // down then left
(Direction::TD | Direction::TB, false) => style.corner_ul, // down then right
(Direction::LR, true) => style.corner_dl, // ┌ (right to up)
(Direction::LR, false) => style.corner_ul, // ┌ (right to down)
(Direction::RL, true) => style.corner_dr, // ┘ (left to up)
(Direction::RL, false) => style.corner_ur, // ┐ (left to down)
(Direction::BT, true) => style.corner_dr, // up then left
(Direction::BT, false) => style.corner_dl, // up then right
}
}
/// Get corner character for turning from secondary to primary axis
/// at the end position
pub fn corner_secondary_to_end(&self, coming_from_before: bool, style: &StyleChars) -> char {
match (self.direction, coming_from_before) {
(Direction::TD | Direction::TB, true) => style.corner_dl, // left to down
(Direction::TD | Direction::TB, false) => style.corner_dr, // right to down
(Direction::LR, true) => style.corner_ur, // ┘ (up to right)
(Direction::LR, false) => style.corner_dr, // ┘ (down to right)
(Direction::RL, true) => style.corner_ul, // ┌ (up to left)
(Direction::RL, false) => style.corner_dl, // └ (down to left)
(Direction::BT, true) => style.corner_ul, // left to up
(Direction::BT, false) => style.corner_ur, // right to up
}
}
/// Advance position along primary axis in flow direction.
pub fn advance(&self, x: usize, y: usize, distance: usize) -> (usize, usize) {
let mut new_x = x;
let mut new_y = y;
match self.primary {
Axis::Horizontal => match self.direction {
Direction::RL => new_x = new_x.saturating_sub(distance),
_ => new_x += distance,
},
Axis::Vertical => match self.direction {
Direction::BT => new_y = new_y.saturating_sub(distance),
_ => new_y += distance,
},
}
(new_x, new_y)
}
/// Retreat position along primary axis (opposite of flow direction).
pub fn retreat(&self, x: usize, y: usize, distance: usize) -> (usize, usize) {
let mut new_x = x;
let mut new_y = y;
match self.primary {
Axis::Horizontal => match self.direction {
Direction::RL => new_x += distance,
_ => new_x = new_x.saturating_sub(distance),
},
Axis::Vertical => match self.direction {
Direction::BT => new_y += distance,
_ => new_y = new_y.saturating_sub(distance),
},
}
(new_x, new_y)
}
/// Return new coordinates with a specific secondary axis value.
pub fn with_secondary(&self, x: usize, y: usize, secondary_val: usize) -> (usize, usize) {
let mut new_x = x;
let mut new_y = y;
self.set_secondary(&mut new_x, &mut new_y, secondary_val);
(new_x, new_y)
}
}
/// Helper to determine if we're moving "before" or "after" on secondary axis
pub fn is_before(from: usize, to: usize) -> bool {
from > to
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_td_orientation() {
let coords = OrientedCoords::new(Direction::TD);
assert_eq!(coords.primary, Axis::Vertical);
assert_eq!(coords.secondary, Axis::Horizontal);
// Primary coord should return y for TD
assert_eq!(coords.primary_coord(10, 20), 20);
// Secondary coord should return x for TD
assert_eq!(coords.secondary_coord(10, 20), 10);
}
#[test]
fn test_lr_orientation() {
let coords = OrientedCoords::new(Direction::LR);
assert_eq!(coords.primary, Axis::Horizontal);
assert_eq!(coords.secondary, Axis::Vertical);
// Primary coord should return x for LR
assert_eq!(coords.primary_coord(10, 20), 10);
// Secondary coord should return y for LR
assert_eq!(coords.secondary_coord(10, 20), 20);
}
}