#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct Rect {
pub x: usize,
pub y: usize,
pub width: usize,
pub height: usize,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Size {
Fixed(usize),
Percent(usize),
Fill(usize),
}
impl Rect {
pub fn new(x: usize, y: usize, width: usize, height: usize) -> Self {
Rect { x, y, width, height }
}
pub fn rows<const N: usize>(self, sizes: [Size; N]) -> [Rect; N] {
self.rows_gap(0, sizes)
}
pub fn columns<const N: usize>(self, sizes: [Size; N]) -> [Rect; N] {
self.columns_gap(0, sizes)
}
pub fn rows_gap<const N: usize>(self, gap: usize, sizes: [Size; N]) -> [Rect; N] {
let lens = split(self.height, gap, &sizes);
let mut y = self.y;
lens.map(|h| {
let r = Rect::new(self.x, y, self.width, h);
y += h + gap;
r
})
}
pub fn columns_gap<const N: usize>(self, gap: usize, sizes: [Size; N]) -> [Rect; N] {
let lens = split(self.width, gap, &sizes);
let mut x = self.x;
lens.map(|w| {
let r = Rect::new(x, self.y, w, self.height);
x += w + gap;
r
})
}
#[must_use]
pub fn shrink(self, x: usize, y: usize) -> Rect {
Rect::new(
self.x + x.min(self.width / 2),
self.y + y.min(self.height / 2),
self.width.saturating_sub(2 * x),
self.height.saturating_sub(2 * y),
)
}
#[must_use]
pub fn center(self, width: usize, height: usize) -> Rect {
let (w, h) = (width.min(self.width), height.min(self.height));
Rect::new(self.x + (self.width - w) / 2, self.y + (self.height - h) / 2, w, h)
}
#[must_use]
pub fn split_left(self, width: usize) -> (Rect, Rect) {
let w = width.min(self.width);
(
Rect::new(self.x, self.y, w, self.height),
Rect::new(self.x + w, self.y, self.width - w, self.height),
)
}
#[must_use]
pub fn split_right(self, width: usize) -> (Rect, Rect) {
let w = width.min(self.width);
(
Rect::new(self.x + self.width - w, self.y, w, self.height),
Rect::new(self.x, self.y, self.width - w, self.height),
)
}
#[must_use]
pub fn split_top(self, height: usize) -> (Rect, Rect) {
let h = height.min(self.height);
(
Rect::new(self.x, self.y, self.width, h),
Rect::new(self.x, self.y + h, self.width, self.height - h),
)
}
#[must_use]
pub fn split_bottom(self, height: usize) -> (Rect, Rect) {
let h = height.min(self.height);
(
Rect::new(self.x, self.y + self.height - h, self.width, h),
Rect::new(self.x, self.y, self.width, self.height - h),
)
}
#[must_use]
pub fn row(self, n: usize) -> Rect {
if n >= self.height {
return Rect::new(self.x, self.y.saturating_add(self.height), self.width, 0);
}
Rect::new(self.x, self.y + n, self.width, 1)
}
pub fn contains(self, x: usize, y: usize) -> bool {
(self.x..self.x.saturating_add(self.width)).contains(&x)
&& (self.y..self.y.saturating_add(self.height)).contains(&y)
}
pub fn is_empty(self) -> bool {
self.width == 0 || self.height == 0
}
#[must_use]
pub fn intersect(self, other: Rect) -> Rect {
let (other_right, other_bottom) = (
other.x.saturating_add(other.width),
other.y.saturating_add(other.height),
);
let right = self.x.saturating_add(self.width).min(other_right);
let bottom = self.y.saturating_add(self.height).min(other_bottom);
let x = self.x.max(other.x).min(other_right);
let y = self.y.max(other.y).min(other_bottom);
Rect::new(x, y, right.saturating_sub(x), bottom.saturating_sub(y))
}
}
fn split<const N: usize>(total: usize, gap: usize, sizes: &[Size; N]) -> [usize; N] {
let mut lens = [0; N];
split_into(total, gap, sizes, &mut lens);
lens
}
fn split_into(total: usize, gap: usize, sizes: &[Size], lens: &mut [usize]) {
let n = sizes.len();
let mut left = total.saturating_sub(gap * n.saturating_sub(1));
let whole = left;
for (len, size) in lens.iter_mut().zip(sizes) {
let want = match *size {
Size::Fixed(n) => n,
Size::Percent(p) => whole * p.min(100) / 100,
Size::Fill(_) => continue,
};
*len = want.min(left);
left -= *len;
}
let weights: usize = sizes.iter().map(|s| if let Size::Fill(w) = s { *w } else { 0 }).sum();
if weights == 0 {
return;
}
let share = left;
let last = sizes.iter().rposition(|s| matches!(s, Size::Fill(w) if *w > 0));
for (i, (len, size)) in lens.iter_mut().zip(sizes).enumerate() {
if let Size::Fill(w) = *size {
*len = if Some(i) == last { left } else { share * w / weights };
left -= *len;
}
}
}
#[cfg(test)]
mod tests {
use super::Size::*;
use super::*;
#[test]
fn rectangles_out_at_the_edge_of_the_numbers_dont_overflow() {
let huge = Rect::new(usize::MAX - 1, usize::MAX - 1, 10, 10);
assert!(huge.contains(usize::MAX - 1, usize::MAX - 1));
assert!(!huge.contains(0, 0));
let screen = Rect::new(0, 0, 80, 24);
assert!(huge.intersect(screen).is_empty());
assert!(screen.intersect(huge).is_empty());
assert_eq!(
Rect::new(2, 3, usize::MAX, usize::MAX).intersect(screen),
Rect::new(2, 3, 78, 21)
);
assert!(huge.row(20).is_empty());
let mut frame = super::super::Frame::new(5, 1);
assert_eq!(frame.print(Rect::new(1, 0, usize::MAX, 1), 0, "hello"), 4);
}
#[test]
fn splits_fixed_percent_and_fill() {
assert_eq!(split(100, 0, &[Fixed(10), Percent(20), Fill(1)]), [10, 20, 70]);
assert_eq!(split(10, 0, &[Fill(1), Fill(2)]), [3, 7]);
assert_eq!(split(11, 1, &[Fill(1), Fill(1)]), [5, 5]);
assert_eq!(split(5, 0, &[Fixed(4), Fixed(4), Fill(1)]), [4, 1, 0]);
}
#[test]
fn centers_and_shrinks() {
let r = Rect::new(0, 0, 80, 24);
assert_eq!(r.center(40, 10), Rect::new(20, 7, 40, 10));
assert_eq!(r.center(100, 100), r);
assert_eq!(r.shrink(2, 1), Rect::new(2, 1, 76, 22));
assert_eq!(Rect::new(0, 0, 3, 1).shrink(2, 2).width, 0);
}
#[test]
fn cuts_rows_and_columns_and_answers_questions() {
let r = Rect::new(2, 1, 10, 4);
assert_eq!(
r.columns([Fixed(3), Fill(1)]),
[Rect::new(2, 1, 3, 4), Rect::new(5, 1, 7, 4)]
);
assert_eq!(
r.rows([Percent(50), Fill(1)]),
[Rect::new(2, 1, 10, 2), Rect::new(2, 3, 10, 2)]
);
assert_eq!(r.row(1), Rect::new(2, 2, 10, 1));
assert!(r.row(9).is_empty() && r.row(9).y == 5);
assert!(r.contains(2, 1) && r.contains(11, 4));
assert!(!r.contains(12, 1) && !r.contains(2, 5) && !r.contains(1, 1));
assert!(Rect::new(0, 0, 0, 3).is_empty() && !r.is_empty());
assert_eq!(split(10, 0, &[Fill(0), Fill(1)]), [0, 10]);
assert_eq!(split(10, 0, &[Fixed(2)]), [2]);
assert_eq!(split(10, 0, &[Fill(0)]), [0]);
}
#[test]
fn intersections_that_miss_stay_on_the_edge() {
let screen = Rect::new(0, 0, 10, 3);
assert_eq!(Rect::new(15, 2, 5, 1).intersect(screen), Rect::new(10, 2, 0, 1));
assert_eq!(Rect::new(2, 9, 4, 4).intersect(screen), Rect::new(2, 3, 4, 0));
}
}