-2

好的,在过去的几个小时里,我尝试用 ASCII 画一个圆圈。我有一组预先确定的 X、Y 和半径。但我不知道如何实施中心(H,K)。我设法使用 Math.pow() 将方程放入 java 中,我想使用的方程是 (xh)^2 + (yk)^2 = radius^2

这是我到目前为止所做的,如果我使用值 h=11, k =4 我会得到一个奇怪的形状。

请帮忙!

4

2 回答 2

1

这样的事情怎么样?

public static void DrawMeACircle(int posX, int posY, int radius)   
{  
int thickness = 2;

  for (int j = 0; j<posY+radius*2 + thickness;j++)
  {
    for (int i = 0; i<posX+radius*2+thickness; i++)
    {
      if ( Math.abs(Math.pow(Math.pow(i-posX,2) + Math.pow(j-posY,2),.5) - radius*2) < thickness)
      {
        System.out.print("#"); // or X ?
      }
      else
      {
        System.out.print(" ");
      }
      //System.out.print("\n");
    }
    System.out.print("\n");
  }
}

x = 16,y = 11,半径 = 5 和厚度 = 2 的结果看起来有点像这样

        #########       
      #############     
     ###############    
    ######## ########   
   ######       ######  
  #####           ##### 
  ####             #### 
 #####             #####
 ####               ####
 ####               ####
 ####               ####
 ###                 ###
 ####               ####
 ####               ####
 ####               ####
 #####             #####
  ####             #### 
  #####           ##### 
   ######       ######  
    ######## ########   
     ###############    
      #############     
        #########       

它被拉长的事实可能是字体的问题......

无论半径和位置如何,为了提供“单线”圆,我想说唯一的方法是事后像这样编辑它:

public static void DrawMeACircle(int posX, int posY, int radius) {  
  String [] result = new String [posY+radius*2+1];
  for (int j = 0; j<posY+radius*2+1;j++)
  {
    result[j] = "";
    for (int i = 0; i<posX+radius*2+1; i++)
    {
      float pointDistance = dist(i, j, posX, posY);
      if (pointDistance < radius*2) 
        result[j] +="#"; // or X ?
      else
        result[j]+=" ";
    }
  }
  boolean wholeLine = true;
  for (int j = 0; j < result.length; j++) {
    boolean started = false;
    if (!wholeLine && j < result.length-1 && !result[j+1].contains("#")) 
      wholeLine = true;

    if (!wholeLine)
      for (int i = 0; i < result[j].length()-1; i++) {
        if (result[j].charAt(i) != '#') continue;
        if (!started) 
          started = true;
        else if (started && result[j].charAt(i+1) != '#') {
        }
        else 
          result[j] = result[j].substring(0, i) + " " + result[j].substring(i+1);
      }
    if(wholeLine && result[j].contains("#")) wholeLine = false;
    System.out.println(result[j]);
  }
}
static float dist(int x1, int y1, int x2, int y2) {
  return (int)Math.abs(Math.pow(Math.pow(x1-x2, 2) + Math.pow(y1-y2, 2), .5));
}

这将(无论你扔什么)会导致这样的结果:

     #######     
   #         #   
  #           #  
  #           #  
 #             # 
 #             # 
 #             # 
 #             # 
 #             # 
 #             # 
 #             # 
  #           #  
  #           #  
   #         #   
     #######     
于 2013-10-10T22:05:39.580 回答
-1

我不确切知道你的输出应该如何,所以这是我的尝试。

public static void DrawMeACircle(int posX, int posY, int radius) {
    int h = 10; // Initial x position.
    int k = 5; 

    int x = posX - h;
    int y = posY - k;

    for (int i = 0; i <= 2*posX; i++){
        for (int j = 0; j <= 2*posY; j++){
            double dx = (x - i);
            double dy = (y - j);

            if (Math.abs(dx*dx + dy*dy - radius*radius) < 5){
                System.out.print("*");
            }
            else{
                System.out.print(" ");
            }
        }
        System.out.println();
    }
}

使用posX=15, posY=15, radius=5, h=10 and k =5它打印的值

    #####                  
   #     #                 
  #       #                
 #         #               
 #         #               
 #         #               
 #         #               
 #         #               
  #       #                
   #     #                 
    #####                  
于 2013-10-10T22:21:07.673 回答