-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path6_FillAlgorithms.cpp
More file actions
168 lines (141 loc) · 4.33 KB
/
Copy path6_FillAlgorithms.cpp
File metadata and controls
168 lines (141 loc) · 4.33 KB
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
165
166
167
168
//
// Created by piyush on 4/2/19.
//
#include<graphics.h>
#include<bits/stdc++.h>
using namespace std;
class Pixel
{
public :
void drawPixel(int x, int y, int colour);
};
void Pixel::drawPixel(int x, int y, int colour) {
putpixel(x, y, 5);
}
//inheriting pixle class
class FillAlgorithms : public Pixel
{
public:
int width, height, X, Y;
void getParameters();
void drawDDALine(float x1, float y1, float x2, float y2);
void floodFill(int x, int y, int old, int fillColour);
void boundaryFill(int x, int y, int fillColour, int backgroundColour);
};
void FillAlgorithms::drawDDALine(float x1, float y1, float x2, float y2) {
float x, y, dx, dy, steps, xm, ym, xin, yin;
x = x1;
y = y1;
drawPixel(x,y,3);
dx = x2 - x1;
dy = y2 - y1;
if(abs(dx) >= abs(dy))
steps = dx;
else
steps = dy;
xin = (float)(dx / steps);
yin = (float)(dy / steps);
for(int i = 1;i <= steps; i++)
{
x = x + xin;
y = y + yin;
xm = x + 0.5;
ym = y + 0.5;
drawPixel(xm, ym, 3);
}
}
void FillAlgorithms::boundaryFill(int x, int y, int fillColour, int backgroundColour) {
if((getpixel(x,y)!=backgroundColour) && (getpixel(x,y)!=fillColour))
{
putpixel(x,y,fillColour);
delay(1);
boundaryFill((x) + 1, y, fillColour, backgroundColour);
boundaryFill(x, (y) + 1, fillColour, backgroundColour);
boundaryFill((x) - 1, y, fillColour, backgroundColour);
boundaryFill(x, (y) - 1, fillColour, backgroundColour);
}
}
void FillAlgorithms::floodFill(int x, int y, int oldColour, int fillColour) {
int current = getpixel(x,y);
if(current == oldColour)
{
putpixel(x,y,fillColour);
delay(10);
floodFill(x - 1, y, oldColour, fillColour);
floodFill(x + 1, y, oldColour, fillColour);
floodFill(x, y + 1, oldColour, fillColour);
floodFill(x, y - 1, oldColour, fillColour);
}
}
void FillAlgorithms::getParameters()
{
cout << "\nEnter the X-Coordinate : ";
cin >> X;
cout << "\nEnter the Y-Coordinate : ";
cin >> Y;
cout << "\nEnter the Heigth : ";
cin >> height;
cout << "\nEnter the Width : ";
cin >> width;
}
int main()
{
FillAlgorithms f;
int sw, dw, xm, ym, xm1, ym1;
int gd=DETECT,gm;
do
{
dw = 1;
cout << "\nFill Algorithms ";
cout << "\nEnter (1). Flood Fill Algorithm";
cout << "\nEnter (2). Boundary Fill Algorithm";
cout << "\nEnter (3). Exit";
cin >> sw;
switch(sw)
{
case 1:
cout << "\nEnter the X-Coordinate : ";
cin >> f.X;
cout << "\nEnter the Y-Coordinate : ";
cin >> f.Y;
cout << "\nEnter the Heigth : ";
cin >> f.height;
cout << "\nEnter the Width : ";
cin >> f.width;
initgraph(&gd,&gm,NULL);
f.drawDDALine(f.X, f.Y, f.X, f.Y+f.height);
f.drawDDALine(f.X, f.Y+f.height, f.X+f.width, f.Y+f.height);
f.drawDDALine(f.X+f.width, f.Y, f.X+f.width, f.Y+f.height);
f.drawDDALine(f.X, f.Y, f.X+f.width, f.Y);
xm = f.X+10;
ym = f.Y+10;
f.floodFill(xm, ym, 3, 5);
break;
case 2 :
cout << "\nEnter the X-Coordinate : ";
cin >> f.X;
cout << "\nEnter the Y-Coordinate : ";
cin >> f.Y;
cout << "\nEnter the Heigth : ";
cin >> f.height;
cout << "\nEnter the Width : ";
cin >> f.width;
initgraph(&gd,&gm,NULL);
f.drawDDALine(f.X, f.Y, f.X, f.Y+f.height);
f.drawDDALine(f.X, f.Y+f.height, f.X+f.width, f.Y+f.height);
f.drawDDALine(f.X+f.width, f.Y, f.X+f.width, f.Y+f.height);
f.drawDDALine(f.X, f.Y, f.X+f.width, f.Y);
xm1 = f.X+10;
ym1 = f.Y+10;
f.floodFill(xm1, ym1, 3, 5);
f.boundaryFill(xm1, ym1, 3, 5);
break;
case 3:
dw = 0;
break;
}
}while(dw);
getchar();
closegraph();
return 0;
}