utils: Add min/max to Coordinate
This commit is contained in:
parent
95b915d429
commit
2f683112a7
|
@ -29,6 +29,22 @@ pub trait Coordinate:
|
||||||
fn to_f64(self) -> f64;
|
fn to_f64(self) -> f64;
|
||||||
/// Convert to this coordinate from a f64
|
/// Convert to this coordinate from a f64
|
||||||
fn from_f64(v: f64) -> Self;
|
fn from_f64(v: f64) -> Self;
|
||||||
|
/// Compare and return the smaller one
|
||||||
|
fn min(self, other: Self) -> Self {
|
||||||
|
if self < other {
|
||||||
|
self
|
||||||
|
} else {
|
||||||
|
other
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// Compare and return the larger one
|
||||||
|
fn max(self, other: Self) -> Self {
|
||||||
|
if self > other {
|
||||||
|
self
|
||||||
|
} else {
|
||||||
|
other
|
||||||
|
}
|
||||||
|
}
|
||||||
/// Test if the coordinate is not negative
|
/// Test if the coordinate is not negative
|
||||||
fn non_negative(self) -> bool;
|
fn non_negative(self) -> bool;
|
||||||
/// Returns the absolute value of this coordinate
|
/// Returns the absolute value of this coordinate
|
||||||
|
@ -815,39 +831,12 @@ impl<N: Coordinate, Kind> Rectangle<N, Kind> {
|
||||||
|
|
||||||
/// Compute the bounding box of a given set of points
|
/// Compute the bounding box of a given set of points
|
||||||
pub fn bounding_box(points: impl IntoIterator<Item = Point<N, Kind>>) -> Self {
|
pub fn bounding_box(points: impl IntoIterator<Item = Point<N, Kind>>) -> Self {
|
||||||
let ret = points.into_iter().fold(None, |acc, point| {
|
let ret = points.into_iter().fold(None, |acc, point| match acc {
|
||||||
match acc {
|
None => Some((point, point)),
|
||||||
None => Some((point, point)),
|
Some((min_point, max_point)) => Some((
|
||||||
// we don't have cmp::{min,max} for f64 :(
|
(point.x.min(min_point.x), point.y.min(min_point.y)).into(),
|
||||||
Some((min_point, max_point)) => Some((
|
(point.x.max(max_point.x), point.y.max(max_point.y)).into(),
|
||||||
(
|
)),
|
||||||
if min_point.x > point.x {
|
|
||||||
point.x
|
|
||||||
} else {
|
|
||||||
min_point.x
|
|
||||||
},
|
|
||||||
if min_point.y > point.y {
|
|
||||||
point.y
|
|
||||||
} else {
|
|
||||||
min_point.y
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.into(),
|
|
||||||
(
|
|
||||||
if max_point.x < point.x {
|
|
||||||
point.x
|
|
||||||
} else {
|
|
||||||
max_point.x
|
|
||||||
},
|
|
||||||
if max_point.y < point.y {
|
|
||||||
point.y
|
|
||||||
} else {
|
|
||||||
max_point.y
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.into(),
|
|
||||||
)),
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
match ret {
|
match ret {
|
||||||
|
|
Loading…
Reference in New Issue