Expand description
Calculate abs value between 2 usize values Example:
use mapgen::geometry::usize_abs;
assert_eq!(usize_abs(5, 3), 2);
assert_eq!(usize_abs(3, 5), 2);
Examples found in repository?
src/map_buffer.rs (line 183)
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
pub fn paint(&mut self, mode: Symmetry, brush_size: usize, x: usize, y: usize) {
match mode {
Symmetry::None => self.apply_paint(brush_size, x, y),
Symmetry::Horizontal => {
let center_x = self.width / 2;
if x == center_x {
self.apply_paint(brush_size, x, y);
} else {
let dist_x = usize_abs(center_x, x);
self.apply_paint(brush_size, center_x + dist_x, y);
self.apply_paint(brush_size, center_x - dist_x, y);
}
}
Symmetry::Vertical => {
let center_y = self.height / 2;
if y == center_y {
self.apply_paint(brush_size, x, y);
} else {
let dist_y = usize_abs(center_y, y);
self.apply_paint(brush_size, x, center_y + dist_y);
self.apply_paint(brush_size, x, center_y - dist_y);
}
}
Symmetry::Both => {
let center_x = self.width / 2;
let center_y = self.height / 2;
if x == center_x && y == center_y {
self.apply_paint(brush_size, x, y);
} else {
let dist_x = usize_abs(center_x, x);
self.apply_paint(brush_size, center_x + dist_x, y);
self.apply_paint(brush_size, center_x - dist_x, y);
let dist_y = usize_abs(center_y, y);
self.apply_paint(brush_size, x, center_y + dist_y);
self.apply_paint(brush_size, x, center_y - dist_y);
}
}
}
}