2013-10-20 166 views
0

我想畫與Java的幫助下圓,但我被困 這是我到目前爲止已經完成,繪製簡單的圓形

public class Circle { 
public static void DrawMeACircle(int posX, int posY, int radius) { 
    int a = 10; 
    int b = 10; 

    int x = posX - a; //x = position of x away from the center 
    int y = posY - b; 
    int xSquared = (x - a)*(x - a); 
    int ySquared = (y - b)*(y - b); 
    for (int i = 0;i <=20; i++) { 
     for (int j = 1;j <=20; j++) { 
      if (Math.abs(xSquared) + (ySquared) >= radius*radius && Math.abs(xSquared) + (ySquared) <= radius*radius) { 
        System.out.println("#"); 
      } else { 
       System.out.println(" "); 
      } 
     } 

    } 
} 

public static void main(String[] args){ 
    DrawMeACircle(5,5,5); 



    } 

}

正如你可以看到,這不能正常工作。有誰知道如何解決這個問題?我很感謝任何可能的幫助,邁克爾。

+0

如果您深入研究圖形並需要繼續繪製圓圈,請查看:http://en.wikipedia.org/wiki/Midpoint_circle_algorithm – Rethunk

回答

0

首先,你的內在if條件不取決於ij,所以是一個常數。這意味着每次都打印相同的符號,即空格符號。

接下來,您每次都使用System.out.println(" ");,爲每個符號添加換行符。所以,結果看起來像一列空格。

最後但並非最不重要:繪圖區域受20x20「像素」限制,無法適合大圓圈。

你可以用一些解決所有這些點在一起,就像

public class Circle { 
public static void DrawMeACircle(int posX, int posY, int radius) { 
    for (int i = 0;i <= posX + radius; i++) { 
     for (int j = 1;j <=posY + radius; j++) { 
      int xSquared = (i - posX)*(i - posX); 
      int ySquared = (j - posY)*(j - posY); 
      if (Math.abs(xSquared + ySquared - radius * radius) < radius) { 
       System.out.print("#"); 
      } else { 
       System.out.print(" "); 
      } 
     } 
     System.out.println(); 
    } 
} 

public static void main(String[] args){ 
    DrawMeACircle(5,15,5); 
} 
} 

這讓我們有點類似於圓圈。