Растровая развертка окружности и эллипса - C#

Узнай цену своей работы

Формулировка задачи:

Растровая развертка окружности и эллипса Пользователь выбирает, какую фигуру строить, задает радиус(ы), щелкает мышью в некоторой точке экрана. Строится выбранная фигура с центром в данной точке, а так же пользователь мышью задает прямоугольную (квадратную) область (нажимает левую кнопку мыши и выделяет область), в которую вписывается эллипс (окружность) Помогите нормально реализовать на C# с помощью Алгоритма Брозейнхейма. Алг: https://mikhail.krivyy.com/2002/12/28/ellipse-c/

Решение задачи: «Растровая развертка окружности и эллипса»

textual
Листинг программы
  1.     public partial class Form1 : Form
  2.     {
  3.         //################ Алгорит Брезенхема для рисования эллипса #################
  4.         void putpixel(Graphics grfx, int x, int y, Brush brush)
  5.         {
  6.             grfx.FillRectangle(brush, x, y, 1, 1);
  7.         }
  8.  
  9.         void ellipse(Graphics grfx, int x, int y, int a, int b, Color color)
  10.         {
  11.             Brush brush = new SolidBrush(color);
  12.  
  13.             int col, i, row, bnew;
  14.             long a_square, b_square, two_a_square, two_b_square, four_a_square, four_b_square, d;
  15.  
  16.             b_square = b * b;
  17.             a_square = a * a;
  18.             row = b;
  19.             col = 0;
  20.             two_a_square = a_square << 1;
  21.             four_a_square = a_square << 2;
  22.             four_b_square = b_square << 2;
  23.             two_b_square = b_square << 1;
  24.             d = two_a_square * ((row - 1) * (row)) + a_square + two_b_square * (1 - a_square);
  25.             while (a_square * (row) > b_square * (col))
  26.             {
  27.                 putpixel(grfx, col + x, row + y, brush);
  28.                 putpixel(grfx, col + x, y - row, brush);
  29.                 putpixel(grfx, x - col, row + y, brush);
  30.                 putpixel(grfx, x - col, y - row, brush);
  31.                 if (d >= 0)
  32.                 {
  33.                     row--;
  34.                     d -= four_a_square * (row);
  35.                 }
  36.                 d += two_b_square * (3 + (col << 1));
  37.                 col++;
  38.             }
  39.             d = two_b_square * (col + 1) * col + two_a_square * (row * (row - 2) + 1) + (1 - two_a_square) * b_square;
  40.             while ((row) + 1 > 0)
  41.             {
  42.                 putpixel(grfx, col + x, row + y, brush);
  43.                 putpixel(grfx, col + x, y - row, brush);
  44.                 putpixel(grfx, x - col, row + y, brush);
  45.                 putpixel(grfx, x - col, y - row, brush);
  46.                 if (d <= 0)
  47.                 {
  48.                     col++;
  49.                     d += four_b_square * col;
  50.                 }
  51.                 row--;
  52.                 d += two_a_square * (3 - (row << 1));
  53.             }
  54.  
  55.             brush.Dispose();
  56.         }
  57.  
  58.         public Form1()
  59.         {
  60.             InitializeComponent();
  61.         }
  62.  
  63.         private void Form1_Click(object sender, EventArgs e)
  64.         {
  65.             using (Graphics grfx = this.CreateGraphics())
  66.             {
  67.                 Point pt = this.PointToClient(Cursor.Position);
  68.                 ellipse(grfx, pt.X, pt.Y, (int)aNumericUpDown.Value, (int)bNumericUpDown.Value, Color.Green);
  69.             }
  70.         }
  71.     }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

11   голосов , оценка 3.818 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы