首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Bresenham直线算法错误

Bresenham直线算法错误
EN

Stack Overflow用户
提问于 2015-08-29 03:24:02
回答 1查看 208关注 0票数 1

我尝试用星号(*)填充矩阵来绘制Bresenham's线,但当我打印出矩阵时,我不知道哪里出了问题。语言是Java

代码语言:javascript
复制
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);
      } 
    } 
 }     

}

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

代码语言:javascript
复制
  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();
     } 
 }

}

EN

回答 1

Stack Overflow用户

发布于 2015-08-30 23:27:28

您很可能需要使用i!=bx更改i==bx

代码语言:javascript
复制
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循环将继续。循环在第一次迭代之前立即在开始时结束,因为此条件为false。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32278452

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档