【问题标题】:2D rotation is leaving holes2D 旋转留下孔洞
【发布时间】:2013-12-11 22:08:28
【问题描述】:

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

    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,但还没有弄清楚这一点。请帮帮我!

【问题讨论】:

  • “修复”是从目标像素倒退到源像素。这将确保完全覆盖。
  • 你能给我举个例子吗?因为我以前听说过,但我不太明白应该从那句话开始。
  • 您正在循环访问源图像并将像素旋转到目标图像中。这将导致“漏洞”。相反,循环遍历目标图像并通过向相反方向旋转来找到源像素位置。
  • 我明天会试试这个,但是如果你有时间,写代码,这样我就可以给你一个正确的答案。谢谢你的解释!
  • 所以我成功地做到了,但现在轮换很奇怪。 pastebin.com/hUWAq894

标签: java bitmap rotation rendering equation


【解决方案1】:

以下代码成功旋转位图,感谢 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;
}

}

【讨论】:

    【解决方案2】:

    我解决了这个问题一次,先将图像放大 4 倍,然后旋转,然后再缩小。它很好地填补了这些漏洞,而且速度不慢也不困难。 BitBank 的回答也不错。我没想到倒着做。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多