rust/land2d/src/lib.rs
author alfadur
Thu, 18 Oct 2018 07:27:14 +0300
changeset 13945 1c30793b1cea
parent 13943 1fa905aa4cdb
child 13948 a325ed57ebfe
permissions -rw-r--r--
put back land2d.map accidentally replaced by testing code
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13941
78c798d655ad Make use of LinePoints in Land2D::draw_line
unc0rr
parents: 13939
diff changeset
     1
extern crate integral_geometry;
13922
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
     2
extern crate vec2d;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
     3
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
     4
use std::cmp;
13939
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
     5
use std::ops;
13922
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
     6
13943
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13941
diff changeset
     7
use integral_geometry::{Point, LinePoints};
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13941
diff changeset
     8
13922
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
     9
pub struct Land2D<T> {
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    10
    pixels: vec2d::Vec2D<T>,
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    11
    width_mask: usize,
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    12
    height_mask: usize,
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    13
}
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    14
13936
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
    15
impl<T: Copy + PartialEq> Land2D<T> {
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
    16
    pub fn new(width: usize, height: usize, fill_value: T) -> Self {
13922
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    17
        assert!(width.is_power_of_two());
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    18
        assert!(height.is_power_of_two());
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    19
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    20
        Self {
13936
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
    21
            pixels: vec2d::Vec2D::new(width, height, fill_value),
13922
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    22
            width_mask: !(width - 1),
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    23
            height_mask: !(height - 1),
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    24
        }
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    25
    }
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    26
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    27
    #[inline]
13929
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
    28
    pub fn width(&self) -> usize {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
    29
        self.pixels.width()
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
    30
    }
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
    31
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
    32
    #[inline]
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
    33
    pub fn height(&self) -> usize {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
    34
        self.pixels.height()
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
    35
    }
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
    36
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
    37
    #[inline]
13936
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
    38
    pub fn is_valid_x(&self, x: i32) -> bool {
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
    39
        (x as usize & self.width_mask) == 0
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
    40
    }
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
    41
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
    42
    #[inline]
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
    43
    pub fn is_valid_y(&self, y: i32) -> bool {
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
    44
        (y as usize & self.height_mask) == 0
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
    45
    }
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
    46
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
    47
    #[inline]
13929
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
    48
    pub fn is_valid_coordinate(&self, x: i32, y: i32) -> bool {
13936
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
    49
        self.is_valid_x(x) && self.is_valid_y(y)
13922
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    50
    }
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    51
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    52
    #[inline]
13945
1c30793b1cea put back land2d.map accidentally replaced by testing code
alfadur
parents: 13943
diff changeset
    53
    pub fn map<U: Default, F: FnOnce(&mut T) -> U>(&mut self, y: i32, x: i32, f: F) -> U {
13922
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    54
        if self.is_valid_coordinate(x, y) {
13939
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
    55
            unsafe {
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
    56
                // hey, I just checked that coordinates are valid!
13945
1c30793b1cea put back land2d.map accidentally replaced by testing code
alfadur
parents: 13943
diff changeset
    57
                f(self.pixels.get_unchecked_mut(y as usize, x as usize))
13936
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
    58
            }
13939
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
    59
        } else {
13945
1c30793b1cea put back land2d.map accidentally replaced by testing code
alfadur
parents: 13943
diff changeset
    60
            U::default()
13922
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    61
        }
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    62
    }
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    63
13939
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
    64
    fn apply_along_line<U: Default + ops::AddAssign, F: FnMut(i32, i32) -> U>(
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
    65
        x1: i32,
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
    66
        y1: i32,
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
    67
        x2: i32,
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
    68
        y2: i32,
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
    69
        f: &mut F,
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
    70
    ) -> U {
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
    71
        let mut result = U::default();
13922
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    72
        let mut e_x: i32 = 0;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    73
        let mut e_y: i32 = 0;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    74
        let mut d_x: i32 = x2 - x1;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    75
        let mut d_y: i32 = y2 - y1;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    76
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    77
        let s_x: i32;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    78
        let s_y: i32;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    79
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    80
        if d_x > 0 {
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    81
            s_x = 1;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    82
        } else if d_x < 0 {
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    83
            s_x = -1;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    84
            d_x = -d_x;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    85
        } else {
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    86
            s_x = d_x;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    87
        }
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    88
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    89
        if d_y > 0 {
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    90
            s_y = 1;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    91
        } else if d_y < 0 {
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    92
            s_y = -1;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    93
            d_y = -d_y;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    94
        } else {
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    95
            s_y = d_y;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    96
        }
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    97
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    98
        let d = cmp::max(d_x, d_y);
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    99
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   100
        let mut x = x1;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   101
        let mut y = y1;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   102
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   103
        for _i in 0..=d {
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   104
            e_x += d_x;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   105
            e_y += d_y;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   106
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   107
            if e_x > d {
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   108
                e_x -= d;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   109
                x += s_x;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   110
            }
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   111
            if e_y > d {
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   112
                e_y -= d;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   113
                y += s_y;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   114
            }
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   115
13939
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   116
            result += f(x, y);
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   117
        }
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   118
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   119
        result
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   120
    }
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   121
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   122
    fn apply_around_circle<U: Default + ops::AddAssign, F: FnMut(i32, i32) -> U>(
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   123
        radius: i32,
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   124
        f: &mut F,
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   125
    ) -> U {
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   126
        let mut dx: i32 = 0;
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   127
        let mut dy: i32 = radius;
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   128
        let mut d = 3 - 2 * radius;
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   129
        let mut result = U::default();
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   130
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   131
        while dx < dy {
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   132
            result += f(dx, dy);
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   133
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   134
            if d < 0 {
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   135
                d += 4 * dx + 6;
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   136
            } else {
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   137
                d += 4 * (dx - dy) + 10;
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   138
                dy -= 1;
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   139
            }
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   140
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   141
            dx += 1;
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   142
        }
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   143
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   144
        if dx == dy {
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   145
            result += f(dx, dy);
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   146
        }
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   147
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   148
        result
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   149
    }
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   150
13943
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13941
diff changeset
   151
    pub fn fill_from_iter<I>(&mut self, i: I, value: T) -> usize
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13941
diff changeset
   152
        where I: std::iter::Iterator<Item = Point>
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13941
diff changeset
   153
    {
13945
1c30793b1cea put back land2d.map accidentally replaced by testing code
alfadur
parents: 13943
diff changeset
   154
        i.map(|p| self.map(p.y, p.x, |v| {*v = value; 1})).count()
13943
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13941
diff changeset
   155
    }
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13941
diff changeset
   156
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13941
diff changeset
   157
    pub fn draw_line(&mut self, from: Point, to: Point, value: T) -> usize {
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13941
diff changeset
   158
        self.fill_from_iter(LinePoints::new(from, to), value)
13922
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   159
    }
13929
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   160
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   161
    pub fn fill(&mut self, start_x: i32, start_y: i32, border_value: T, fill_value: T) {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   162
        debug_assert!(self.is_valid_coordinate(start_x - 1, start_y));
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   163
        debug_assert!(self.is_valid_coordinate(start_x, start_y));
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   164
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   165
        let mut stack: Vec<(usize, usize, usize, isize)> = Vec::new();
13936
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
   166
        fn push<T: Copy + PartialEq>(
13929
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   167
            land: &Land2D<T>,
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   168
            stack: &mut Vec<(usize, usize, usize, isize)>,
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   169
            xl: usize,
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   170
            xr: usize,
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   171
            y: usize,
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   172
            dir: isize,
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   173
        ) {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   174
            let yd = y as isize + dir;
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   175
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   176
            if land.is_valid_coordinate(0, yd as i32) {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   177
                stack.push((xl, xr, yd as usize, dir));
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   178
            }
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   179
        };
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   180
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   181
        let start_x_l = (start_x - 1) as usize;
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   182
        let start_x_r = start_x as usize;
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   183
        push(self, &mut stack, start_x_l, start_x_r, start_y as usize, -1);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   184
        push(self, &mut stack, start_x_l, start_x_r, start_y as usize, 1);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   185
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   186
        loop {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   187
            let a = stack.pop();
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   188
            match a {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   189
                None => return,
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   190
                Some(a) => {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   191
                    let (mut xl, mut xr, y, mut dir) = a;
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   192
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   193
                    while xl > 0 && self
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   194
                        .pixels
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   195
                        .get(y, xl)
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   196
                        .map_or(false, |v| *v != border_value && *v != fill_value)
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   197
                    {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   198
                        xl -= 1;
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   199
                    }
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   200
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   201
                    while xr < self.width() - 1 && self
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   202
                        .pixels
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   203
                        .get(y, xr)
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   204
                        .map_or(false, |v| *v != border_value && *v != fill_value)
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   205
                    {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   206
                        xr += 1;
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   207
                    }
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   208
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   209
                    while xl < xr {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   210
                        while xl <= xr
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   211
                            && (self.pixels[y][xl] == border_value
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   212
                                || self.pixels[y][xl] == fill_value)
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   213
                        {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   214
                            xl += 1;
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   215
                        }
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   216
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   217
                        let mut x = xl;
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   218
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   219
                        while xl <= xr
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   220
                            && (self.pixels[y][xl] != border_value
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   221
                                && self.pixels[y][xl] != fill_value)
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   222
                        {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   223
                            self.pixels[y][xl] = fill_value;
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   224
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   225
                            xl += 1;
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   226
                        }
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   227
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   228
                        if x < xl {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   229
                            push(self, &mut stack, x, xl - 1, y, dir);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   230
                            push(self, &mut stack, x, xl - 1, y, -dir);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   231
                        }
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   232
                    }
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   233
                }
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   234
            }
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   235
        }
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   236
    }
13936
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
   237
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
   238
    #[inline]
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
   239
    fn fill_circle_line<F: Fn(&mut T) -> usize>(
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
   240
        &mut self,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
   241
        y: i32,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
   242
        x_from: i32,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
   243
        x_to: i32,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
   244
        f: &F,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
   245
    ) -> usize {
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
   246
        let mut result = 0;
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
   247
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
   248
        if self.is_valid_y(y) {
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
   249
            for i in cmp::min(x_from, 0) as usize..cmp::max(x_to as usize, self.width() - 1) {
13939
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   250
                unsafe {
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   251
                    // coordinates are valid at this point
13936
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
   252
                    result += f(self.pixels.get_unchecked_mut(y as usize, i));
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
   253
                }
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
   254
            }
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
   255
        }
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
   256
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
   257
        result
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
   258
    }
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
   259
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
   260
    #[inline]
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
   261
    fn fill_circle_lines<F: Fn(&mut T) -> usize>(
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
   262
        &mut self,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
   263
        x: i32,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
   264
        y: i32,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
   265
        dx: i32,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
   266
        dy: i32,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
   267
        f: &F,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
   268
    ) -> usize {
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
   269
        self.fill_circle_line(y + dy, x - dx, x + dx, f)
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
   270
            + self.fill_circle_line(y - dy, x - dx, x + dx, f)
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
   271
            + self.fill_circle_line(y + dx, x - dy, x + dy, f)
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
   272
            + self.fill_circle_line(y - dx, x - dy, x + dy, f)
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
   273
    }
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
   274
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
   275
    pub fn change_round<F: Fn(&mut T) -> usize>(
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
   276
        &mut self,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
   277
        x: i32,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
   278
        y: i32,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
   279
        radius: i32,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
   280
        f: F,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
   281
    ) -> usize {
13939
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   282
        <Land2D<T>>::apply_around_circle(radius, &mut |dx, dy| {
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   283
            self.fill_circle_lines(x, y, dx, dy, &f)
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   284
        })
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   285
    }
13936
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
   286
13939
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   287
    #[inline]
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   288
    fn change_dots_around<U: Default + ops::AddAssign, F: FnMut(i32, i32) -> U>(
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   289
        x: i32,
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   290
        y: i32,
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   291
        xx: i32,
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   292
        yy: i32,
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   293
        f: &mut F,
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   294
    ) -> U {
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   295
        let mut result = U::default();
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   296
        result += f(y + yy, x + xx);
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   297
        result += f(y - yy, x + xx);
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   298
        result += f(y + yy, x - xx);
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   299
        result += f(y - yy, x - xx);
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   300
        result += f(y + xx, x + yy);
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   301
        result += f(y - xx, x + yy);
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   302
        result += f(y + xx, x - yy);
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   303
        result += f(y - xx, x - yy);
13936
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
   304
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
   305
        result
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
   306
    }
13939
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   307
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   308
    pub fn draw_thick_line(
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   309
        &mut self,
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   310
        x1: i32,
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   311
        y1: i32,
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   312
        x2: i32,
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   313
        y2: i32,
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   314
        radius: i32,
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   315
        value: T,
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   316
    ) -> usize {
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   317
        <Land2D<T>>::apply_around_circle(radius, &mut |dx, dy| {
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   318
            <Land2D<T>>::apply_along_line(x1, y1, x2, y2, &mut |x, y| {
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   319
                <Land2D<T>>::change_dots_around(x, y, dx, dy, &mut |x, y| {
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   320
                    self.map(x, y, |p| {
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   321
                        if *p != value {
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   322
                            *p = value;
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   323
                            1
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   324
                        } else {
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   325
                            0
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   326
                        }
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   327
                    })
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   328
                })
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   329
            })
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   330
        })
9c112f2ae02d Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents: 13936
diff changeset
   331
    }
13922
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   332
}
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   333
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   334
#[cfg(test)]
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   335
mod tests {
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   336
    use super::*;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   337
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   338
    #[test]
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   339
    fn basics() {
13936
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
   340
        let l: Land2D<u8> = Land2D::new(32, 64, 0);
13922
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   341
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   342
        assert!(l.is_valid_coordinate(0, 0));
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   343
        assert!(!l.is_valid_coordinate(-1, -1));
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   344
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   345
        assert!(l.is_valid_coordinate(31, 63));
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   346
        assert!(!l.is_valid_coordinate(32, 63));
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   347
        assert!(!l.is_valid_coordinate(31, 64));
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   348
    }
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   349
13929
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   350
    #[test]
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   351
    fn fill() {
13936
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13929
diff changeset
   352
        let mut l: Land2D<u8> = Land2D::new(128, 128, 0);
13929
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   353
13943
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13941
diff changeset
   354
        l.draw_line(Point::new(0, 0), Point::new(32, 96), 1);
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13941
diff changeset
   355
        l.draw_line(Point::new(32, 96), Point::new(64, 32), 1);
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13941
diff changeset
   356
        l.draw_line(Point::new(64, 32), Point::new(96, 80), 1);
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13941
diff changeset
   357
        l.draw_line(Point::new(96, 80), Point::new(128, 0), 1);
13929
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   358
13943
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13941
diff changeset
   359
        l.draw_line(Point::new(0, 128), Point::new(64, 96), 1);
1fa905aa4cdb move point struct into integral-geometry and use it to refactor a bit
alfadur
parents: 13941
diff changeset
   360
        l.draw_line(Point::new(128, 128), Point::new(64, 96), 1);
13929
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   361
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   362
        l.fill(32, 32, 1, 2);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   363
        l.fill(16, 96, 1, 3);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   364
        l.fill(60, 100, 1, 4);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   365
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   366
        assert_eq!(l.pixels[0][0], 1);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   367
        assert_eq!(l.pixels[96][64], 1);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   368
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   369
        assert_eq!(l.pixels[40][32], 2);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   370
        assert_eq!(l.pixels[40][96], 2);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   371
        assert_eq!(l.pixels[5][0], 3);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   372
        assert_eq!(l.pixels[120][0], 3);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   373
        assert_eq!(l.pixels[5][127], 3);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   374
        assert_eq!(l.pixels[120][127], 3);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   375
        assert_eq!(l.pixels[35][64], 3);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   376
        assert_eq!(l.pixels[120][20], 4);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   377
        assert_eq!(l.pixels[120][100], 4);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   378
        assert_eq!(l.pixels[100][64], 4);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   379
    }
13922
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   380
}