rust/integral-geometry/src/lib.rs
changeset 14125 376a0551b00a
parent 14124 6e0be42d0a8f
child 14126 32383b888309
equal deleted inserted replaced
14124:6e0be42d0a8f 14125:376a0551b00a
     1 extern crate fpnum;
     1 extern crate fpnum;
     2 
     2 
     3 use fpnum::distance;
     3 use fpnum::distance;
     4 use std::ops::{
     4 use std::cmp::max;
     5     Add, AddAssign,
     5 use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, RangeInclusive, Sub, SubAssign};
     6     Div, DivAssign,
       
     7     Mul, MulAssign,
       
     8     Sub, SubAssign,
       
     9     RangeInclusive
       
    10 };
       
    11 
     6 
    12 #[derive(PartialEq, Eq, Clone, Copy, Debug)]
     7 #[derive(PartialEq, Eq, Clone, Copy, Debug)]
    13 pub struct Point {
     8 pub struct Point {
    14     pub x: i32,
     9     pub x: i32,
    15     pub y: i32,
    10     pub y: i32,
    66 
    61 
    67     #[inline]
    62     #[inline]
    68     pub fn cross(self, other: Point) -> i32 {
    63     pub fn cross(self, other: Point) -> i32 {
    69         self.dot(other.rotate90())
    64         self.dot(other.rotate90())
    70     }
    65     }
       
    66 
       
    67     #[inline]
       
    68     pub fn fit(&self, rect: &Rect) -> Point {
       
    69         let x = if self.x > rect.right() {
       
    70             rect.right()
       
    71         } else if self.x < rect.left() {
       
    72             rect.left()
       
    73         } else {
       
    74             self.x
       
    75         };
       
    76         let y = if self.y > rect.bottom() {
       
    77             rect.bottom()
       
    78         } else if self.y < rect.top() {
       
    79             rect.top()
       
    80         } else {
       
    81             self.y
       
    82         };
       
    83 
       
    84         Point::new(x, y)
       
    85     }
    71 }
    86 }
    72 
    87 
    73 #[derive(PartialEq, Eq, Clone, Copy, Debug)]
    88 #[derive(PartialEq, Eq, Clone, Copy, Debug)]
    74 pub struct Size {
    89 pub struct Size {
    75     pub width: usize,
    90     pub width: usize,
   114     }
   129     }
   115 
   130 
   116     #[inline]
   131     #[inline]
   117     pub fn to_mask(&self) -> SizeMask {
   132     pub fn to_mask(&self) -> SizeMask {
   118         SizeMask::new(*self)
   133         SizeMask::new(*self)
       
   134     }
       
   135 
       
   136     #[inline]
       
   137     pub fn to_square(&self) -> Size {
       
   138         Size::square(max(self.width, self.height))
   119     }
   139     }
   120 
   140 
   121     pub fn to_grid_index(&self) -> GridIndex {
   141     pub fn to_grid_index(&self) -> GridIndex {
   122         GridIndex::new(*self)
   142         GridIndex::new(*self)
   123     }
   143     }
   353         assert!(self.contains_inside(point));
   373         assert!(self.contains_inside(point));
   354         [
   374         [
   355             Rect::from_box(self.left(), point.x, self.top(), point.y),
   375             Rect::from_box(self.left(), point.x, self.top(), point.y),
   356             Rect::from_box(point.x, self.right(), self.top(), point.y),
   376             Rect::from_box(point.x, self.right(), self.top(), point.y),
   357             Rect::from_box(point.x, self.right(), point.y, self.bottom()),
   377             Rect::from_box(point.x, self.right(), point.y, self.bottom()),
   358             Rect::from_box(self.left(), point.x, point.y, self.bottom())
   378             Rect::from_box(self.left(), point.x, point.y, self.bottom()),
   359         ]
   379         ]
   360     }
   380     }
   361 }
   381 }
   362 
   382 
   363 pub struct Polygon {
   383 pub struct Polygon {
   364     vertices: Vec<Point>
   384     vertices: Vec<Point>,
   365 }
   385 }
   366 
   386 
   367 impl Polygon {
   387 impl Polygon {
   368     pub fn new(vertices: &[Point]) -> Self {
   388     pub fn new(vertices: &[Point]) -> Self {
   369         let mut v = Vec::with_capacity(vertices.len() + 1);
   389         let mut v = Vec::with_capacity(vertices.len() + 1);
   655         assert!(!r.contains_inside(Point::new(100, 70)));
   675         assert!(!r.contains_inside(Point::new(100, 70)));
   656 
   676 
   657         assert_eq!(r.top_left(), Point::new(10, 0));
   677         assert_eq!(r.top_left(), Point::new(10, 0));
   658         assert_eq!(r.with_margin(12), Rect::from_box(22, 88, 12, 58));
   678         assert_eq!(r.with_margin(12), Rect::from_box(22, 88, 12, 58));
   659     }
   679     }
   660 }
   680 
       
   681     #[test]
       
   682     fn fit() {
       
   683         let r = Rect::from_box(10, 100, 0, 70);
       
   684 
       
   685         assert_eq!(Point::new(0, -10).fit(&r), Point::new(10, 0));
       
   686         assert_eq!(Point::new(1000, 1000).fit(&r), Point::new(100, 70));
       
   687     }
       
   688 }