jDe*_*per 5 java algorithm formatting geometry ascii-art
我正在尝试更改以下代码,以便获得半径 2 的输出:
*****
*** ***
** **
*** ***
*****
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激,因为我快要疯了!
*****
*** ***
** **
*** ***
*****
Run Code Online (Sandbox Code Playgroud)
输出看起来是椭圆形的,因为字符的宽度和高度不同。想象一个图像,它的像素不是正方形,而是垂直的矩形。我借用了@Prasanth Rajendran链接的c#版本代码,并做了一些修改:
lineWidth参数xScale参数,现在1/xScale一行中的每个字符都相当于1数据点// 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);
}
Run Code Online (Sandbox Code Playgroud)
现在结果是这样的:
printPattern(2, 1, 2):
******
*** ***
** **
*** ***
******
printPattern(10, 3, 2):
**************
**********************
****************************
*********** ***********
******** ********
******** ********
******* *******
******* *******
****** ******
****** ******
****** ******
****** ******
****** ******
****** ******
****** ******
******* *******
******* *******
******** ********
******** ********
*********** ***********
****************************
**********************
**************
Run Code Online (Sandbox Code Playgroud)
它们在 CMD 中看起来更好:
希望你能把它翻译成java。
| 归档时间: |
|
| 查看次数: |
370 次 |
| 最近记录: |