打印出指定宽度的 ASCII 圆
Print out an ASCII circle of the specified width
我正在尝试更改以下代码以便获得半径 2 的输出:
*****
*** ***
** **
*** ***
*****
任何帮助将不胜感激,因为我快要发疯了!
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("");
}
}
}
输出看起来是椭圆形的,因为字符的宽度和高度不同。想一想它的像素不是正方形,而是垂直矩形的图像。我借用了@Prasanth Rajendran 链接的 c# 版本的代码,并做了一些修改:
- 添加了
lineWidth
个参数
- 添加了
xScale
参数,现在每行1/xScale
个字符相当于1
个数据点
- 通过添加 [0.5, 0.5] 偏移将字符位置移动到其中心
// 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);
}
现在的结果是这样的:
printPattern(2, 1, 2):
******
*** ***
** **
*** ***
******
printPattern(10, 3, 2):
**************
**********************
****************************
*********** ***********
******** ********
******** ********
******* *******
******* *******
****** ******
****** ******
****** ******
****** ******
****** ******
****** ******
****** ******
******* *******
******* *******
******** ********
******** ********
*********** ***********
****************************
**********************
**************
它们在 CMD 中看起来更好:
希望你能翻译成java。
您可以在其内部绘制圆的边,并将圆的方程表示为:
double edge = (x*x + y*y) / (double) r - r;
如果半径r=2
,线宽w=1
,那么圆是这样的:
******
**** ****
** **
**** ****
******
r=2,w=1.0
假设两个字符的宽度等于一个字符的高度。
// radius
int r = 12;
// line width
double w = 3;
// assume that the width of two characters
// is equal to the height of one character
for (int y = -r; y <= r; y++) {
for (int x = -r; x <= r; x++) {
double edge = (x*x + y*y) / (double) r - r;
// edge is inside the circle
if (edge > - w*4/3 && edge < 1) {
System.out.print("**");
} else {
System.out.print(" ");
}
}
System.out.println();
}
System.out.println("r="+r+",w="+w);
输出:
**************
**********************
******************************
********** **********
******** ********
******** ********
****** ******
****** ******
****** ******
****** ******
****** ******
****** ******
****** ******
****** ******
****** ******
****** ******
****** ******
****** ******
****** ******
******** ********
******** ********
********** **********
******************************
**********************
**************
r=12,w=3.0
另请参阅:
• Print out an ASCII circle and axes with characters
• Printing multiline ASCII animation in the console
我正在尝试更改以下代码以便获得半径 2 的输出:
*****
*** ***
** **
*** ***
*****
任何帮助将不胜感激,因为我快要发疯了!
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("");
}
}
}
输出看起来是椭圆形的,因为字符的宽度和高度不同。想一想它的像素不是正方形,而是垂直矩形的图像。我借用了@Prasanth Rajendran 链接的 c# 版本的代码,并做了一些修改:
- 添加了
lineWidth
个参数 - 添加了
xScale
参数,现在每行1/xScale
个字符相当于1
个数据点 - 通过添加 [0.5, 0.5] 偏移将字符位置移动到其中心
// 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);
}
现在的结果是这样的:
printPattern(2, 1, 2):
******
*** ***
** **
*** ***
******
printPattern(10, 3, 2):
**************
**********************
****************************
*********** ***********
******** ********
******** ********
******* *******
******* *******
****** ******
****** ******
****** ******
****** ******
****** ******
****** ******
****** ******
******* *******
******* *******
******** ********
******** ********
*********** ***********
****************************
**********************
**************
它们在 CMD 中看起来更好:
希望你能翻译成java。
您可以在其内部绘制圆的边,并将圆的方程表示为:
double edge = (x*x + y*y) / (double) r - r;
如果半径r=2
,线宽w=1
,那么圆是这样的:
******
**** ****
** **
**** ****
******
r=2,w=1.0
假设两个字符的宽度等于一个字符的高度。
// radius
int r = 12;
// line width
double w = 3;
// assume that the width of two characters
// is equal to the height of one character
for (int y = -r; y <= r; y++) {
for (int x = -r; x <= r; x++) {
double edge = (x*x + y*y) / (double) r - r;
// edge is inside the circle
if (edge > - w*4/3 && edge < 1) {
System.out.print("**");
} else {
System.out.print(" ");
}
}
System.out.println();
}
System.out.println("r="+r+",w="+w);
输出:
**************
**********************
******************************
********** **********
******** ********
******** ********
****** ******
****** ******
****** ******
****** ******
****** ******
****** ******
****** ******
****** ******
****** ******
****** ******
****** ******
****** ******
****** ******
******** ********
******** ********
********** **********
******************************
**********************
**************
r=12,w=3.0
另请参阅:
• Print out an ASCII circle and axes with characters
• Printing multiline ASCII animation in the console