impl Point {
pub fn new(x: u16, y: u16) -> Self {
Self { x, y }
}
pub fn transform(&self, direction: Direction, times: u16) -> Self {
let times = times as i16;
let transformation = match direction {
Direction::Up => (0, -times),
Direction::Right => (times, 0),
Direction::Down => (0, times),
Direction::Left => (-times, 0),
};
Self::new(
Self::transform_value(self.x, transformation.0),
Self::transform_value(self.y, transformation.1),
)
}
fn transform_value(value: u16, by: i16) -> u16 {
if by.is_negative() && by.abs() as u16 > value {
panic!("Transforming value {} by {} would result in a negative number", value, by);
} else {
(value as i16 + by) as u16
}
}
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter