image rotation
This commit is contained in:
parent
24e11362a0
commit
04c823c309
36
src/image.rs
36
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<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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue