Skip to content

Commit

Permalink
allowing the construction of a Rect from top-left & bottom-right corners
Browse files Browse the repository at this point in the history
  • Loading branch information
sminez committed Jun 25, 2024
1 parent 5d87a6b commit 78fbe94
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/pure/geometry.rs
Original file line number Diff line number Diff line change
@@ -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))]
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 78fbe94

Please sign in to comment.