algorithm - 正方形像素网格的图像旋转算法

标签 algorithm image rotation

我目前正在开发我自己的小型在线像素编辑器。
现在我正在尝试添加旋转功能。 但我不太清楚如何实现它。

这是我的像素网格的基本查询:

for (var y = 0;y < pixelAmount;y++) {
    for (var x = 0;x < pixelAmount;x++) {
        var name = y + "x" + x;

        newY = ?? ;
        newX = ?? ;

        if ($(newY + "x" + newX).style.backgroundColor != "rgb(255, 255, 255)")
        { $(name).style.backgroundColor = $(newY + "x" + newX).style.backgroundColor; }
    }
}

如何计算 newYnewX

最佳答案

How do you rotate a two dimensional array?

从这个 ^ 帖子我得到了这个方法(在 c# 中):

int a[4][4];
int n=4;
int tmp;
for (int i=0; i<n/2; i++){
        for (int j=i; j<n-i-1; j++){
                tmp=a[i][j];
                a[i][j]=a[j][n-i-1];
                a[j][n-i-1]=a[n-i-1][n-j-1];
                a[n-i-1][n-j-1]=a[n-j-1][i];
                a[n-j-1][i]=tmp;
        }
}   

或者这个:

int[,] array = new int[4,4] {
    { 1,2,3,4 },
    { 5,6,7,8 },
    { 9,0,1,2 },
    { 3,4,5,6 }
};

int[,] rotated = RotateMatrix(array, 4);

static int[,] RotateMatrix(int[,] matrix, int n) {
    int[,] ret = new int[n, n];

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            ret[i, j] = matrix[n - j - 1, i];
        }
    }

    return ret;
}

第一种方法不使用第二个数组 (/matrix) 来节省内存..

关于algorithm - 正方形像素网格的图像旋转算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7220018/

相关文章:

algorithm - 什么算法可以让一个数字最接近一个可以均匀(在一定范围内)分成两个其他常数的常数?

python - 图像分类软件

iphone - Cocos2d iPhone : Rotate Sprite using Accelerometer

c - 数组:C 中的左旋转

java - 选择最佳运算符组合以找到目标数字

c - 如何在动态规划技术中从头开始打印最佳路径

java - 如何刷新JTabbedPane中的数据?

php - 从 iOS 应用程序错误旋转上传的图像

algorithm - 如何检查是否可以在字典中找到所有子字符串

php - 如何在电子邮件中嵌入图像