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