rust/integral-geometry/src/lib.rs
changeset 14081 5d42204ac35e
parent 14078 bf40b5f938b0
child 14088 f483f844da98
equal deleted inserted replaced
14080:af203fb307a7 14081:5d42204ac35e
       
     1 extern crate fpnum;
       
     2 
       
     3 use fpnum::distance;
     1 use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
     4 use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
     2 
     5 
     3 #[derive(PartialEq, Eq, Clone, Copy, Debug)]
     6 #[derive(PartialEq, Eq, Clone, Copy, Debug)]
     4 pub struct Point {
     7 pub struct Point {
     5     pub x: i32,
     8     pub x: i32,
    33     }
    36     }
    34 
    37 
    35     #[inline]
    38     #[inline]
    36     pub fn max_norm(self) -> i32 {
    39     pub fn max_norm(self) -> i32 {
    37         std::cmp::max(self.x.abs(), self.y.abs())
    40         std::cmp::max(self.x.abs(), self.y.abs())
       
    41     }
       
    42 
       
    43     #[inline]
       
    44     pub fn integral_norm(self) -> u32 {
       
    45         distance(self.x, self.y).abs_round()
    38     }
    46     }
    39 
    47 
    40     #[inline]
    48     #[inline]
    41     pub fn transform(self, matrix: &[i32; 4]) -> Self {
    49     pub fn transform(self, matrix: &[i32; 4]) -> Self {
    42         Point::new(
    50         Point::new(
   208 
   216 
   209         Rect::new(left, top, (right - left) as u32, (bottom - top) as u32)
   217         Rect::new(left, top, (right - left) as u32, (bottom - top) as u32)
   210     }
   218     }
   211 
   219 
   212     pub fn from_size(top_left: Point, size: Size) -> Self {
   220     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)
   221         Rect::new(
       
   222             top_left.x,
       
   223             top_left.y,
       
   224             size.width as u32,
       
   225             size.height as u32,
       
   226         )
   214     }
   227     }
   215 
   228 
   216     #[inline]
   229     #[inline]
   217     pub fn size(&self) -> Size {
   230     pub fn size(&self) -> Size {
   218         Size::new(self.width as usize, self.height as usize)
   231         Size::new(self.width as usize, self.height as usize)
   254             self.left() + margin,
   267             self.left() + margin,
   255             self.right() - margin,
   268             self.right() - margin,
   256             self.top() + margin,
   269             self.top() + margin,
   257             self.bottom() - margin,
   270             self.bottom() - margin,
   258         )
   271         )
       
   272     }
       
   273 
       
   274     #[inline]
       
   275     pub fn contains_inside(&self, point: Point) -> bool {
       
   276         point.x > self.left()
       
   277             && point.x < self.right()
       
   278             && point.y > self.top()
       
   279             && point.y < self.bottom()
   259     }
   280     }
   260 }
   281 }
   261 
   282 
   262 #[derive(PartialEq, Eq, Clone, Copy, Debug)]
   283 #[derive(PartialEq, Eq, Clone, Copy, Debug)]
   263 pub struct Line {
   284 pub struct Line {
   489 
   510 
   490     #[test]
   511     #[test]
   491     fn rect() {
   512     fn rect() {
   492         let r = Rect::from_box(10, 100, 0, 70);
   513         let r = Rect::from_box(10, 100, 0, 70);
   493 
   514 
       
   515         assert!(r.contains_inside(Point::new(99, 69)));
       
   516         assert!(!r.contains_inside(Point::new(100, 70)));
       
   517 
   494         assert_eq!(r.top_left(), Point::new(10, 0));
   518         assert_eq!(r.top_left(), Point::new(10, 0));
   495         assert_eq!(r.with_margin(12), Rect::from_box(22, 88, 12, 58));
   519         assert_eq!(r.with_margin(12), Rect::from_box(22, 88, 12, 58));
   496     }
   520     }
   497 }
   521 }