2011-11-15 127 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:

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

什麼是「drewan線」?你的意思是「畫一條線」? –

+0

該字典中沒有「drewan」的結果。 –

+0

對不起請原諒我的拼寫 –

回答

0

我懷疑這個分配

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

應該

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

而此行是錯誤的也

fb.setPixel(x1, y1); 

,因爲你永遠不會改變的x1y1值。