Растровая развертка окружности и эллипса - C#
Формулировка задачи:
Растровая развертка окружности и эллипса
Пользователь выбирает, какую фигуру строить, задает радиус(ы), щелкает мышью в некоторой точке экрана. Строится выбранная фигура с центром в данной точке, а так же пользователь мышью задает прямоугольную (квадратную) область (нажимает левую кнопку мыши и выделяет область), в которую вписывается эллипс (окружность)
Помогите нормально реализовать на C# с помощью Алгоритма Брозейнхейма.
Алг:
https://mikhail.krivyy.com/2002/12/28/ellipse-c/
Решение задачи: «Растровая развертка окружности и эллипса»
textual
Листинг программы
public partial class Form1 : Form
{
//################ Алгорит Брезенхема для рисования эллипса #################
void putpixel(Graphics grfx, int x, int y, Brush brush)
{
grfx.FillRectangle(brush, x, y, 1, 1);
}
void ellipse(Graphics grfx, int x, int y, int a, int b, Color color)
{
Brush brush = new SolidBrush(color);
int col, i, row, bnew;
long a_square, b_square, two_a_square, two_b_square, four_a_square, four_b_square, d;
b_square = b * b;
a_square = a * a;
row = b;
col = 0;
two_a_square = a_square << 1;
four_a_square = a_square << 2;
four_b_square = b_square << 2;
two_b_square = b_square << 1;
d = two_a_square * ((row - 1) * (row)) + a_square + two_b_square * (1 - a_square);
while (a_square * (row) > b_square * (col))
{
putpixel(grfx, col + x, row + y, brush);
putpixel(grfx, col + x, y - row, brush);
putpixel(grfx, x - col, row + y, brush);
putpixel(grfx, x - col, y - row, brush);
if (d >= 0)
{
row--;
d -= four_a_square * (row);
}
d += two_b_square * (3 + (col << 1));
col++;
}
d = two_b_square * (col + 1) * col + two_a_square * (row * (row - 2) + 1) + (1 - two_a_square) * b_square;
while ((row) + 1 > 0)
{
putpixel(grfx, col + x, row + y, brush);
putpixel(grfx, col + x, y - row, brush);
putpixel(grfx, x - col, row + y, brush);
putpixel(grfx, x - col, y - row, brush);
if (d <= 0)
{
col++;
d += four_b_square * col;
}
row--;
d += two_a_square * (3 - (row << 1));
}
brush.Dispose();
}
public Form1()
{
InitializeComponent();
}
private void Form1_Click(object sender, EventArgs e)
{
using (Graphics grfx = this.CreateGraphics())
{
Point pt = this.PointToClient(Cursor.Position);
ellipse(grfx, pt.X, pt.Y, (int)aNumericUpDown.Value, (int)bNumericUpDown.Value, Color.Green);
}
}
}