using System;
namespace CirclePrint
{
enum EShape
{
Rectangle,
IsoscelesRightTriangle,
IsoscelesTriangle,
Circle
}
internal class Program
{
static void Main(string[] args)
{
uint width = 21;
uint height = 21;
char[,] canvas = Draw(width, height, EShape.Circle);
}
public static char[,] Draw(uint width, uint height, EShape shape)
{
if (width == 0 || height == 0)
{
return new char[0, 0];
}
uint col = height + 4;
uint row = width + 4;
char[,] canvas = new char[col, row];
uint r = row / 2;
int centerX = (int)row / 2;
int centerY = (int)col / 2;
switch (shape)
{
case EShape.Circle:
if (width != height)
{
return new char[0, 0];
}
else if (width / 2 == 0)
{
return new char[0, 0];
}
for (int y = 0; y < col; y++)
{
for (int x = 0; x < row; x++)
{
// upper side, Bottom side
if (y == 0 || y == col - 1)
{
canvas[y, x] = '-';
Console.Write(canvas[y, x]);
}
// left, right side
if (y != 0 && y != col - 1)
{
if (x == 0 || x == row - 1)
{
canvas[y, x] = '|';
Console.Write(canvas[y, x]);
}
else if (y == 1 || y == col - 2)
{
canvas[y, x] = ' ';
Console.Write(canvas[y, x]);
}
else if (x == 1 || x == row - 2)
{
canvas[y, x] = ' ';
Console.Write(canvas[y, x]);
}
else if (y >= 2 || y <= col - 2) // circle print
{
int dx = x - centerX;
int dy = y - centerY;
if (dx * dx + dy * dy <= r * r)
{
canvas[y, x] = '*';
Console.Write(canvas[y, x]);
}
else
{
canvas[y, x] = ' ';
Console.Write(canvas[y, x]);
}
}
}
}
Console.WriteLine();
}
break;
}
return canvas;
}
}
}
我的能力解决不了,所以问了个问题。此代码根据输入的宽度和高度值在可变形矩形框内输出一个圆。
我想要的是像这里的截图一样打印一个圆圈。如图所示,我写的代码输出了一个愚蠢的八角形......
我无法解决的部分是我写'circle print'作为评论的部分。
我认为找到'centerX,Y'的值可以解决这个问题,但我不确定这是否属实。