打印出指定宽度的ASCII圆圈 [英] Print out an ASCII circle of the specified width

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

问题描述

我正在尝试更改以下代码,以便获得半径2的输出:

  ******** ***** ***** ******** 

当我快要疯了时,任何帮助将不胜感激!

 公共类Main {公共静态void main(String [] args){//dist表示到中心的距离双倍距离双半径= 2;//用于水平移动for(int i = 0; i< = 2 * radius; i ++){//垂直移动for(int j = 0; j< = 2 * radius; j ++){dist = Math.sqrt((i-半径)*(i-半径)+(j-半径)*(j-半径));//dist应该在范围内(半径-0.5)//和(半径+ 0.5)打印星号(*)如果(距离>半径-0.5&&距离<半径+ 0.5)System.out.print("*");别的System.out.print(");}System.out.println(");}}} 

解决方案

输出看起来是椭圆形的,因为字符的宽度和高度不同.想想一个图像,它的像素不是正方形,而是垂直的矩形.我借用了

希望您可以将其翻译为的问题.

I'm trying to change the following code so I get this output for radius 2:

  *****
***   ***
**     **
***   ***
  *****

Any help will be appreciated as I'm about to go crazy!

public class Main {
    public static void main(String[] args) {
        // dist represents distance to the center
        double dist;
        double radius = 2;

        // for horizontal movement
        for (int i = 0; i <= 2 * radius; i++) {
            // for vertical movement
            for (int j = 0; j <= 2 * radius; j++) {
                dist = Math.sqrt(
                        (i - radius) * (i - radius) +
                        (j - radius) * (j - radius));

                // dist should be in the range (radius - 0.5)
                // and (radius + 0.5) to print stars(*)
                if (dist > radius - 0.5 && dist < radius + 0.5)
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
            System.out.println("");
        }
    }
}

解决方案

The output looks oval because characters have not same width and height. Think of an image that its pixels are not square, but vertical rectangles. I borrowed the version of the code linked by @Prasanth Rajendran, and made some modifications:

  1. added lineWidth argument
  2. added xScale argument, now every 1/xScale characters in a row are equivalent to 1 data points
  3. moved character position to its center, by adding a [0.5, 0.5] offset

// function to print circle pattern 
static void printPattern(int radius, int lineWidth, double xScale)
{
    double hUnitsPerChar = 1 / xScale;
    double hChars = (2 * radius + lineWidth) / hUnitsPerChar;
    double vChars = 2 * radius + lineWidth;
    // dist represents distance to the center 
    double dist;
    double lineWidth_2 = (double)lineWidth / 2;
    double center = radius + lineWidth_2;
    // for vertical movement 
    for (int j = 0; j <= vChars - 1; j++)
    {
        double y = j + 0.5;
        // for horizontal movement 
        for (int i = 0; i <= hChars - 1; i++)
        {
            double x = (i + 0.5) * hUnitsPerChar;
            dist = Math.Sqrt(
                (x - center) * (x - center) +
                (y - center) * (y - center));

            // dist should be in the range  
            // (radius - lineWidth/2) and (radius + lineWidth/2)  
            // to print stars(*) 
            if (dist > radius - lineWidth_2 &&
                            dist < radius + lineWidth_2)
                Console.Write("*");
            else
                Console.Write(" ");
        }

        Console.WriteLine("");
    }
}
static void Main(string[] args)
{
    printPattern(2, 1, 2);
    printPattern(10, 3, 2);
}

Now the results are like this:

printPattern(2, 1, 2):

  ******
***    ***
**      **
***    ***
  ******

printPattern(10, 3, 2):

                **************
            **********************
         ****************************
      ***********            ***********
     ********                    ********
   ********                        ********
  *******                            *******
 *******                              *******
 ******                                ******
******                                  ******
******                                  ******
******                                  ******
******                                  ******
******                                  ******
 ******                                ******
 *******                              *******
  *******                            *******
   ********                        ********
     ********                    ********
      ***********            ***********
         ****************************
            **********************
                **************

They look better in CMD:

Hope you can translate it to .

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

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