LuckyStrike
Junior Member | Редактировать | Профиль | Сообщение | Цитировать | Сообщить модератору Доброго времени суток, уважаемые программисты...прошу помочь с задачкой: Реализовать в виде процедуры «Построчный алгоритм заполнения с затравкой». вот алгоритм: Алгоритм: Push Затравка(x,y) while <стек не пуст> Pop Пиксел(x,y) Пиксел(x,y)=Цвет_закраски Врем_x=x x=x+1 while Пиксел(x,y)<>Цвет_границы Пиксел(x,y)=Цвет_закраски x=x+1 end while Xправ=x-1 x=Врем_x x=x-1 while Пиксел(x,y)<>Цвет_границы Пиксел(x,y)=Цвет_закраски x=x-1 end while Xлев=x+1 {ищем затравку на строке выше} x=Xлев y=y+1 while x<=Xправ Флаг=0 while (Пиксел(x,y)<>Цвет_закраски and Пиксел(x,y)<>Цвет_границы and x<Xправ) if Флаг=0 then Флаг=1 x=x+1 end while if Флаг=1 then if (x=Xправ and Пиксел(x,y)<>Цвет_закраски and Пиксел(x,y)<>Цвет_границы) then Push Пиксел(x,y) else Push Пиксел(x-1,y) end if Флаг=0 end if Xвход=x while (Пиксел(x,y)=Цвет_закраски or Пиксел(x,y)=Цвет_границы) and (x<Xправ) x=x+1 end while if x=Xвход then x=x+1 end if end while {аналогично ищем затравку на строке ниже заполненной — y=y-1} end while finish а нужно сделать в С#...вот что я сделал(можно и необязательно делать в точности как алгоритм) using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Drawing.Imaging; using System.Runtime.InteropServices; namespace лаба5_2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } void floodFillScanline(int x, int y, int newColor, int oldColor) { int h=0; int w = 0; int[,] screenBuffer=new int [0,0]; if (oldColor == newColor) return; if (screenBuffer[x,y] != oldColor) return; int y1; //draw current scanline from start position to the top y1 = y; while (y1 < h && screenBuffer[x,y1] == oldColor) { screenBuffer[x,y1] = newColor; y1++; } //draw current scanline from start position to the bottom y1 = y - 1; while (y1 >= 0 && screenBuffer[x,y1] == oldColor) { screenBuffer[x,y1] = newColor; y1--; } //test for new scanlines to the left y1 = y; while (y1 < h && screenBuffer[x,y1] == newColor) { if (x > 0 && screenBuffer[x - 1,y1] == oldColor) { floodFillScanline(x - 1, y1, newColor, oldColor); } y1++; } y1 = y - 1; while (y1 >= 0 && screenBuffer[x,y1] == newColor) { if (x > 0 && screenBuffer[x - 1,y1] == oldColor) { floodFillScanline(x - 1, y1, newColor, oldColor); } y1--; } //test for new scanlines to the right y1 = y; while (y1 < h && screenBuffer[x,y1] == newColor) { if (x < w - 1 && screenBuffer[x + 1,y1] == oldColor) { floodFillScanline(x + 1, y1, newColor, oldColor); } y1++; } y1 = y - 1; while (y1 >= 0 && screenBuffer[x,y1] == newColor) { if (x < w - 1 && screenBuffer[x + 1,y1] == oldColor) { floodFillScanline(x + 1, y1, newColor, oldColor); } y1--; } } private void panel1_Paint(object sender, PaintEventArgs e) { using (Bitmap bitmap = new Bitmap(500, 500)) { using (Graphics g = Graphics.FromImage(bitmap)) { g.Clear(Color.White); List<Point> points = new List<Point>(); for (double i = 0; i < 10; i++) { double dist = (i % 2 == 0) ? 100 : 50; double x = 200 + Math.Cos(i / 10d * Math.PI * 2d) * dist; double y = 200 + Math.Sin(i / 10d * Math.PI * 2d) * dist; points.Add(new Point((int)x, (int)y)); } g.DrawPolygon(Pens.Blue, points.ToArray()); } floodFillScanline(0, 0, 330, 330); e.Graphics.DrawImage(bitmap, 0, 0); } } } } |