From 04c823c30939740fd3b4484b0501391732bb3265 Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Mon, 20 Jul 2020 15:07:26 +0100 Subject: [PATCH] image rotation --- src/image.rs | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/src/image.rs b/src/image.rs index 1dc7a93..152b978 100644 --- a/src/image.rs +++ b/src/image.rs @@ -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 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); + } }