1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
| using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public class StackX
{
private int maxSize;
private int[] stackArray;
private int top;
public StackX(int s)
{
maxSize = s;
stackArray = new int[maxSize];
top = -1;
}
public void push(int j)
{
stackArray[++top] = j;
}
public int pop()
{
return stackArray[top--];
}
public long peek()
{
return stackArray[top];
}
public bool isEmpty()
{
return (top == -1);
}
public bool isFull()
{
return (top == maxSize - 1);
}
}
public Form1()
{
InitializeComponent();
}
static Bitmap b = new Bitmap(600, 600);
Graphics gr = Graphics.FromImage(b);
Pen p = new Pen(Color.Black);
int x0, y0, xmax, ymax;
int hx = 3, hy = 3;
StackX stx = new StackX(1000);
StackX sty = new StackX(1000);
int x1max = 200, xmin = 0, y1max = 200, ymin = 0;
Rectangle rec;
Point P1, P2;
//Функция определения знака целого числа
int sign(int r)
{
if (r > 0) return 1;
else if (r < 0) return -1;
else return 0;
}
//Вывод "крупного" пиксела на экран
void point(int ix, int iy)
{
P1 = new Point(x0 + hx * 2 * ix - hx + 1, y0 - hy * 2 * iy - hy + 1);
P2 = new Point(x0 + hx * 2 * ix + hx - 1, y0 - hy * 2 * iy + hy - 1);
rec = new Rectangle(P1, new Size(x0 + hx * 2 * ix + hx - 1 - (x0 + hx * 2 * ix - hx + 1), y0 - hy * 2 * iy + hy - 1 - (y0 - hy * 2 * iy - hy + 1)));
gr.FillRectangle(Brushes.Black, rec);
}
//Генерация точек прямой методом приращений
void line4(int x0, int y0, int x1, int y1)
{
int dx = Math.Abs(x1 - x0), dy = Math.Abs(y1 - y0);
int ex, ey;
if (x1 > x0) ex = 1; else ex = -1;
if (y1 > y0) ey = 1; else ey = -1;
int E = 0, x = x0, y = y0;
while ((y != y1) || (x != x1))
{
gr.DrawRectangle(p, x, y, 1, 1);
if (E > 0) { E -= dx; y += ey; } else { E += dy; x += ex; }
}
gr.DrawRectangle(p, x, y, 1, 1);
}
void flstr(int x, int y)
{
int xleft, xright, tmp, flag = 0, xenter;
stx.push(x);
sty.push(y);
while (!(stx.isEmpty()) && !(sty.isEmpty()))
{
x = stx.pop();
y = sty.pop();
tmp = x;
while (Color.Black.ToArgb() != b.GetPixel(x, y).ToArgb() && x <= x1max)
x++;
xright = x - 1;
x = tmp;
while (Color.Black.ToArgb() != b.GetPixel(x, y).ToArgb() && x > xmin)
x--;
xleft = x + 1;
gr.DrawLine(p, xleft - 1, y, xright + 1, y);
x = xleft;
for (int i = 0; i < 2; i++)
{
if (y < y1max && y > ymin)
{
x = xleft;
y += 1 - i * 2;
while (x <= xright)
{
flag = 0;
while (Color.Black.ToArgb() != b.GetPixel(x, y).ToArgb() && x < xright)
{
if (flag == 0) flag = 1;
x++;
}
if (flag == 1)
{
if (x == xright && Color.Black.ToArgb() != b.GetPixel(x, y).ToArgb())
{
stx.push(x);
sty.push(y);
}
else { stx.push(x - 1); sty.push(y); }
flag = 0;
}
xenter = x;
while (Color.Black.ToArgb() == b.GetPixel(x, y).ToArgb() && x < xright)
x++;
if (x == xenter) x++;
}
}
y--;
}
}
}
private void button1_Click(object sender, EventArgs e)
{
/*xmax = pictureBox1.Width;
ymax = pictureBox1.Height;
x0 = xmax / 2;
y0 = ymax / 2;
gr.DrawLine(p, x0, 0, x0, ymax);
gr.DrawLine(p, 0, y0, xmax, y0);*/
line4(10, 10, 100, 200);
line4(100, 200, 200, 30);
line4(10, 10, 200, 30);
flstr(200, 200);
pictureBox1.Image = b;
}
}
} |