Ошибка алгоритма линии Брезенхэма

Я пытаюсь заполнить матрицу звездами (*), чтобы нарисовать линию Брезенхема, но когда я распечатываю это, матрица заполнена только одной звездой, я не знаю, что не так. Язык 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();
     } 
 }

}


person Ntuthuko Sokhulu Mthiyane    schedule 28.08.2015    source источник


Ответы (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 продолжается, пока это условие истинно. В вашем коде цикл завершается сразу при запуске, до первой итерации, потому что это условие ложно.

person Tagir Valeev    schedule 30.08.2015