Skip to content

Commit 6dbb8ef

Browse files
authored
Merge pull request #104 from derekdreery/rect_from_center_size
Helper to construct Rect from center and size.
2 parents 4648ab8 + a6f7939 commit 6dbb8ef

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/rect.rs

+23
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,19 @@ impl Rect {
4949
Rect::from_points(origin, origin + size.into().to_vec2())
5050
}
5151

52+
/// A new rectangle from center and size.
53+
#[inline]
54+
pub fn from_center_size(center: impl Into<Point>, size: impl Into<Size>) -> Rect {
55+
let center = center.into();
56+
let size = 0.5 * size.into();
57+
Rect::new(
58+
center.x - size.width,
59+
center.y - size.height,
60+
center.x + size.width,
61+
center.y + size.height,
62+
)
63+
}
64+
5265
/// Create a new `Rect` with the same size as `self` and a new origin.
5366
#[inline]
5467
pub fn with_origin(self, origin: impl Into<Point>) -> Rect {
@@ -531,4 +544,14 @@ mod tests {
531544
);
532545
assert_eq!(format!("{:.2}", r), "Rect { (10.00, 12.23) (22.22×23.10) }");
533546
}
547+
548+
/* TODO uncomment when a (possibly approximate) equality test has been decided on
549+
#[test]
550+
fn rect_from_center_size() {
551+
assert_eq!(
552+
Rect::from_center_size(Point::new(3.0, 2.0), Size::new(2.0, 4.0)),
553+
Rect::new(2.0, 0.0, 4.0, 4.0)
554+
);
555+
}
556+
*/
534557
}

0 commit comments

Comments
 (0)