use glam::Vec2;
use super::Direction;
pub(crate) type Rect = (Vec2, Vec2);
pub const MAJOR_AXIS_WEIGHT: f32 = 13.0;
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Score {
pub in_beam: bool,
pub distance: f32,
}
impl Score {
#[must_use]
pub fn beats(self, other: Self) -> bool {
match (self.in_beam, other.in_beam) {
(true, false) => true,
(false, true) => false,
_ => self.distance < other.distance,
}
}
}
fn project((min, max): Rect, direction: Direction) -> (f32, f32, f32, f32) {
match direction {
Direction::Right => (min.x, max.x, min.y, max.y),
Direction::Left => (-max.x, -min.x, min.y, max.y),
Direction::Down => (min.y, max.y, min.x, max.x),
Direction::Up => (-max.y, -min.y, min.x, max.x),
}
}
#[must_use]
pub fn score(from: Rect, to: Rect, direction: Direction) -> Option<Score> {
let (from_lead, from_trail, from_cross_min, from_cross_max) = project(from, direction);
let (to_lead, to_trail, to_cross_min, to_cross_max) = project(to, direction);
let ahead = (from_lead < to_lead || from_trail <= to_lead) && from_trail < to_trail;
if !ahead {
return None;
}
let in_beam = to_cross_min < from_cross_max && to_cross_max > from_cross_min;
let major = (to_lead - from_trail).max(0.0);
let minor = ((from_cross_min + from_cross_max) - (to_cross_min + to_cross_max)).abs() * 0.5;
Some(Score {
in_beam,
distance: MAJOR_AXIS_WEIGHT * major * major + minor * minor,
})
}
pub fn pick<K: Copy>(
from: Rect,
candidates: impl IntoIterator<Item = (K, Rect)>,
direction: Direction,
) -> (Option<K>, Vec<(K, Score)>) {
let mut best: Option<(K, Score)> = None;
let mut scored = Vec::new();
for (key, rect) in candidates {
let Some(score) = score(from, rect, direction) else {
continue;
};
scored.push((key, score));
if best.is_none_or(|(_, held)| score.beats(held)) {
best = Some((key, score));
}
}
(best.map(|(key, _)| key), scored)
}
#[cfg(test)]
mod tests {
use super::*;
fn rect(x: f32, y: f32, width: f32, height: f32) -> Rect {
(Vec2::new(x, y), Vec2::new(x + width, y + height))
}
fn pick_named(
from: Rect,
layout: &[(&'static str, Rect)],
direction: Direction,
) -> Option<&'static str> {
pick(from, layout.iter().copied(), direction).0
}
fn grid() -> Vec<(&'static str, Rect)> {
const NAMES: [[&str; 3]; 3] = [["a0", "a1", "a2"], ["b0", "b1", "b2"], ["c0", "c1", "c2"]];
let mut cells = Vec::new();
for (row, names) in NAMES.iter().enumerate() {
for (column, name) in names.iter().enumerate() {
cells.push((
*name,
rect(column as f32 * 50.0, row as f32 * 30.0, 40.0, 20.0),
));
}
}
cells
}
#[test]
fn a_grid_moves_to_the_adjacent_cell_and_stops_at_its_edges() {
use Direction::{Down, Left, Right, Up};
let cells = grid();
let table: &[(&str, [Option<&str>; 4])] = &[
("a0", [None, Some("a1"), Some("b0"), None]),
("a1", [None, Some("a2"), Some("b1"), Some("a0")]),
("a2", [None, None, Some("b2"), Some("a1")]),
("b0", [Some("a0"), Some("b1"), Some("c0"), None]),
("b1", [Some("a1"), Some("b2"), Some("c1"), Some("b0")]),
("b2", [Some("a2"), None, Some("c2"), Some("b1")]),
("c0", [Some("b0"), Some("c1"), None, None]),
("c1", [Some("b1"), Some("c2"), None, Some("c0")]),
("c2", [Some("b2"), None, None, Some("c1")]),
];
for (from, want) in table {
let origin = cells
.iter()
.find(|(name, _)| name == from)
.expect("named")
.1;
let others: Vec<_> = cells
.iter()
.copied()
.filter(|(name, _)| name != from)
.collect();
for (direction, want) in [Up, Right, Down, Left].into_iter().zip(want) {
assert_eq!(
pick_named(origin, &others, direction),
*want,
"{from} moving {direction:?}"
);
}
}
}
#[test]
fn a_tall_neighbour_does_not_steal_a_vertical_move_from_an_aligned_one() {
let current = rect(0.0, 0.0, 100.0, 40.0);
let layout = [
("tall", rect(110.0, 20.0, 100.0, 300.0)),
("aligned", rect(0.0, 100.0, 100.0, 40.0)),
];
let (_, scored) = pick(current, layout, Direction::Down);
let tall = scored[0].1;
let aligned = scored[1].1;
assert!(
tall.distance < aligned.distance,
"the fixture no longer has the shape: the tall column must be the nearer by distance \
({tall:?} against {aligned:?})"
);
assert!(!tall.in_beam && aligned.in_beam);
assert_eq!(
pick_named(current, &layout, Direction::Down),
Some("aligned")
);
}
#[test]
fn a_candidate_in_the_beam_beats_a_nearer_one_outside_it() {
let current = rect(100.0, 100.0, 40.0, 20.0);
type Case = (Direction, [(&'static str, Rect); 3], &'static str);
let cases: [Case; 4] = [
(
Direction::Right,
[
("near-off", rect(150.0, 125.0, 40.0, 20.0)),
("far-beam", rect(400.0, 110.0, 40.0, 20.0)),
("farther-beam", rect(500.0, 100.0, 40.0, 20.0)),
],
"far-beam",
),
(
Direction::Left,
[
("near-off", rect(50.0, 80.0, 40.0, 19.0)),
("farther-beam", rect(-300.0, 100.0, 40.0, 20.0)),
("far-beam", rect(-100.0, 119.0, 40.0, 20.0)),
],
"far-beam",
),
(
Direction::Down,
[
("near-off", rect(141.0, 121.0, 40.0, 20.0)),
("far-beam", rect(139.0, 300.0, 40.0, 20.0)),
("farther-beam", rect(100.0, 301.0, 40.0, 20.0)),
],
"far-beam",
),
(
Direction::Up,
[
("near-off", rect(40.0, 70.0, 59.0, 20.0)),
("farther-beam", rect(100.0, -200.0, 40.0, 20.0)),
("far-beam", rect(120.0, -100.0, 40.0, 20.0)),
],
"far-beam",
),
];
for (direction, layout, want) in cases {
assert_eq!(
pick_named(current, &layout, direction),
Some(want),
"moving {direction:?}"
);
}
}
#[test]
fn overlap_containment_and_ties_are_decided_by_the_edges_and_the_order() {
let current = rect(100.0, 100.0, 40.0, 20.0);
let container = rect(0.0, 0.0, 400.0, 400.0);
for direction in Direction::ALL {
assert_eq!(score(current, container, direction), None, "{direction:?}");
}
let reaching = rect(120.0, 100.0, 40.0, 20.0);
let overlap = score(current, reaching, Direction::Right).expect("reaches further right");
assert_eq!(
overlap,
Score {
in_beam: true,
distance: 0.0
}
);
assert_eq!(score(current, reaching, Direction::Left), None);
let level = rect(100.0, 200.0, 40.0, 20.0);
assert_eq!(score(current, level, Direction::Right), None);
let layout = [
("above", rect(200.0, 90.0, 40.0, 20.0)),
("below", rect(200.0, 110.0, 40.0, 20.0)),
];
assert_eq!(
pick_named(current, &layout, Direction::Right),
Some("above")
);
let reversed = [layout[1], layout[0]];
assert_eq!(
pick_named(current, &reversed, Direction::Right),
Some("below")
);
}
#[test]
fn the_distance_is_thirteen_major_squared_plus_minor_squared() {
let current = rect(0.0, 0.0, 10.0, 10.0);
let to = rect(30.0, 3.0, 10.0, 10.0);
assert_eq!(
score(current, to, Direction::Right),
Some(Score {
in_beam: true,
distance: 5209.0
})
);
let to = rect(12.0, -15.0, 10.0, 10.0);
assert_eq!(
score(current, to, Direction::Up),
Some(Score {
in_beam: false,
distance: 469.0
})
);
}
}