image rotation

This commit is contained in:
Max Bradbury 2020-07-20 15:07:26 +01:00
parent 24e11362a0
commit 04c823c309
1 changed files with 35 additions and 1 deletions

View File

@ -29,6 +29,21 @@ impl Image {
self.pixels = pixels;
}
/// rotate image 90° clockwise
fn rotate(&mut self) {
let mut pixels = Vec::with_capacity(64);
// start from bottom-left corner, work upward in that column,
// then work on the next column to the right
for x in 0..8 {
for y in (0..8).rev() {
pixels.push(self.pixels[(y * 8) + x]);
}
}
self.pixels = pixels;
}
}
impl From<String> for Image {
@ -163,7 +178,7 @@ mod test {
}
#[test]
fn test_mirror() {
fn mirror() {
let mut image = crate::mock::image::asymmetrical();
image.mirror();
@ -180,4 +195,23 @@ mod test {
assert_eq!(image, mirrored);
}
#[test]
fn rotate() {
let mut image = crate::mock::image::asymmetrical();
image.rotate();
let rotated = Image { pixels: vec![
0,0,0,1,1,0,0,0,
0,0,1,0,0,1,0,0,
0,1,0,0,0,0,0,0,
1,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
]};
assert_eq!(image, rotated);
}
}