"Drawing" 由控制台应用程序中的字符组成的实心椭圆
"Drawing" a filled ellipse made of characters in a Console application
我想将给定的角色绘制到控制台应用程序中,形成一个椭圆。
我不知道如何解决的问题是,我只有在知道角度和半径(使用 Sin 和 Cos 函数)后才知道在哪里画一个字符,但这样我可能会留下空白。
它更复杂,因为我想要 "draw" 一个填充的椭圆,而不仅仅是边框。
我该怎么做?
我想要的方法是这样的:
DrawEllipse(char ch, int centerX, int centerY, int width, int height)
只是一个想法: 我可能会在椭圆的矩形区域中编写一个带有内循环的循环,并确定某个位置是在椭圆区域的内部还是外部。
首先,这里是如何绘制一个实心圆(假设一个 80x25 控制台 window)。其他人可能知道数学允许宽度和高度参数。
static void DrawCircle(char ch, int centerX, int centerY, int radius)
{
for(int y = 0; y < 25; y++)
{
for(int x = 0; x < 80; x++)
{
char c = ' ';
var dX = x - centerX;
var dY = y - centerY;
if(dX * dX + dY * dY < (radius * radius))
{
c = ch;
}
Console.Write(c);
}
}
}
这将是一个合理的近似值。
public static void DrawEllipse( char c, int centerX, int centerY, int width, int height )
{
for( int i = 0; i < width; i++ )
{
int dx = i - width / 2;
int x = centerX + dx;
int h = (int) Math.Round( height * Math.Sqrt( width * width / 4.0 - dx * dx ) / width );
for( int dy = 1; dy <= h; dy++ )
{
Console.SetCursorPosition( x, centerY + dy );
Console.Write( c );
Console.SetCursorPosition( x, centerY - dy );
Console.Write( c );
}
if( h >= 0 )
{
Console.SetCursorPosition( x, centerY );
Console.Write( c );
}
}
}
我想将给定的角色绘制到控制台应用程序中,形成一个椭圆。
我不知道如何解决的问题是,我只有在知道角度和半径(使用 Sin 和 Cos 函数)后才知道在哪里画一个字符,但这样我可能会留下空白。
它更复杂,因为我想要 "draw" 一个填充的椭圆,而不仅仅是边框。
我该怎么做?
我想要的方法是这样的:
DrawEllipse(char ch, int centerX, int centerY, int width, int height)
只是一个想法: 我可能会在椭圆的矩形区域中编写一个带有内循环的循环,并确定某个位置是在椭圆区域的内部还是外部。
首先,这里是如何绘制一个实心圆(假设一个 80x25 控制台 window)。其他人可能知道数学允许宽度和高度参数。
static void DrawCircle(char ch, int centerX, int centerY, int radius)
{
for(int y = 0; y < 25; y++)
{
for(int x = 0; x < 80; x++)
{
char c = ' ';
var dX = x - centerX;
var dY = y - centerY;
if(dX * dX + dY * dY < (radius * radius))
{
c = ch;
}
Console.Write(c);
}
}
}
这将是一个合理的近似值。
public static void DrawEllipse( char c, int centerX, int centerY, int width, int height )
{
for( int i = 0; i < width; i++ )
{
int dx = i - width / 2;
int x = centerX + dx;
int h = (int) Math.Round( height * Math.Sqrt( width * width / 4.0 - dx * dx ) / width );
for( int dy = 1; dy <= h; dy++ )
{
Console.SetCursorPosition( x, centerY + dy );
Console.Write( c );
Console.SetCursorPosition( x, centerY - dy );
Console.Write( c );
}
if( h >= 0 )
{
Console.SetCursorPosition( x, centerY );
Console.Write( c );
}
}
}