From 78fbe9426b0eef27bd9677ba1d947cd357e26ca6 Mon Sep 17 00:00:00 2001 From: Innes Anderson-Morrison Date: Tue, 25 Jun 2024 16:30:08 +0100 Subject: [PATCH] allowing the construction of a Rect from top-left & bottom-right corners --- src/pure/geometry.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/pure/geometry.rs b/src/pure/geometry.rs index f8190d89..625dd1d0 100644 --- a/src/pure/geometry.rs +++ b/src/pure/geometry.rs @@ -1,7 +1,7 @@ //! Geometry primitives #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; -use std::cmp::max; +use std::cmp::{max, min}; /// An x,y coordinate pair #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] @@ -149,6 +149,15 @@ pub struct Rect { pub h: u32, } +impl From<(Point, Point)> for Rect { + fn from((p1, p2): (Point, Point)) -> Self { + let (x1, x2) = (min(p1.x, p2.x), max(p1.x, p2.x)); + let (y1, y2) = (min(p1.y, p2.y), max(p1.y, p2.y)); + + Rect::new(x1, y1, x2 - x1, y2 - y1) + } +} + impl Rect { /// Create a new Rect. pub const fn new(x: u32, y: u32, w: u32, h: u32) -> Rect {