2013-10-20 169 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); 
} 
} 

这让我们有点类似于圆圈。