rust/integral-geometry/src/lib.rs
changeset 14717 16024046d458
parent 14627 2e2b31cf0871
child 14725 19d30d96d7d6
equal deleted inserted replaced
14716:8e74d4eb89f5 14717:16024046d458
   107     pub width: usize,
   107     pub width: usize,
   108     pub height: usize,
   108     pub height: usize,
   109 }
   109 }
   110 
   110 
   111 impl Size {
   111 impl Size {
       
   112     pub const EMPTY: Self = Self::square(0);
       
   113 
   112     #[inline]
   114     #[inline]
   113     pub const fn new(width: usize, height: usize) -> Self {
   115     pub const fn new(width: usize, height: usize) -> Self {
   114         Self { width, height }
   116         Self { width, height }
   115     }
   117     }
   116 
   118 
   160         Self::square(max(self.width, self.height))
   162         Self::square(max(self.width, self.height))
   161     }
   163     }
   162 
   164 
   163     pub fn to_grid_index(&self) -> GridIndex {
   165     pub fn to_grid_index(&self) -> GridIndex {
   164         GridIndex::new(*self)
   166         GridIndex::new(*self)
       
   167     }
       
   168 
       
   169     #[inline]
       
   170     pub fn contains(&self, other: Self) -> bool {
       
   171         self.width >= other.width && self.height >= other.height
   165     }
   172     }
   166 }
   173 }
   167 
   174 
   168 #[derive(PartialEq, Eq, Clone, Copy, Debug)]
   175 #[derive(PartialEq, Eq, Clone, Copy, Debug)]
   169 pub struct SizeMask {
   176 pub struct SizeMask {
   300     top_left: Point,
   307     top_left: Point,
   301     bottom_right: Point,
   308     bottom_right: Point,
   302 }
   309 }
   303 
   310 
   304 impl Rect {
   311 impl Rect {
       
   312     pub const EMPTY: Self = Self {
       
   313         top_left: Point::ZERO,
       
   314         bottom_right: Point::ZERO,
       
   315     };
       
   316 
   305     #[inline]
   317     #[inline]
   306     pub fn new(top_left: Point, bottom_right: Point) -> Self {
   318     pub fn new(top_left: Point, bottom_right: Point) -> Self {
   307         debug_assert!(top_left.x <= bottom_right.x + 1);
   319         debug_assert!(top_left.x <= bottom_right.x + 1);
   308         debug_assert!(top_left.y <= bottom_right.y + 1);
   320         debug_assert!(top_left.y <= bottom_right.y + 1);
   309         Self {
   321         Self {
   411     pub fn contains_inside(&self, point: Point) -> bool {
   423     pub fn contains_inside(&self, point: Point) -> bool {
   412         point.x > self.left()
   424         point.x > self.left()
   413             && point.x < self.right()
   425             && point.x < self.right()
   414             && point.y > self.top()
   426             && point.y > self.top()
   415             && point.y < self.bottom()
   427             && point.y < self.bottom()
       
   428     }
       
   429 
       
   430     #[inline]
       
   431     pub fn contains_rect(&self, other: &Self) -> bool {
       
   432         self.contains(other.top_left()) && self.contains(other.bottom_right())
   416     }
   433     }
   417 
   434 
   418     #[inline]
   435     #[inline]
   419     pub fn intersects(&self, other: &Rect) -> bool {
   436     pub fn intersects(&self, other: &Rect) -> bool {
   420         self.left() <= self.right()
   437         self.left() <= self.right()
   430             Self::from_box(self.left(), point.x, self.top(), point.y),
   447             Self::from_box(self.left(), point.x, self.top(), point.y),
   431             Self::from_box(point.x, self.right(), self.top(), point.y),
   448             Self::from_box(point.x, self.right(), self.top(), point.y),
   432             Self::from_box(point.x, self.right(), point.y, self.bottom()),
   449             Self::from_box(point.x, self.right(), point.y, self.bottom()),
   433             Self::from_box(self.left(), point.x, point.y, self.bottom()),
   450             Self::from_box(self.left(), point.x, point.y, self.bottom()),
   434         ]
   451         ]
       
   452     }
       
   453 
       
   454     #[inline]
       
   455     pub fn with_margins(&self, left: i32, right: i32, top: i32, bottom: i32) -> Self {
       
   456         Self::from_box(
       
   457             self.left() + left,
       
   458             self.right() + right,
       
   459             self.top() + top,
       
   460             self.bottom() + bottom,
       
   461         )
   435     }
   462     }
   436 
   463 
   437     #[inline]
   464     #[inline]
   438     pub fn quotient(self, x: usize, y: usize) -> Point {
   465     pub fn quotient(self, x: usize, y: usize) -> Point {
   439         self.top_left() + Point::new((x % self.width()) as i32, (y % self.height()) as i32)
   466         self.top_left() + Point::new((x % self.width()) as i32, (y % self.height()) as i32)