Q1.6 To rotate a image by 90 degress.
来源:互联网 发布:java特种兵.pdf 编辑:程序博客网 时间:2024/05/20 07:16
Q: Given an image represented by an NxN matrix, where each pixel in theimage is 4 bytes, write a method to rotate the image by 90 degrees.Can you do this in place?
A:
思路一:第一步交换主对角线两侧的对称元素,第二步交换第i行和第n-1-i行。即先交换主对角线对称的元素,在交换关于水平N/2线对称的元素。
思路二: 逐层旋转。 上->右, 右->下, 下->左, 左->上
#include<iostream>#include<vector>using namespace std;void swap(int &a, int &b){ int t = a; a = b; b = t;}void rotateImage1(int matrix[][4], int n) { for (int i = 0; i < n; i++) { for (int j = 0; j < n - 1 - i; j++ ) { swap(matrix[i][j], matrix[n-j-1][n-i-1]); } } for (int i = 0; i < n/2; i++) { for (int j = 0; j < n; j++) { swap(matrix[i][j], matrix[n-1-i][j]); } } return ;}void rotateImage2(int matrix[][4], int n) {for (int layer = 0; layer < n/2; layer++) {int first = layer;int last = n - 1 - layer;for (int i = first; i < last; i++) {int offset = i - first;int top = matrix[first][i];matrix[first][i] = matrix[last-offset][first];matrix[last-offset][first] = matrix[last][last - offset];matrix[last][last - offset] = matrix[i][last];matrix[i][last] = top;}}}int main() { int a[4][4] = { {1,2,3,4}, {5,6,7,8}, {9,10,11,12}, {13,14,15,16} };for(int i=0; i<4; ++i){ for(int j=0; j<4; ++j) cout<<a[i][j]<<" "; cout<<endl; } cout<<endl; rotateImage2(a, 4); for(int i=0; i<4; ++i){ for(int j=0; j<4; ++j) cout<<a[i][j]<<" "; cout<<endl; } return 0;}
0 0
- Q1.6 To rotate a image by 90 degress.
- Rotate Image By Angle 範例二
- Rotate a UIImage by 90 degree angles
- 6 rotate-image
- Using GDI+ in a Single Document MFC Project to Rotate, Zoom, and Constraint an Image
- Ch1.6: IN-place-ly rotate a pic(NxN matrix) by 90 digree in Verilog(CPP)
- A way to extend a RAW image's storage by using qemu-img
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- 删除单链表某个结点(Java版)
- [Objective-C]关联(objc_setAssociatedObject、objc_getAssociatedObject、objc_removeAssociatedObjects)
- 改 c 程序笔记_4_“size_t”类型说明
- 系统重转装
- WV.1回文序数(321123)
- Q1.6 To rotate a image by 90 degress.
- php 对接火车票,飞机票的一些心得(勿喷)
- 【BZOJ1086】【SCOI2005】王室联邦 树分块
- JVM detailed test case with code
- 网络中的哪些事儿(三)之我眼中的三层交换机
- this 的使用方法(java4 Android_16)
- mac/linux中vim永久显示行号、开启语法高亮
- javascript判断ie浏览器
- node框架---Jade详解