48 pub fn is_valid_coordinate(&self, x: i32, y: i32) -> bool { |
48 pub fn is_valid_coordinate(&self, x: i32, y: i32) -> bool { |
49 self.is_valid_x(x) && self.is_valid_y(y) |
49 self.is_valid_x(x) && self.is_valid_y(y) |
50 } |
50 } |
51 |
51 |
52 #[inline] |
52 #[inline] |
53 pub fn get_mut(&mut self, y: i32, x: i32) -> Option<&mut T> { |
53 pub fn map<U: Default, F: FnOnce(&mut T) -> U>(&mut self, y: i32, x: i32, f: F) -> U { |
54 if self.is_valid_coordinate(x, y) { |
54 if self.is_valid_coordinate(x, y) { |
55 unsafe { |
55 unsafe { |
56 // hey, I just checked that coordinates are valid! |
56 // hey, I just checked that coordinates are valid! |
57 Some(self.pixels.get_unchecked_mut(y as usize, x as usize)) |
57 f(self.pixels.get_unchecked_mut(y as usize, x as usize)) |
58 } |
58 } |
59 } else { |
59 } else { |
60 None |
60 U::default() |
61 } |
61 } |
62 } |
|
63 |
|
64 #[inline] |
|
65 pub fn map<U: Default, F: FnOnce(&mut T) -> U>(&mut self, y: i32, x: i32, f: F) -> U { |
|
66 self.get_mut(y, x).map(f).unwrap_or_default() |
|
67 } |
62 } |
68 |
63 |
69 fn apply_along_line<U: Default + ops::AddAssign, F: FnMut(i32, i32) -> U>( |
64 fn apply_along_line<U: Default + ops::AddAssign, F: FnMut(i32, i32) -> U>( |
70 x1: i32, |
65 x1: i32, |
71 y1: i32, |
66 y1: i32, |
154 } |
149 } |
155 |
150 |
156 pub fn fill_from_iter<I>(&mut self, i: I, value: T) -> usize |
151 pub fn fill_from_iter<I>(&mut self, i: I, value: T) -> usize |
157 where I: std::iter::Iterator<Item = Point> |
152 where I: std::iter::Iterator<Item = Point> |
158 { |
153 { |
159 i.map(|p| self.get_mut(p.y, p.x).map(|v| *v = value)).count() |
154 i.map(|p| self.map(p.y, p.x, |v| {*v = value; 1})).count() |
160 } |
155 } |
161 |
156 |
162 pub fn draw_line(&mut self, from: Point, to: Point, value: T) -> usize { |
157 pub fn draw_line(&mut self, from: Point, to: Point, value: T) -> usize { |
163 self.fill_from_iter(LinePoints::new(from, to), value) |
158 self.fill_from_iter(LinePoints::new(from, to), value) |
164 } |
159 } |