Sort using more deterministic algorithm

`hypot` while useful for most of the cases, may break sorting
assumptions like deterministic comparison.
As seen in: https://github.com/pop-os/cosmic-launcher/issues/352

Add and use a new method that trades the performance for more
determinism.
This commit is contained in:
Mateusz Mikuła 2026-01-12 23:38:03 +01:00 committed by Ashley Wulber
parent fc6b46342b
commit 044496f652
2 changed files with 14 additions and 2 deletions

View file

@ -34,6 +34,18 @@ impl<T: Num> Point<T> {
a.hypot(b) a.hypot(b)
} }
/// Computes the distance to another [`Point`], using a slower but more deterministic algorithm.
/// Useful for sorting algorithms.
pub fn distance_slow(&self, to: Self) -> T
where
T: Float,
{
let a = self.x - to.x;
let b = self.y - to.y;
(a * a + b * b).sqrt()
}
} }
impl<T> From<[T; 2]> for Point<T> impl<T> From<[T; 2]> for Point<T>

View file

@ -49,8 +49,8 @@ pub fn group(mut damage: Vec<Rectangle>, bounds: Rectangle) -> Vec<Rectangle> {
damage.sort_by(|a, b| { damage.sort_by(|a, b| {
a.center() a.center()
.distance(Point::ORIGIN) .distance_slow(Point::ORIGIN)
.total_cmp(&b.center().distance(Point::ORIGIN)) .total_cmp(&b.center().distance_slow(Point::ORIGIN))
}); });
let mut output = Vec::new(); let mut output = Vec::new();