rust/land2d/src/lib.rs
author unc0rr
Tue, 16 Oct 2018 23:59:51 +0200
changeset 13952 9230aed8a32e
parent 13945 a140f28decc4
child 13955 9c112f2ae02d
permissions -rw-r--r--
Implement Land2D::change_round()
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13938
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
13952
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
    11
impl<T: Copy + PartialEq> Land2D<T> {
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
    12
    pub fn new(width: usize, height: usize, fill_value: T) -> Self {
13938
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 {
13952
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
    17
            pixels: vec2d::Vec2D::new(width, height, fill_value),
13938
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]
13945
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    24
    pub fn width(&self) -> usize {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    25
        self.pixels.width()
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    26
    }
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    27
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    28
    #[inline]
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    29
    pub fn height(&self) -> usize {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    30
        self.pixels.height()
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    31
    }
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    32
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    33
    #[inline]
13952
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
    34
    pub fn is_valid_x(&self, x: i32) -> bool {
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
    35
        (x as usize & self.width_mask) == 0
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
    36
    }
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
    37
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
    38
    #[inline]
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
    39
    pub fn is_valid_y(&self, y: i32) -> bool {
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
    40
        (y as usize & self.height_mask) == 0
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
    41
    }
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
    42
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
    43
    #[inline]
13945
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
    44
    pub fn is_valid_coordinate(&self, x: i32, y: i32) -> bool {
13952
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
    45
        self.is_valid_x(x) && self.is_valid_y(y)
13938
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    46
    }
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    47
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    48
    #[inline]
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    49
    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
    50
        if self.is_valid_coordinate(x, y) {
13952
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
    51
            unsafe { // hey, I just checked that coordinates are valid!
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
    52
                f(self.pixels.get_unchecked_mut(y as usize, x as usize));
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
    53
            }
13938
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    54
        }
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    55
    }
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    56
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    57
    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
    58
        let mut e_x: i32 = 0;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    59
        let mut e_y: i32 = 0;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    60
        let mut d_x: i32 = x2 - x1;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    61
        let mut d_y: i32 = y2 - y1;
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
        let s_x: i32;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    64
        let s_y: i32;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    65
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    66
        if d_x > 0 {
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    67
            s_x = 1;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    68
        } else if d_x < 0 {
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    69
            s_x = -1;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    70
            d_x = -d_x;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    71
        } else {
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    72
            s_x = d_x;
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
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    75
        if d_y > 0 {
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    76
            s_y = 1;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    77
        } else if d_y < 0 {
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    78
            s_y = -1;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    79
            d_y = -d_y;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    80
        } else {
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    81
            s_y = d_y;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    82
        }
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    83
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    84
        let d = cmp::max(d_x, d_y);
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
        let mut x = x1;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    87
        let mut y = y1;
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
        for _i in 0..=d {
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    90
            e_x += d_x;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    91
            e_y += d_y;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    92
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    93
            if e_x > d {
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    94
                e_x -= d;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    95
                x += s_x;
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
            if e_y > d {
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    98
                e_y -= d;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    99
                y += s_y;
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
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   102
            self.map(y, x, |p| *p = value);
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   103
        }
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   104
    }
13945
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   105
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   106
    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
   107
        debug_assert!(self.is_valid_coordinate(start_x - 1, start_y));
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   108
        debug_assert!(self.is_valid_coordinate(start_x, start_y));
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   109
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   110
        let mut stack: Vec<(usize, usize, usize, isize)> = Vec::new();
13952
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   111
        fn push<T: Copy + PartialEq>(
13945
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   112
            land: &Land2D<T>,
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   113
            stack: &mut Vec<(usize, usize, usize, isize)>,
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   114
            xl: usize,
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   115
            xr: usize,
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   116
            y: usize,
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   117
            dir: isize,
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   118
        ) {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   119
            let yd = y as isize + dir;
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   120
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   121
            if land.is_valid_coordinate(0, yd as i32) {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   122
                stack.push((xl, xr, yd as usize, dir));
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   123
            }
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   124
        };
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   125
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   126
        let start_x_l = (start_x - 1) as usize;
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   127
        let start_x_r = start_x as usize;
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   128
        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
   129
        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
   130
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   131
        loop {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   132
            let a = stack.pop();
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   133
            match a {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   134
                None => return,
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   135
                Some(a) => {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   136
                    let (mut xl, mut xr, y, mut dir) = a;
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   137
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   138
                    while xl > 0 && self
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   139
                        .pixels
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   140
                        .get(y, xl)
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   141
                        .map_or(false, |v| *v != border_value && *v != fill_value)
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   142
                    {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   143
                        xl -= 1;
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   144
                    }
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   145
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   146
                    while xr < self.width() - 1 && self
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   147
                        .pixels
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   148
                        .get(y, xr)
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   149
                        .map_or(false, |v| *v != border_value && *v != fill_value)
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   150
                    {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   151
                        xr += 1;
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   152
                    }
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   153
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   154
                    while xl < xr {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   155
                        while xl <= xr
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   156
                            && (self.pixels[y][xl] == border_value
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   157
                                || self.pixels[y][xl] == fill_value)
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   158
                        {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   159
                            xl += 1;
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   160
                        }
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   161
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   162
                        let mut x = xl;
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   163
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   164
                        while xl <= xr
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   165
                            && (self.pixels[y][xl] != border_value
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   166
                                && self.pixels[y][xl] != fill_value)
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   167
                        {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   168
                            self.pixels[y][xl] = fill_value;
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   169
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   170
                            xl += 1;
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   171
                        }
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   172
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   173
                        if x < xl {
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   174
                            push(self, &mut stack, x, xl - 1, y, dir);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   175
                            push(self, &mut stack, x, xl - 1, y, -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
            }
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   180
        }
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   181
    }
13952
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   182
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   183
    #[inline]
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   184
    fn fill_circle_line<F: Fn(&mut T) -> usize>(
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   185
        &mut self,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   186
        y: i32,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   187
        x_from: i32,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   188
        x_to: i32,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   189
        f: &F,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   190
    ) -> usize {
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   191
        let mut result = 0;
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   192
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   193
        if self.is_valid_y(y) {
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   194
            for i in cmp::min(x_from, 0) as usize..cmp::max(x_to as usize, self.width() - 1) {
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   195
                unsafe { // coordinates are valid at this point
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   196
                    result += f(self.pixels.get_unchecked_mut(y as usize, i));
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   197
                }
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   198
            }
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   199
        }
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   200
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   201
        result
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   202
    }
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   203
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   204
    #[inline]
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   205
    fn fill_circle_lines<F: Fn(&mut T) -> usize>(
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   206
        &mut self,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   207
        x: i32,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   208
        y: i32,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   209
        dx: i32,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   210
        dy: i32,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   211
        f: &F,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   212
    ) -> usize {
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   213
        self.fill_circle_line(y + dy, x - dx, x + dx, f)
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   214
            + self.fill_circle_line(y - dy, x - dx, x + dx, f)
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   215
            + self.fill_circle_line(y + dx, x - dy, x + dy, f)
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   216
            + self.fill_circle_line(y - dx, x - dy, x + dy, f)
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   217
    }
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   218
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   219
    pub fn change_round<F: Fn(&mut T) -> usize>(
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   220
        &mut self,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   221
        x: i32,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   222
        y: i32,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   223
        radius: i32,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   224
        f: F,
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   225
    ) -> usize {
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   226
        let mut dx: i32 = 0;
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   227
        let mut dy: i32 = radius;
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   228
        let mut d = 3 - 2 * radius;
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   229
        let mut result = 0;
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   230
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   231
        while dx < dy {
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   232
            result += self.fill_circle_lines(x, y, dx, dy, &f);
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   233
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   234
            if d < 0 {
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   235
                d += 4 * dx + 6;
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   236
            } else {
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   237
                d += 4 * (dx - dy) + 10;
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   238
                dy -= 1;
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   239
            }
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   240
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   241
            dx += 1;
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   242
        }
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   243
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   244
        if dx == dy {
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   245
            result += self.fill_circle_lines(x, y, dx, dy, &f);
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   246
        }
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   247
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   248
        result
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   249
    }
13938
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   250
}
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   251
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   252
#[cfg(test)]
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   253
mod tests {
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   254
    use super::*;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   255
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   256
    #[test]
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   257
    fn basics() {
13952
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   258
        let l: Land2D<u8> = Land2D::new(32, 64, 0);
13938
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   259
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   260
        assert!(l.is_valid_coordinate(0, 0));
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   261
        assert!(!l.is_valid_coordinate(-1, -1));
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   262
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   263
        assert!(l.is_valid_coordinate(31, 63));
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   264
        assert!(!l.is_valid_coordinate(32, 63));
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   265
        assert!(!l.is_valid_coordinate(31, 64));
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   266
    }
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   267
13945
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   268
    #[test]
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   269
    fn fill() {
13952
9230aed8a32e Implement Land2D::change_round()
unc0rr
parents: 13945
diff changeset
   270
        let mut l: Land2D<u8> = Land2D::new(128, 128, 0);
13945
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   271
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   272
        l.draw_line(0, 0, 32, 96, 1);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   273
        l.draw_line(32, 96, 64, 32, 1);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   274
        l.draw_line(64, 32, 96, 80, 1);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   275
        l.draw_line(96, 80, 128, 0, 1);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   276
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   277
        l.draw_line(0, 128, 64, 96, 1);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   278
        l.draw_line(128, 128, 64, 96, 1);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   279
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   280
        l.fill(32, 32, 1, 2);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   281
        l.fill(16, 96, 1, 3);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   282
        l.fill(60, 100, 1, 4);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   283
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   284
        assert_eq!(l.pixels[0][0], 1);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   285
        assert_eq!(l.pixels[96][64], 1);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   286
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   287
        assert_eq!(l.pixels[40][32], 2);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   288
        assert_eq!(l.pixels[40][96], 2);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   289
        assert_eq!(l.pixels[5][0], 3);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   290
        assert_eq!(l.pixels[120][0], 3);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   291
        assert_eq!(l.pixels[5][127], 3);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   292
        assert_eq!(l.pixels[120][127], 3);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   293
        assert_eq!(l.pixels[35][64], 3);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   294
        assert_eq!(l.pixels[120][20], 4);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   295
        assert_eq!(l.pixels[120][100], 4);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   296
        assert_eq!(l.pixels[100][64], 4);
a140f28decc4 Implement Land2D::fill() + tests
unc0rr
parents: 13938
diff changeset
   297
    }
13938
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   298
}