rust/land2d/src/lib.rs
author unc0rr
Mon, 15 Oct 2018 23:10:03 +0200
changeset 13929 a140f28decc4
parent 13922 a83ba9ba1566
child 13936 9230aed8a32e
permissions -rw-r--r--
Implement Land2D::fill() + tests
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13922
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
     1
extern crate vec2d;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
     2
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
     3
use std::cmp;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
     4
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
     5
pub struct Land2D<T> {
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
     6
    pixels: vec2d::Vec2D<T>,
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
     7
    width_mask: usize,
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
     8
    height_mask: usize,
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
     9
}
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    10
13929
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
    11
impl<T: Default + Copy + PartialEq> Land2D<T> {
13922
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    12
    pub fn new(width: usize, height: usize) -> Self {
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    13
        assert!(width.is_power_of_two());
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    14
        assert!(height.is_power_of_two());
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    15
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    16
        Self {
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    17
            pixels: vec2d::Vec2D::new(width, height, T::default()),
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    18
            width_mask: !(width - 1),
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    19
            height_mask: !(height - 1),
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    20
        }
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    21
    }
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
    #[inline]
13929
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
    24
    pub fn width(&self) -> usize {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
    25
        self.pixels.width()
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
    26
    }
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
    27
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
    28
    #[inline]
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
    29
    pub fn height(&self) -> usize {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
    30
        self.pixels.height()
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
    31
    }
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
    32
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
    33
    #[inline]
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
    34
    pub fn is_valid_coordinate(&self, x: i32, y: i32) -> bool {
13922
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    35
        (x as usize & self.width_mask) == 0 && (y as usize & self.height_mask) == 0
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    36
    }
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    37
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    38
    #[inline]
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    39
    pub fn map<U, F: FnOnce(&mut T) -> U>(&mut self, y: i32, x: i32, f: F) {
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    40
        if self.is_valid_coordinate(x, y) {
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    41
            self.pixels.get_mut(y as usize, x as usize).map(f);
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    42
        }
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    43
    }
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    44
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    45
    pub fn draw_line(&mut self, x1: i32, y1: i32, x2: i32, y2: i32, value: T) {
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    46
        let mut e_x: i32 = 0;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    47
        let mut e_y: i32 = 0;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    48
        let mut d_x: i32 = x2 - x1;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    49
        let mut d_y: i32 = y2 - y1;
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
        let s_x: i32;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    52
        let s_y: i32;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    53
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    54
        if d_x > 0 {
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    55
            s_x = 1;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    56
        } else if d_x < 0 {
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    57
            s_x = -1;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    58
            d_x = -d_x;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    59
        } else {
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    60
            s_x = d_x;
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
        if d_y > 0 {
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    64
            s_y = 1;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    65
        } else if d_y < 0 {
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    66
            s_y = -1;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    67
            d_y = -d_y;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    68
        } else {
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    69
            s_y = d_y;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    70
        }
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    71
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    72
        let d = cmp::max(d_x, d_y);
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    73
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    74
        let mut x = x1;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    75
        let mut y = 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
        for _i in 0..=d {
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    78
            e_x += d_x;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    79
            e_y += d_y;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    80
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    81
            if e_x > d {
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    82
                e_x -= d;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    83
                x += s_x;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    84
            }
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    85
            if e_y > d {
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    86
                e_y -= d;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    87
                y += s_y;
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
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    90
            self.map(y, x, |p| *p = value);
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    91
        }
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    92
    }
13929
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
    93
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
    94
    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
    95
        debug_assert!(self.is_valid_coordinate(start_x - 1, start_y));
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
    96
        debug_assert!(self.is_valid_coordinate(start_x, start_y));
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
    97
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
    98
        let mut stack: Vec<(usize, usize, usize, isize)> = Vec::new();
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
    99
        fn push<T: Default + Copy + PartialEq>(
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   100
            land: &Land2D<T>,
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   101
            stack: &mut Vec<(usize, usize, usize, isize)>,
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   102
            xl: usize,
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   103
            xr: usize,
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   104
            y: usize,
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   105
            dir: isize,
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   106
        ) {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   107
            let yd = y as isize + dir;
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   108
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   109
            if land.is_valid_coordinate(0, yd as i32) {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   110
                stack.push((xl, xr, yd as usize, dir));
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   111
            }
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   112
        };
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   113
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   114
        let start_x_l = (start_x - 1) as usize;
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   115
        let start_x_r = start_x as usize;
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   116
        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
   117
        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
   118
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   119
        loop {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   120
            let a = stack.pop();
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   121
            match a {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   122
                None => return,
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   123
                Some(a) => {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   124
                    let (mut xl, mut xr, y, mut dir) = a;
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   125
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   126
                    while xl > 0 && self
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   127
                        .pixels
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   128
                        .get(y, xl)
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   129
                        .map_or(false, |v| *v != border_value && *v != fill_value)
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   130
                    {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   131
                        xl -= 1;
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   132
                    }
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   133
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   134
                    while xr < self.width() - 1 && self
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   135
                        .pixels
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   136
                        .get(y, xr)
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   137
                        .map_or(false, |v| *v != border_value && *v != fill_value)
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   138
                    {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   139
                        xr += 1;
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   140
                    }
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   141
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   142
                    while xl < xr {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   143
                        while xl <= xr
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   144
                            && (self.pixels[y][xl] == border_value
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   145
                                || self.pixels[y][xl] == fill_value)
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   146
                        {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   147
                            xl += 1;
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   148
                        }
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   149
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   150
                        let mut x = xl;
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   151
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   152
                        while xl <= xr
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   153
                            && (self.pixels[y][xl] != border_value
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   154
                                && self.pixels[y][xl] != fill_value)
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   155
                        {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   156
                            self.pixels[y][xl] = fill_value;
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   157
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   158
                            xl += 1;
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   159
                        }
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   160
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   161
                        if x < xl {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   162
                            push(self, &mut stack, x, xl - 1, y, dir);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   163
                            push(self, &mut stack, x, xl - 1, y, -dir);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   164
                        }
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   165
                    }
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   166
                }
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   167
            }
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   168
        }
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   169
    }
13922
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   170
}
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   171
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   172
#[cfg(test)]
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   173
mod tests {
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   174
    use super::*;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   175
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   176
    #[test]
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   177
    fn basics() {
13929
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   178
        let l: Land2D<u8> = Land2D::new(32, 64);
13922
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   179
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   180
        assert!(l.is_valid_coordinate(0, 0));
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   181
        assert!(!l.is_valid_coordinate(-1, -1));
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   182
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   183
        assert!(l.is_valid_coordinate(31, 63));
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   184
        assert!(!l.is_valid_coordinate(32, 63));
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   185
        assert!(!l.is_valid_coordinate(31, 64));
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   186
    }
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   187
13929
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   188
    #[test]
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   189
    fn fill() {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   190
        let mut l: Land2D<u8> = Land2D::new(128, 128);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   191
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   192
        l.draw_line(0, 0, 32, 96, 1);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   193
        l.draw_line(32, 96, 64, 32, 1);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   194
        l.draw_line(64, 32, 96, 80, 1);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   195
        l.draw_line(96, 80, 128, 0, 1);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   196
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   197
        l.draw_line(0, 128, 64, 96, 1);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   198
        l.draw_line(128, 128, 64, 96, 1);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   199
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   200
        l.fill(32, 32, 1, 2);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   201
        l.fill(16, 96, 1, 3);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   202
        l.fill(60, 100, 1, 4);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   203
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   204
        assert_eq!(l.pixels[0][0], 1);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   205
        assert_eq!(l.pixels[96][64], 1);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   206
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   207
        assert_eq!(l.pixels[40][32], 2);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   208
        assert_eq!(l.pixels[40][96], 2);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   209
        assert_eq!(l.pixels[5][0], 3);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   210
        assert_eq!(l.pixels[120][0], 3);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   211
        assert_eq!(l.pixels[5][127], 3);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   212
        assert_eq!(l.pixels[120][127], 3);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   213
        assert_eq!(l.pixels[35][64], 3);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   214
        assert_eq!(l.pixels[120][20], 4);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   215
        assert_eq!(l.pixels[120][100], 4);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   216
        assert_eq!(l.pixels[100][64], 4);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13922
diff changeset
   217
    }
13922
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   218
}