This repository has been archived by the owner on Mar 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
flood.c
133 lines (125 loc) · 2.89 KB
/
flood.c
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
#include "vga256.h"
static int BorderColor;
static int StackPtr;
static POINT Stack[256];
static int UpXLimit, UpXLimit1;
static int ScanDownForBorder(int x, int y)
{
for( ; x >= vp.x1; x--)
if(GetPixel(x, y) == BorderColor)
break;
return (x + 1);
}
static int ScanUpForBorder(int x, int y)
{
for( ; x <= vp.x2; x++)
if(GetPixel(x, y) == BorderColor)
break;
return (x - 1);
}
static int ScanUpForNotBorder(int x, int xmax, int y)
{
for( ; x <= xmax; x++)
if(GetPixel(x, y) != BorderColor)
return (x);
return (-1);
}
static int PopPoint(int x, int y, int DeltaY)
{
int i;
if(DeltaY < 0)
x = ~x;
for(i = 0; i < StackPtr; i++)
if((Stack[i].x == x) && (Stack[i].y == y))
{
for(StackPtr--; i < StackPtr; i++)
Stack[i] = Stack[i + 1];
return (1);
}
return (0);
}
static void PushPoint(int x, int maxx, int y, int DeltaY)
{
while((x = ScanUpForNotBorder(x, maxx, y)) >= 0)
{
Stack[StackPtr].x = DeltaY >= 0 ? x : ~x;
Stack[StackPtr++].y = y;
x = ScanUpForBorder(x, y) + 1;
}
}
static void Scanner(int x, int y, int DeltaY)
{
int ny, lx;
do
{
ny = y + DeltaY;
if((ny < vp.y1) || (ny > vp.y2))
break;
if(GetPixel(x, ny) != BorderColor)
lx = ScanDownForBorder(x, ny);
else
if((lx = ScanUpForNotBorder(x, UpXLimit, ny)) < 0)
break;
UpXLimit1 = UpXLimit;
UpXLimit = ScanUpForBorder(lx, ny);
PatBar(lx, ny, UpXLimit, ny);
PushPoint(lx, x - 1, y, -DeltaY);
if(UpXLimit1 != UpXLimit)
{
int i, j, x1, x2, dy;
if(UpXLimit1 <= UpXLimit)
{
x1 = UpXLimit;
x2 = UpXLimit1;
dy = DeltaY;
}
else
{
x1 = UpXLimit1;
x2 = UpXLimit;
dy = -DeltaY;
y = ny;
}
if((j = ScanUpForBorder(i = x1, y)) > x1)
{
while((j = ScanUpForBorder(i = j, y + dy)) > i &&
(j = ScanUpForBorder(i = j, y)) > i) ;
PushPoint(x1 + 1, i, y + dy, dy);
}
PushPoint(x2 + 1, i, y, -dy);
}
} while(PopPoint(x = lx, y = ny, -DeltaY) == 0);
}
void FloodFill(int x, int y, unsigned char color)
{
int DeltaY = -1;
BorderColor = color;
if(GetPixel(x, y) != BorderColor)
{
Stack[0].x = x = ScanDownForBorder(x, y);
Stack[0].y = y;
StackPtr = 1;
UpXLimit = ScanUpForBorder(x, y);
while(1)
{
Scanner(x, y, DeltaY);
do
{
if(StackPtr == 0)
return;
x = Stack[--StackPtr].x;
y = Stack[StackPtr].y;
if(x >= 0)
DeltaY = 1;
else
{
DeltaY = -1;
x = ~x;
}
UpXLimit1 = UpXLimit;
UpXLimit = ScanUpForBorder(x, y);
PatBar(x, y, UpXLimit, y);
} while(PopPoint(x, y, -DeltaY) != 0);
}
}
}