java - 二维旋转会留下孔洞

标签 java bitmap rotation rendering equation

我正在尝试创建一个二维旋转,但没有任何运气,这是代码!

    public void render(int xPos, int yPos, double a, BitMap data){
    double angle = Math.toRadians(a);
    int xScr = 0;
    int yScr = 0;
    int CenterX = data.getWidth() / 2;
    int CenterY = data.getHeight() / 2;
    for(int y = 0; y < data.getHeight(); y++){
        yScr = (y + yPos);
        if(yScr < 0){
            continue;
        }
        else if(yScr >= height){
            return;
        }
        for(int x = 0; x < data.getWidth(); x++){
            xScr = (x + xPos);
            if(xScr < 0){
                continue;
            }
            if(yScr >= width){
                return;
            }
           int dataX = (int)(CenterX + (x - CenterX) * Math.cos(angle) - (y - CenterY) *Math.sin(angle));

           int dataY = (int)(CenterY + (x - CenterX) * Math.sin(angle) + (y - CenterY) * Math.cos(angle));

           if(dataX > 0 && dataX < data.getWidth()){
               if(dataY > 0 && dataY < data.getHeight()){
                    screenPixels.setValue(dataX, dataY, data.getValue(x, y));
               }
           }

        }
    }
}

立方体正在渲染并旋转,但它留下了洞。我知道这是因为 dataX 和 dataY 进行了四舍五入,并且会留下像素。我真的不知道从哪里开始,如果有人可以编写缺少的代码,我会非常高兴,因为我本周末将参加 Ludumdare,但尚未弄清楚这一点。请帮助我!

最佳答案

以下代码成功旋转位图,感谢 reddit.com/user/dd_123!

public void render2(int xPos, int yPos, double angle, BitMap data){
   double angle2 = Math.toRadians(angle);
   angle = angle2;
   int w = data.getWidth();
   int h  = data.getHeight();
   int size =  (int) (Math.sqrt(w * w + h * h));
   BitMap newBitMap = new BitMap(size, size);
   int xCenter = w / 2;
   int yCenter = h / 2;
   final int halfSize = size / 2;
   for(int y = 0; y < size; y++){
       for(int x = 0; x < size; x++){
          int samplePointX = x - halfSize;
          int samplePointY = y - halfSize;

          int xData, yData;
          xData = (int)(samplePointX * -Math.cos(angle) + samplePointY * Math.sin(angle));
          yData = (int)(samplePointX * -Math.sin(angle) - samplePointY * Math.cos(angle));
          xData += xCenter;
          yData += yCenter;
          if(!(xData >= 0 && xData < w)){
             continue;
          }
          if(!(yData >= 0 && yData < h)){
             continue;
          }
          if((x) + (y) * size > size * size){
             continue;
          }
          screenPixels.setValue(x, y, data.getValue(xData, yData));
       }
   }

}

位图类

public class BitMap {

public BitMap(int width, int height){
    this.width = width;
    this.height = height;

    this.data = new int[width * height];
}

public BitMap(int[] data, int width, int height) {
    this.data = data;
    this.width = width;
    this.height = height;
}

private int[] data;
private int width, height;

public int getWidth(){
    return width;
}

public int getHeight(){
    return height;
}

public int getValue(int x, int y){
    return data[x + y * width];
}

public BitMap fillWithValues(int value){
    for(int i = 0; i < data.length; i++){
        data[i] = value;
    }
    return this;
}

public void setValue(int xScr, int yScr, int value) {
    data[xScr + yScr * width] = value;
}

public int[] getValues() {
    return data;
}

public BitMap subData(int xPos, int yPos, int w, int h) {
    BitMap bitmap = new BitMap(w, h);
    for(int y = 0; y < h; y++){
        for(int x = 0; x < w; x++){
            bitmap.setValue(x, y, this.getValue(x + xPos, y + yPos));
        }
    }
    return bitmap;
}

}

关于java - 二维旋转会留下孔洞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20530949/

相关文章:

java - 使用 BlobKey 在 Google AppEngine 的 BlobStore 中查找 Blob

java - 单击注销按钮终止 session 并重定向到登录页面

c - GetObject 在文本光标上失败

android - 带圆角和描边的位图图像

java - 使用 android.gms.drive 从 android 中删除/垃圾文件

android - Android SurfaceView 的低 FPS

google-chrome - 将 css3 旋转到 Chrome 中的 DIV,然后背景附件 :fixed creating bug

c# - 如何在 iTextSharp 中旋转文本?

html - 旋转和对齐 div

java - 不使用 oncreate 在 Activity 之间交换数据