rust/land2d/src/lib.rs
changeset 14076 e5904ead4864
parent 14052 9c817b2eedae
child 14078 bf40b5f938b0
equal deleted inserted replaced
14075:df0e86b2630f 14076:e5904ead4864
     2 extern crate vec2d;
     2 extern crate vec2d;
     3 
     3 
     4 use std::cmp;
     4 use std::cmp;
     5 
     5 
     6 use integral_geometry::{
     6 use integral_geometry::{
     7     ArcPoints, EquidistantPoints, LinePoints,
     7     ArcPoints, EquidistantPoints,
     8     Point, Size, SizeMask
     8     Point, Size, SizeMask, Line
     9 };
     9 };
    10 
    10 
    11 pub struct Land2D<T> {
    11 pub struct Land2D<T> {
    12     pixels: vec2d::Vec2D<T>,
    12     pixels: vec2d::Vec2D<T>,
    13     play_size: Size,
    13     play_size: Size,
   101                 1
   101                 1
   102             })
   102             })
   103         }).count()
   103         }).count()
   104     }
   104     }
   105 
   105 
   106     pub fn draw_line(&mut self, from: Point, to: Point, value: T) -> usize {
   106     pub fn draw_line(&mut self, line: Line, value: T) -> usize {
   107         self.fill_from_iter(LinePoints::new(from, to), value)
   107         self.fill_from_iter(line.into_iter(), value)
   108     }
   108     }
   109 
   109 
   110     pub fn fill(&mut self, start_point: Point, border_value: T, fill_value: T) {
   110     pub fn fill(&mut self, start_point: Point, border_value: T, fill_value: T) {
   111         debug_assert!(self.is_valid_coordinate(start_point.x - 1, start_point.y));
   111         debug_assert!(self.is_valid_coordinate(start_point.x - 1, start_point.y));
   112         debug_assert!(self.is_valid_coordinate(start_point.x, start_point.y));
   112         debug_assert!(self.is_valid_coordinate(start_point.x, start_point.y));
   261                 self.fill_row(center, vector.transform(m), value)
   261                 self.fill_row(center, vector.transform(m), value)
   262             ).sum::<usize>()
   262             ).sum::<usize>()
   263         }).sum()
   263         }).sum()
   264     }
   264     }
   265 
   265 
   266     pub fn draw_thick_line(&mut self, from: Point, to: Point, radius: i32, value: T) -> usize {
   266     pub fn draw_thick_line(&mut self, line: Line, radius: i32, value: T) -> usize {
   267         let mut result = 0;
   267         let mut result = 0;
   268 
   268 
   269         for vector in ArcPoints::new(radius) {
   269         for vector in ArcPoints::new(radius) {
   270             for delta in EquidistantPoints::new(vector) {
   270             for delta in EquidistantPoints::new(vector) {
   271                 for point in LinePoints::new(from, to) {
   271                 for point in line.into_iter() {
   272                     self.map_point(point + delta, |p| {
   272                     self.map_point(point + delta, |p| {
   273                         if *p != value {
   273                         if *p != value {
   274                             *p = value;
   274                             *p = value;
   275                             result += 1;
   275                             result += 1;
   276                         }
   276                         }
   306 
   306 
   307     #[test]
   307     #[test]
   308     fn fill() {
   308     fn fill() {
   309         let mut l: Land2D<u8> = Land2D::new(Size::square(128), 0);
   309         let mut l: Land2D<u8> = Land2D::new(Size::square(128), 0);
   310 
   310 
   311         l.draw_line(Point::new(0, 0), Point::new(32, 96), 1);
   311         l.draw_line(Line::new(Point::new(0, 0), Point::new(32, 96)), 1);
   312         l.draw_line(Point::new(32, 96), Point::new(64, 32), 1);
   312         l.draw_line(Line::new(Point::new(32, 96), Point::new(64, 32)), 1);
   313         l.draw_line(Point::new(64, 32), Point::new(96, 80), 1);
   313         l.draw_line(Line::new(Point::new(64, 32), Point::new(96, 80)), 1);
   314         l.draw_line(Point::new(96, 80), Point::new(128, 0), 1);
   314         l.draw_line(Line::new(Point::new(96, 80), Point::new(128, 0)), 1);
   315 
   315 
   316         l.draw_line(Point::new(0, 128), Point::new(64, 96), 1);
   316         l.draw_line(Line::new(Point::new(0, 128), Point::new(64, 96)), 1);
   317         l.draw_line(Point::new(128, 128), Point::new(64, 96), 1);
   317         l.draw_line(Line::new(Point::new(128, 128), Point::new(64, 96)), 1);
   318 
   318 
   319         l.fill(Point::new(32, 32), 1, 2);
   319         l.fill(Point::new(32, 32), 1, 2);
   320         l.fill(Point::new(16, 96), 1, 3);
   320         l.fill(Point::new(16, 96), 1, 3);
   321         l.fill(Point::new(60, 100), 1, 4);
   321         l.fill(Point::new(60, 100), 1, 4);
   322 
   322