【问题标题】:Error with Bresenham's line algorithmBresenham 线算法错误
【发布时间】:2015-08-28 19:24:01
【问题描述】:

我正在尝试用星号 (*) 填充矩阵以绘制 Bresenham 的线,但是当我打印出矩阵仅填充一颗星的东西时,我不知道出了什么问题。语言是Java

public class PtLine extends VectorObject{   // inherites from another class
private int bx;
private int by;
private int delX;
private int delY;

public PtLine(int id,int x,int y,int bx,int by){
     super(id,x,y);
     this.bx = bx;
     this.by = by;
     this.delX = this.bx-x;
     this.delY = this.by-y;

 }
 public void draw ( char [][] matrix ){    // filling the martic with stars
    int D = 2*delY - delX;
    matrix[x][y] = '*';
    int j = y;

    for (int i=x+1;i==bx;i++){
       if(D > 0){
          j+=1;
          matrix[i][j]='*';
          D = D + (2*delY-2*delX);
       }
      else{
         matrix[i][j]='*';
         D = D + (2*delY);
      } 
    } 
 }     

}

下面的代码是当我试图打印出矩阵时

  class Question3{
     public static void main ( String args [] ){


     char[][] matrix = new char[20][20];
     for (int y = 0; y < 20; y++) {
        for (int x = 0; x < 20; x++) {
            matrix[y][x] = ' ';
         }
      }

     PtLine n = new PtLine(6,6,6,13,13);
     n.draw(matrix);
     for (int y = 0; y < 20; y++) {
        for (int x = 0; x < 20; x++) {
            System.out.print(matrix[x][y]);
         }
         System.out.println();
     } 
 }

}

【问题讨论】:

    标签: java bresenham


    【解决方案1】:

    您可能必须将i==bx 更改为i!=bx

    public void draw ( char [][] matrix ){    // filling the martic with stars
      int D = 2*delY - delX;
      matrix[x][y] = '*';
      int j = y;
    
      for (int i=x+1;i!=bx;i++){ // here
      ...
      }
    }
    

    for 循环在此条件为真时继续。在您的代码循环中,在第一次迭代之前立即结束,因为这个条件是假的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-30
      • 1970-01-01
      相关资源
      最近更新 更多