我正在try 将椭圆绘制成2Darray.我将x和y作为左上角的开始位置,将右下角的x和y作为结束位置.
我正在try 绘制一个完全适合该矩形空间的椭圆. 我使用的算法在开始和结束坐标均匀时有效,但当宽度坐标不均匀时,其一个像素太短,高度相同.
这是我的代码:
private void DrawEllipse(in NexusCoord start, in NexusCoord end, in NexusChar character)
{
ThrowIfOutOfBounds(start);
ThrowIfOutOfBounds(end);
GetOrThrowColorIndex(character.Foreground, character.Background, nameof(character), out var foregroundColorIndex, out var backgroundColorIndex);
var glyph = new Glyph(character.Value, foregroundColorIndex, backgroundColorIndex);
var width = end.X - start.X;
var height = end.Y - start.Y;
var centerX = width / 2;
var centerY = height / 2;
var radiusX = start.X + centerX;
var radiusY = start.Y + centerY;
var x = 0;
var y = centerY;
var d1 = centerY * centerY - centerX * centerX * centerY;
var dx = 2 * centerY * centerY * x;
var dy = 2 * centerX * centerX * y;
while (dx < dy)
{
SetGlyph(new NexusCoord(x + radiusX, y + radiusY), glyph);
SetGlyph(new NexusCoord(-x + radiusX, y + radiusY), glyph);
SetGlyph(new NexusCoord(x + radiusX, -y + radiusY), glyph);
SetGlyph(new NexusCoord(-x + radiusX, -y + radiusY), glyph);
if (d1 < 0)
{
x++;
dx += 2 * centerY * centerY;
d1 = d1 + dx + centerY * centerY;
}
else
{
x++;
y--;
dx += 2 * centerY * centerY;
dy -= 2 * centerX * centerX;
d1 = d1 + dx - dy + centerY * centerY;
}
}
var d2 = centerY * centerY * ((x + 0.5) * (x + 0.5)) + centerX * centerX * ((y - 1) * (y - 1)) - centerX * centerX * centerY * centerY;
while (y >= 0)
{
SetGlyph(new NexusCoord(x + radiusX, y + radiusY), glyph);
SetGlyph(new NexusCoord(-x + radiusX, y + radiusY), glyph);
SetGlyph(new NexusCoord(x + radiusX, -y + radiusY), glyph);
SetGlyph(new NexusCoord(-x + radiusX, -y + radiusY), glyph);
if (d2 > 0)
{
y--;
dy -= 2 * centerX * centerX;
d2 = d2 + centerX * centerX - dy;
}
else
{
y--;
x++;
dx += 2 * centerY * centerY;
dy -= 2 * centerX * centerX;
d2 = d2 + dx - dy + centerX * centerX;
}
}
}
当我在控制台Graphic.DrawShape(NexusShape.Ellipse, new NexusCoord(10, 10), new NexusCoord(45, 35)));
中调用该代码来绘制它时,它看起来像这样:
粉色矩形是应该填充的空间.
我try 使用双倍和舍入,但也不起作用.