rust/land2d/src/lib.rs
author unc0rr
Sun, 14 Oct 2018 23:17:47 +0200
changeset 13938 a83ba9ba1566
child 13945 a140f28decc4
permissions -rw-r--r--
Start land2d library implementation: draw_line() method
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
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    11
impl<T: Default + Copy> Land2D<T> {
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]
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    24
    fn is_valid_coordinate(&self, x: i32, y: i32) -> bool {
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    25
        (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
    26
    }
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    27
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    28
    #[inline]
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    29
    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
    30
        if self.is_valid_coordinate(x, y) {
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    31
            self.pixels.get_mut(y as usize, x as usize).map(f);
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    32
        }
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    33
    }
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    34
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    35
    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
    36
        let mut e_x: i32 = 0;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    37
        let mut e_y: i32 = 0;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    38
        let mut d_x: i32 = x2 - x1;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    39
        let mut d_y: i32 = y2 - y1;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    40
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    41
        let s_x: i32;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    42
        let s_y: i32;
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
        if d_x > 0 {
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    45
            s_x = 1;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    46
        } else if d_x < 0 {
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    47
            s_x = -1;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    48
            d_x = -d_x;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    49
        } else {
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    50
            s_x = d_x;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    51
        }
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    52
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    53
        if d_y > 0 {
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    54
            s_y = 1;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    55
        } else if d_y < 0 {
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    56
            s_y = -1;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    57
            d_y = -d_y;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    58
        } else {
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    59
            s_y = d_y;
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
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    62
        let d = cmp::max(d_x, d_y);
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    63
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    64
        let mut x = x1;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    65
        let mut y = y1;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    66
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    67
        for _i in 0..=d {
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    68
            e_x += d_x;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    69
            e_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
            if e_x > d {
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    72
                e_x -= d;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    73
                x += s_x;
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 e_y > d {
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    76
                e_y -= d;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    77
                y += s_y;
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    78
            }
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    79
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    80
            self.map(y, x, |p| *p = value);
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    81
        }
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
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    85
#[cfg(test)]
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    86
mod tests {
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    87
    use super::*;
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
    #[test]
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    90
    fn basics() {
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    91
        let  l:Land2D<u8> = Land2D::new(32, 64);
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
        assert!(l.is_valid_coordinate(0, 0));
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    94
        assert!(!l.is_valid_coordinate(-1, -1));
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
        assert!(l.is_valid_coordinate(31, 63));
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    97
        assert!(!l.is_valid_coordinate(32, 63));
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    98
        assert!(!l.is_valid_coordinate(31, 64));
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
    99
    }
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   100
a83ba9ba1566 Start land2d library implementation: draw_line() method
unc0rr
parents:
diff changeset
   101
}