Java 如何使用“绘制直线”*&引用;
我试图在控制台中使用“Java 如何使用“绘制直线”*&引用;,java,Java,我试图在控制台中使用“*”来画一条线,但我只得到2个“*”。我认为直线算法有问题。也许有更简单的方法 类别MyGraphics: class MyGraphics { int x, y; private int width, height; MyGraphics(int wid, int hit) { fb= new FrameBuffer(wid,hit); width = fb.getWidth(); height = fb.getHeight(); } MyGr
*
”来画一条线,但我只得到2个“*
”。我认为直线算法有问题。也许有更简单的方法
类别MyGraphics:
class MyGraphics {
int x, y;
private int width, height;
MyGraphics(int wid, int hit) {
fb= new FrameBuffer(wid,hit);
width = fb.getWidth();
height = fb.getHeight();
}
MyGraphics() {
fb = new FrameBuffer();
width = fb.getWidth();
height = fb.getHeight();
}
void drawLine(int x1,int y1,int x2,int y2)
{
width= x2 - x ;
height = y2 - y ;
int dx1 = 0, dy1 = 0, dx2 = 0, dy2 = 0 ;
if (width<0) dx1 = -1 ; else if (width>0) dx1 = 1 ;
if (height<0) dy1 = -1 ; else if (height>0) dy1 = 1 ;
if (width<0) dx2 = -1 ; else if (width>0) dx2 = 1 ;
int longest = Math.abs(width) ;
int shortest = Math.abs(height) ;
if (!(longest>shortest)) {
longest = Math.abs(height) ;
shortest = Math.abs(width) ;
if (height<0) dy2 = -1 ; else if (height>0) dy2 = 1 ;
dx2 = 0 ;
}
int numerator = longest >> 1 ;
for (int i=0;i<=longest;i++) {
fb.setPixel(x1, y1);
numerator += shortest ;
if (!(numerator<longest)) {
numerator -= longest ;
x += dx1 ;
y += dy1 ;
} else {
x += dx2 ;
y += dy2 ;
}
}
return;
}
void display() {
fb.display();
return;
} // simply calls the frame buffer's display method
FrameBuffer fb;
}
/*void drawLine(int x1,int y1,int x2,int y2)
{
width= x2 - x ;
height = y2 - y ;
int dx1 = 0, dy1 = 0, dx2 = 0, dy2 = 0 ;
if (width<0) dx1 = -1 ; else if (width>0) dx1 = 1 ;
if (height<0) dy1 = -1 ; else if (height>0) dy1 = 1 ;
if (width<0) dx2 = -1 ; else if (width>0) dx2 = 1 ;
int longest = Math.abs(width) ;
int shortest = Math.abs(height) ;
if (!(longest>shortest)) {
longest = Math.abs(height) ;
shortest = Math.abs(width) ;
if (height<0) dy2 = -1 ; else if (height>0) dy2 = 1 ;
dx2 = 0 ;
}
int numerator = longest >> 1 ;
for (int i=0;i<=longest;i++) {
fb.setPixel(y, x);
numerator += shortest ;
if (!(numerator<longest)) {
numerator -= longest ;
x += dx1 ;
y += dy1 ;
} else {
x += dx2 ;
y += dy2 ;
}
}
return;
}*/
我怀疑这是一个错误
width= x2 - x ;
height = y2 - y ;
应该是
width= x2 - x1 ;
height = y2 - y1 ;
这条线也错了
fb.setPixel(x1, y1);
既然您从未更改
x1
和y1
的值,那么什么是“德雷旺线”?你的意思是“画一条线”吗?字典上说“drewan”没有结果。首先,x1和y1从来没有改变过,所以fb.setPixel(x1,y1)除了连续输出一个像素外几乎没有什么作用。。。放弃整个抽绳功能,重新从基础开始,只做一个对角线,而不是所有4+方向,然后在此基础上构建。这应该会让你更容易,你现在正在为自己过度复杂的事情,你根本不理解这段代码。再次重写函数,相信我。从一个非常基本的开始。。。
fb.setPixel(x1, y1);