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:
parent
fc6b46342b
commit
044496f652
2 changed files with 14 additions and 2 deletions
|
|
@ -34,6 +34,18 @@ impl<T: Num> Point<T> {
|
|||
|
||||
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>
|
||||
|
|
|
|||
|
|
@ -49,8 +49,8 @@ pub fn group(mut damage: Vec<Rectangle>, bounds: Rectangle) -> Vec<Rectangle> {
|
|||
|
||||
damage.sort_by(|a, b| {
|
||||
a.center()
|
||||
.distance(Point::ORIGIN)
|
||||
.total_cmp(&b.center().distance(Point::ORIGIN))
|
||||
.distance_slow(Point::ORIGIN)
|
||||
.total_cmp(&b.center().distance_slow(Point::ORIGIN))
|
||||
});
|
||||
|
||||
let mut output = Vec::new();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue