rust/integral-geometry/src/lib.rs
changeset 14078 bf40b5f938b0
parent 14077 5ade484f3351
child 14081 5d42204ac35e
equal deleted inserted replaced
14077:5ade484f3351 14078:bf40b5f938b0
   200             width,
   200             width,
   201             height,
   201             height,
   202         }
   202         }
   203     }
   203     }
   204 
   204 
       
   205     pub fn from_box(left: i32, right: i32, top: i32, bottom: i32) -> Self {
       
   206         assert!(left <= right);
       
   207         assert!(top <= bottom);
       
   208 
       
   209         Rect::new(left, top, (right - left) as u32, (bottom - top) as u32)
       
   210     }
       
   211 
       
   212     pub fn from_size(top_left: Point, size: Size) -> Self {
       
   213         Rect::new(top_left.x, top_left.y, size.width as u32, size.height as u32)
       
   214     }
       
   215 
   205     #[inline]
   216     #[inline]
   206     pub fn size(&self) -> Size {
   217     pub fn size(&self) -> Size {
   207         Size::new(self.width as usize, self.height as usize)
   218         Size::new(self.width as usize, self.height as usize)
   208     }
   219     }
   209 
   220 
   210     #[inline]
   221     #[inline]
   211     pub fn area(&self) -> usize {
   222     pub fn area(&self) -> usize {
   212         self.size().area()
   223         self.size().area()
       
   224     }
       
   225 
       
   226     #[inline]
       
   227     pub fn left(&self) -> i32 {
       
   228         self.x
       
   229     }
       
   230 
       
   231     #[inline]
       
   232     pub fn top(&self) -> i32 {
       
   233         self.y
       
   234     }
       
   235 
       
   236     #[inline]
       
   237     pub fn right(&self) -> i32 {
       
   238         self.x + self.width as i32
       
   239     }
       
   240 
       
   241     #[inline]
       
   242     pub fn bottom(&self) -> i32 {
       
   243         self.y + self.height as i32
       
   244     }
       
   245 
       
   246     #[inline]
       
   247     pub fn top_left(&self) -> Point {
       
   248         Point::new(self.x, self.y)
       
   249     }
       
   250 
       
   251     #[inline]
       
   252     pub fn with_margin(&self, margin: i32) -> Self {
       
   253         Rect::from_box(
       
   254             self.left() + margin,
       
   255             self.right() - margin,
       
   256             self.top() + margin,
       
   257             self.bottom() - margin,
       
   258         )
   213     }
   259     }
   214 }
   260 }
   215 
   261 
   216 #[derive(PartialEq, Eq, Clone, Copy, Debug)]
   262 #[derive(PartialEq, Eq, Clone, Copy, Debug)]
   217 pub struct Line {
   263 pub struct Line {
   438     fn line() {
   484     fn line() {
   439         let l = Line::new(Point::new(1, 1), Point::new(5, 6));
   485         let l = Line::new(Point::new(1, 1), Point::new(5, 6));
   440 
   486 
   441         assert_eq!(l.center(), Point::new(3, 3));
   487         assert_eq!(l.center(), Point::new(3, 3));
   442     }
   488     }
   443 }
   489 
       
   490     #[test]
       
   491     fn rect() {
       
   492         let r = Rect::from_box(10, 100, 0, 70);
       
   493 
       
   494         assert_eq!(r.top_left(), Point::new(10, 0));
       
   495         assert_eq!(r.with_margin(12), Rect::from_box(22, 88, 12, 58));
       
   496     }
       
   497 }