打印出带字符的ASCII圆和轴 [英] Print out an ASCII circle and axes with characters

查看:73
本文介绍了打印出带字符的ASCII圆和轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须打印一个圆(将圆的半径,圆心的坐标( cx cy )作为输入,并打印一个字符必须绘制).

I have to print a circle (taking as input it's radius, the coordinates of the center of the circle (cx and cy), and the character with which it has to be drawn).

我为轴和圆编写了一系列if块.如果我分别使用它们,它们会很好地工作,但是当我将它们放在相同的方法中(我只需要一个方法)时,它们会以不希望的方式重叠.

I wrote a series of if-blocks for the axes and the circle. They work well if I use them separately, but when I put them in the same method (I have to have only one method) they overlap in an undesirable way.

注意:如果字符与轴重叠,则字符具有优先权.

Note: in case if the character overlaps the axes, the character has priority.

public static void drawCircle(int radius, int cx, int cy, char symbol) {
    // this method verifies that there are no negative
    // values involved (throws error/exception)
    verifyInput(radius, cx, cy);

    // set the values for extension of the axes
    // (aka how long are they)
    int xMax = cx + radius + 1;
    int yMax = cy + radius + 1;

    for (int j = yMax; j >= 0; j--) {
        for (int i = 0; i <= xMax; i++) {
            // set of if-block to print the axes
            if (i == 0 && j == 0) {
                System.out.print('+');
            } else if (i == 0) {
                if (j == yMax) {
                    System.out.print('^');
                }
                if (j != yMax) {
                    System.out.println('|');
                }
            } else if (j == 0) {
                if (i == xMax) {
                    System.out.println('>');
                }
                if (i != xMax) {
                    System.out.print('-');
                }
            }

            // if block to print the circle
            if (onCircle(radius, cx, cy, i, j) == true) {
                System.out.print('*');
            } else {
                System.out.print(' ');
            }
        }
        // I'm not sure if I need to use the print here V
        // System.out.println()
    }
}

这是 onCircle 方法.如果每个 i,j 都在要绘制的圆的轮廓上,它将对其进行验证.

Here is the onCircle method. It verifies for every i,j if it is on the outline of the circle to be drawn.

public static boolean onCircle(int radius, int cx, int cy, int x, int y) {
    boolean isDrawn = false;
    if (Math.pow(radius,2)<=(Math.pow((x-cx),2)+Math.pow((y-cy),2))
            && (Math.pow((x-cx),2)+Math.pow((y-cy),2))<=(Math.pow(radius,2)+1)) {
        isDrawn = true;
    }
    return isDrawn;
}

这是我的输出(没有最后的打印语句):

Here is my output (without the last print statement):

^          |
 ***  |
*   * |
*   * |
*   * + - - - - -*-*-*- >

这是我的输出(带有最后的打印语句):

Here is my output (with the last print statement):

^

|
   ***
|
  *   *
|
  *   *
|
  *   *

+ - - - - -*-*-*- >

这是我应该得到的:

推荐答案

一般的圆的方程:

Java 中,可以这样实现:

(x-a)*(x-a) + (y-b)*(y-b) == r*r

对于整数坐标系,它可以像这样四舍五入:

(int) Math.sqrt((x-a)*(x-a) + (y-b)*(y-b)) == r

如果半径 r = 12 和圆心 a = 5,b = 1 ,则圆和轴如下所示:

If the radius r=12 and the center of the circle a=5,b=1, then the circle and axes look like this:

r=12,a=5,b=1
                    ^y                                        
                    |                                         
                    |                                         
                    | * * * * * * * * *                       
                  * *                   * *                   
              * *   |                       * *               
            * *     |                         * *             
          * *       |                           * *           
          *         |                             *           
        *           |                               *         
        *           |                               *         
      *             |                                 *       
      *             |                                 *       
      *             |                                 *       
      *             |                                 *       
      *             |         *                       *       
------* ------------+---------------------------------* ---->x
      *             |                                 *       
      *             |                                 *       
      *             |                                 *       
        *           |                               *         
        *           |                               *         
          *         |                             *           
          * *       |                           * *           
            * *     |                         * *             
              * *   |                       * *               
                  * *                   * *                   
                    | * * * * * * * * *                       
                    |                                         
                    |                                         
                    |                                         

Try it online!

// radius
int r = 12;
// center of the circle
int a = 5, b = 1;
// whitespace
int s = 3;
// print area
int xMin = a-r-s, xMax = a+r+s;
int yMin = b-r-s, yMax = b+r+s;

// output an ASCII circle and axes
System.out.println("r="+r+",a="+a+",b="+b);
for (int y = yMax; y >= yMin; y--) {
    for (int x = xMin; x <= xMax; x++) {
        if ((int) Math.sqrt((x-a)*(x-a) + (y-b)*(y-b)) == r) {
            // circle
            System.out.print("* ");
        } else if (x == a && y == b) {
            // center of the circle
            System.out.print("* ");
        } else if (x == 0 && y == 0) {
            // origin of coordinates
            System.out.print("+-");
        } else if (x == 0) {
            // ordinate axis - y
            System.out.print(y == yMax ? "^y" : "| ");
        } else if (y == 0) {
            // abscissa axis - x
            System.out.print(x == xMax ? ">x" : "--");
        } else {
            // whitespace
            System.out.print("  ");
        }
    }
    // new line
    System.out.println();
}


另请参阅:
打印出ASCII圆和轴
打印出指定宽度的ASCII圆

这篇关于打印出带字符的ASCII圆和轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆