1

我正在尝试用星星(*)填充矩阵以绘制 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();
     } 
 }

}

4

1 回答 1

0

您可能必须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此条件为真时,循环继续。在您的代码循环中,在第一次迭代之前立即结束,因为这个条件是错误的。

于 2015-08-30T15:27:27.957 回答