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
|
use crate::Rectangle;
/// A fixed reference line for the measurement of coordinates.
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub enum Axis {
/// The horizontal axis: —
Horizontal,
/// The vertical axis: |
Vertical,
}
impl Axis {
pub(super) fn split(
&self,
rectangle: &Rectangle,
ratio: f32,
spacing: f32,
) -> (Rectangle, Rectangle) {
match self {
Axis::Horizontal => {
let height_top =
(rectangle.height * ratio - spacing / 2.0).round();
let height_bottom = rectangle.height - height_top - spacing;
(
Rectangle {
height: height_top,
..*rectangle
},
Rectangle {
y: rectangle.y + height_top + spacing,
height: height_bottom,
..*rectangle
},
)
}
Axis::Vertical => {
let width_left =
(rectangle.width * ratio - spacing / 2.0).round();
let width_right = rectangle.width - width_left - spacing;
(
Rectangle {
width: width_left,
..*rectangle
},
Rectangle {
x: rectangle.x + width_left + spacing,
width: width_right,
..*rectangle
},
)
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn split_horizontal() {
let a = Axis::Horizontal;
// rectangle height, spacing, top height, bottom y, bottom height
let cases = vec![
// Even height, even spacing
(10.0, 2.0, 4.0, 6.0, 4.0),
// Odd height, even spacing
(9.0, 2.0, 4.0, 6.0, 3.0),
// Even height, odd spacing
(10.0, 1.0, 5.0, 6.0, 4.0),
// Odd height, odd spacing
(9.0, 1.0, 4.0, 5.0, 4.0),
];
for case in cases {
let (h0, spacing, h1_top, y_bottom, h1_bottom) = case;
let r = Rectangle {
x: 0.0,
y: 0.0,
width: 10.0,
height: h0,
};
let (top, bottom) = a.split(&r, 0.5, spacing);
assert_eq!(
top,
Rectangle {
height: h1_top,
..r
}
);
assert_eq!(
bottom,
Rectangle {
y: y_bottom,
height: h1_bottom,
..r
}
);
}
}
#[test]
fn split_vertical() {
let a = Axis::Vertical;
// rectangle width, spacing, left width, right x, right width
let cases = vec![
// Even width, even spacing
(10.0, 2.0, 4.0, 6.0, 4.0),
// Odd width, even spacing
(9.0, 2.0, 4.0, 6.0, 3.0),
// Even width, odd spacing
(10.0, 1.0, 5.0, 6.0, 4.0),
// Odd width, odd spacing
(9.0, 1.0, 4.0, 5.0, 4.0),
];
for case in cases {
let (w0, spacing, w1_left, x_right, w1_right) = case;
let r = Rectangle {
x: 0.0,
y: 0.0,
width: w0,
height: 10.0,
};
let (left, right) = a.split(&r, 0.5, spacing);
assert_eq!(
left,
Rectangle {
width: w1_left,
..r
}
);
assert_eq!(
right,
Rectangle {
x: x_right,
width: w1_right,
..r
}
);
}
}
}
|