Java 为什么这个代码只旋转正方形?

Java 为什么这个代码只旋转正方形?,java,algorithm,rotation,2d,Java,Algorithm,Rotation,2d,我将使用这个算法进行图像旋转,但是我意识到它只旋转正方形,而不是矩形 有人知道为什么吗 主要代码问题: public static int[] rotate(double angle, int[] pixels, int width, int height) { final double radians = Math.toRadians(angle); final double cos = Math.cos(radians); final double sin = Ma

我将使用这个算法进行图像旋转,但是我意识到它只旋转正方形,而不是矩形

有人知道为什么吗

主要代码问题:

public static int[] rotate(double angle, int[] pixels, int width, int height) {
    final double radians = Math.toRadians(angle);

    final double cos = Math.cos(radians);
    final double sin = Math.sin(radians);

    final int[] pixels2 = new int[pixels.length];

    for(int pixel = 0; pixel < pixels2.length; pixel++) {
        pixels2[pixel] = 0xFFFFFF;
    }

    for(int x = 0; x < width; x++) {
        for(int y = 0; y < height; y++) {
            final int centerx = width / 2;
            final int centery = height / 2;
            final int m = x - centerx;
            final int n = y - centery;
            final int j = ((int) ( m * cos + n * sin ) ) + centerx;
            final int k = ((int) ( n * cos - m * sin ) ) + centery;
            if( j >= 0 && j < width && k >= 0 && k < height ){
                pixels2[ ( y * width + x ) ] = pixels[ ( k * width + j ) ];
            }
        }
    }
    return pixels2;
}
公共静态int[]旋转(双角度,int[]像素,int宽度,int高度){
最终双弧度=数学托拉度(角度);
最终双余弦=数学余弦(弧度);
最终双正弦=数学正弦(弧度);
final int[]pixels2=新int[pixels.length];
用于(int-pixel=0;pixel=0&&j=0&&k
上下文应用程序:

try {
    BufferedImage testrot = ImageIO.read(new File("./32x32.png"));

    int[] linearpixels = new int[testrot.getWidth() * testrot.getHeight()];
    int c = 0;
    for(int i = 0; i < testrot.getWidth(); i++){
        for(int j = 0; j < testrot.getHeight(); j++){
            linearpixels[c] = testrot.getRGB(i, j);
            c++;
        }
    }

    int[] lintestrot = rotate(50, linearpixels, 32, 32);
    BufferedImage image = new BufferedImage(70, 70, BufferedImage.TYPE_INT_RGB);
    c = 0;
    for(int i = 0; i < 32; i++){
        for(int j = 0; j < 32; j++){
            image.setRGB(i, j, lintestrot[c]);
            c++;
        }
    }

    File outputfile = new File("test002.bmp");
    ImageIO.write(image, "bmp", outputfile);

} catch (IOException e1) {
    e1.printStackTrace();
}
试试看{
BuffereImage testrot=ImageIO.read(新文件(“./32x32.png”);
int[]linearpixels=new int[testrot.getWidth()*testrot.getHeight()];
int c=0;
对于(int i=0;i

如果将宽度或高度更改为33,则结果将是错误的(错误图像)。

您的算法确实有效。问题在于上下文应用程序中的循环。因为像素是按光栅顺序存储的,所以外循环需要迭代到高度,内循环迭代到宽度,例如:

for(int i = 0; i < testrot.getHeight(); i++){
    for(int j = 0; j < testrot.getWidth(); j++){
        linearpixels[c] = testrot.getRGB(j, i); //edit here, tested
        c++;
    }
}
循环需要如下更改:

c = 0;
for(int i = 0; i < 40; i++){
    for(int j = 0; j < 32; j++){
        image.setRGB(i, j, lintestrot[c]);
        c++;
    }
}
c=0;
对于(int i=0;i<40;i++){
对于(int j=0;j<32;j++){
setRGB(i,j,lintestrot[c]);
C++;
}
}

请注意,循环中的顺序(高度然后宽度)与函数调用(宽度然后高度)相反。

您为什么不能做一些事情呢?使用算法给了我更多的自由,但我已经在分析您的主题,这很有帮助,谢谢。您是否将for循环中的限制改为33或其他什么,也是吗?是的。我正在做。我以前已经观察到这种行为(其他循环),我甚至没有尝试过。我会翻译你的英语来理解问题的来源,非常感谢你,我正在测试。Samgak,经过测试和批准!下次注意变量名:“LINEARPIXELS”呵呵。
c = 0;
for(int i = 0; i < 40; i++){
    for(int j = 0; j < 32; j++){
        image.setRGB(i, j, lintestrot[c]);
        c++;
    }
}