Add centering of layouts.

This commit is contained in:
hal8174 2024-09-26 00:30:57 +02:00
parent b79d0dffb6
commit 907691685c
3 changed files with 101 additions and 17 deletions

View file

@ -325,7 +325,9 @@ impl Layout<'_> {
assert!(Self::place_block(problem, &mut blocks, rng));
Layout { problem, blocks }
let mut l = Layout { problem, blocks };
l.center();
l
}
fn place_block<R: Rng + ?Sized>(
@ -381,6 +383,28 @@ impl Layout<'_> {
false
}
pub fn get_aabb(&self) -> AABB {
self.blocks
.iter()
.map(|b| b.get_aabb())
.reduce(AABB::combine)
.expect("At least one block is required.")
}
pub fn center(&mut self) {
let aabb = self.get_aabb();
let rest = self.problem.size - aabb.size();
let new_min = Position::new(rest.x / 2, rest.y / 2);
let t = Transformation::new(Direction::Up, new_min - aabb.min());
for b in &mut self.blocks {
*b = b.transform(t);
}
}
/// Mutate existing layout, creating a valid layout
pub fn mutate<R: Rng + ?Sized>(&self, rng: &mut R) -> Self {
let mut s = self.clone();
@ -395,11 +419,12 @@ impl Layout<'_> {
loop {
let p = r.choose_weighted(rng, |i| i.1).unwrap();
if p.0(&mut s, rng) && rng.gen_bool(0.05) {
if p.0(&mut s, rng) && rng.gen_bool(0.2) {
break;
}
}
s.center();
s
}