2011-11-15 20 views
-2

"*"をコンソールに使用して線を描こうとしていますが、 "*"は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; 
}*/ 

クラスMyGraphicsApp:私はこれがあるべき

width= x2 - x ; 
height = y2 - y ; 

を割り当て疑い

class MyGraphicsApp { 
MyGraphicsApp() {mg = new MyGraphics(80, 25);} 

void paint(MyGraphics g) { 
    g.drawLine(5, 12, 27, 2); 
    g.drawLine(2, 2, 30, 30); 
} 

void repaint() { 
    paint(mg); 
    mg.display(); 
} 

public static void main(String [] args) { 
    MyGraphicsApp myGraphicsApp = new MyGraphicsApp(); 
    myGraphicsApp.repaint(); 
} 

MyGraphics mg; 
} 
+0

"ドリューンライン"とは何ですか? 「線を引く」という意味ですか? –

+0

その辞書は "drewan"の結果はありません。 –

+0

はい申し訳ありません私の綴り –

答えて

0

width= x2 - x1 ; 
height = y2 - y1 ; 

そして、あなたはx1y1の値を変更することはありませんので、このラインも

fb.setPixel(x1, y1); 

間違っています。

関連する問題