gpt4 book ai didi

c# - 在 C# 中将填充和未填充的椭圆的点渲染到数组

转载 作者:行者123 更新时间:2023-12-02 05:39:51 25 4
gpt4 key购买 nike

有人知道在 C# 中将 Ellipse 渲染为数组的任何代码吗?我四处张望,找不到任何可以解决我的问题的东西。

给定以下数组:

bool[,] pixels = new bool[100, 100];

我正在寻找在矩形区域内同时呈现空心椭圆和实心椭圆的函数。例如:

public void Ellipse(bool[,] pixels, Rectangle area)
{
// fill pixels[x,y] = true here for the ellipse within area.
}

public void FillEllipse(bool[,] pixels, Rectangle area)
{
// fill pixels[x,y] = true here for the ellipse within area.
}

Ellipse(pixels, new Rectangle(20, 20, 60, 60));
FillEllipse(pixels, new Rectangle(40, 40, 20, 20));

如有任何帮助,我们将不胜感激。

最佳答案

像这样的东西应该可以解决问题

public class EllipseDrawer
{
private static PointF GetEllipsePointFromX(float x, float a, float b)
{
//(x/a)^2 + (y/b)^2 = 1
//(y/b)^2 = 1 - (x/a)^2
//y/b = -sqrt(1 - (x/a)^2) --Neg root for upper portion of the plane
//y = b*-sqrt(1 - (x/a)^2)
return new PointF(x, b * -(float)Math.Sqrt(1 - (x * x / a / a)));
}

public static void Ellipse(bool[,] pixels, Rectangle area)
{
DrawEllipse(pixels, area, false);
}

public static void FillEllipse(bool[,] pixels, Rectangle area)
{
DrawEllipse(pixels, area, true);
}

private static void DrawEllipse(bool[,] pixels, Rectangle area, bool fill)
{
// Get the size of the matrix
var matrixWidth = pixels.GetLength(0);
var matrixHeight = pixels.GetLength(1);

var offsetY = area.Top;
var offsetX = area.Left;

// Figure out how big the ellipse is
var ellipseWidth = (float)area.Width;
var ellipseHeight = (float)area.Height;

// Figure out the radiuses of the ellipses
var radiusX = ellipseWidth / 2;
var radiusY = ellipseHeight / 2;

//Keep track of the previous y position
var prevY = 0;
var firstRun = true;

// Loop through the points in the matrix
for (var x = 0; x <= radiusX; ++x)
{
var xPos = x + offsetX;
var rxPos = (int)ellipseWidth - x - 1 + offsetX;

if (xPos < 0 || rxPos < xPos || xPos >= matrixWidth)
{
continue;
}

var pointOnEllipseBoundCorrespondingToXMatrixPosition = GetEllipsePointFromX(x - radiusX, radiusX, radiusY);
var y = (int) Math.Floor(pointOnEllipseBoundCorrespondingToXMatrixPosition.Y + (int)radiusY);
var yPos = y + offsetY;

var ryPos = (int)ellipseHeight - y - 1 + offsetY;

if (yPos >= 0)
{
if (xPos > -1 && xPos < matrixWidth && yPos > -1 && yPos < matrixHeight)
{
pixels[xPos, yPos] = true;
}

if(xPos > -1 && xPos < matrixWidth && ryPos > -1 && ryPos < matrixHeight)
{
pixels[xPos, ryPos] = true;
}

if (rxPos > -1 && rxPos < matrixWidth)
{
if (yPos > -1 && yPos < matrixHeight)
{
pixels[rxPos, yPos] = true;
}

if (ryPos > -1 && ryPos < matrixHeight)
{
pixels[rxPos, ryPos] = true;
}
}
}

//While there's a >1 jump in y, fill in the gap (assumes that this is not the first time we've tracked y, x != 0)
for (var j = prevY - 1; !firstRun && j > y - 1 && y > 0; --j)
{
var jPos = j + offsetY;
var rjPos = (int)ellipseHeight - j - 1 + offsetY;

if(jPos == rjPos - 1)
{
continue;
}

if(jPos > -1 && jPos < matrixHeight)
{
pixels[xPos, jPos] = true;
}

if(rjPos > -1 && rjPos < matrixHeight)
{
pixels[xPos, rjPos] = true;
}

if (rxPos > -1 && rxPos < matrixWidth)
{
if(jPos > -1 && jPos < matrixHeight)
{
pixels[rxPos, jPos] = true;
}

if(rjPos > -1 && rjPos < matrixHeight)
{
pixels[rxPos, rjPos] = true;
}
}
}

firstRun = false;
prevY = y;
var countTarget = radiusY - y;

for (var count = 0; fill && count < countTarget; ++count)
{
++yPos;
--ryPos;

// Set all four points in the matrix we just learned about
// also, make the indication that for the rest of this row, we need to fill the body of the ellipse
if(yPos > -1 && yPos < matrixHeight)
{
pixels[xPos, yPos] = true;
}

if(ryPos > -1 && ryPos < matrixHeight)
{
pixels[xPos, ryPos] = true;
}

if (rxPos > -1 && rxPos < matrixWidth)
{
if(yPos > -1 && yPos < matrixHeight)
{
pixels[rxPos, yPos] = true;
}

if(ryPos > -1 && ryPos < matrixHeight)
{
pixels[rxPos, ryPos] = true;
}
}
}
}
}
}

关于c# - 在 C# 中将填充和未填充的椭圆的点渲染到数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11166275/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com