gpt4 book ai didi

Java初学者坐标越界

转载 作者:行者123 更新时间:2023-12-02 02:55:15 26 4
gpt4 key购买 nike

我一直在学习如何旋转图片,我必须将图片旋转 180 度和 270 度。我还没有开始使用 270,而且我似乎不知道如何修复越界错误。

public Picture rotate180()
{
Picture rotated = new Picture ();

for (int x = 0, roty = 0; x < getWidth(); x++, roty++)
{
for (int y = 0, rotx = getWidth()-1; y<getHeight(); y++, rotx--)
{
Pixel oldPixel = getPixel(x,y);
Pixel newPixel = getPixel(rotx,roty);
newPixel.setColor(oldPixel.getColor());
}
}
return rotated;
}

最佳答案

private enum Degree {
_90_ {
public Picture rotate(Picture picture) {
Picture res = swapRowCol(new Picture(picture.height, picture.width), picture);

// mirror each column
for (int col = 0; col < res.width; col++)
for (int row1 = 0, row2 = res.height - 1; row1 < row2; row1++, row2--)
swapColor(res, col, row1, col, row2);

return res;
}
},
_180_ {
public Picture rotate(Picture picture) {
Picture res = new Picture(picture.width, picture.height);

// desc rows and mirror each one
for (int row1 = 0, row2 = res.height - 1; row1 < res.height; row1++, row2--)
for (int col1 = 0, col2 = picture.width - 1; col1 < picture.width; col1++, col2--)
res.getPixel(col2, row2).setColor(picture.getPixel(col1, row1).getColor());

return res;
}
},
_270_ {
public Picture rotate(Picture picture) {
Picture res = swapRowCol(new Picture(picture.height, picture.width), picture);

// mirror each row
for (int row = 0; row < res.height; row++)
for (int col1 = 0, col2 = res.width - 1; col1 < col2; col1++, col2--)
swapColor(res, col1, row, col2, row);

return res;
}
};

public abstract Picture rotate(Picture picture);

protected static Picture swapRowCol(Picture dst, Picture src) {
// (x;y) -> (y;x)
for (int row = 0; row < src.height; row++)
for (int col = 0; col < src.width; col++)
dst.getPixel(row, col).setColor(src.getPixel(col, row).getColor());

return dst;
}

protected static void swapColor(Picture res, int srcCol, int srcRow, int dstCol, int dstRow) {
Pixel pixel1 = res.getPixel(srcCol, srcRow);
Pixel pixel2 = res.getPixel(dstCol, dstRow);
int color = pixel1.getColor();
pixel1.setColor(pixel2.getColor());
pixel2.setColor(color);
}
}

关于Java初学者坐标越界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43192992/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com