-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfloodfill_and_boundaryfill.py
234 lines (204 loc) · 5.92 KB
/
floodfill_and_boundaryfill.py
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
from graphics import *
window_w = 800
window_h = 800
points=[]
def DrawAxis(win):
for i in range(window_h):
pt = Point(window_w/2,i)
pt.draw(win)
for i in range(window_w):
pt = Point(i,window_h/2)
pt.draw(win)
def ConvertPixel(xx,yy):
xx+=window_w/2
yy = -yy
yy+=window_h/2
return xx,yy
def BresenhamLine(x1,y1,x2,y2,win):
x, y = x1, y1
dx = abs(x2 - x1)
dy = abs(y2 - y1)
if x1==x2 and y1==y2 :
PutPixle(win,x,y)
elif y1==y2:
if x1<x2:
while x != x2 :
PutPixle(win,x,y)
x+=1
else:
while x != x2 :
PutPixle(win,x,y)
x-=1
elif (x1==x2):
if y1<y2:
while y!=y2:
PutPixle(win,x,y)
y+=1
else:
while y!=y2:
PutPixle(win,x,y)
y-=1
elif dy<dx:
xIncrement = 0
yIncrement = 0
if x1<x2:
xIncrement = 1
else:
xIncrement = -1
if y1<y2:
yIncrement = 1
else:
yIncrement = -1
p = 2*dy-dx
while x != x2:
if p>0:
p=p+2*(dy-dx)
y+=yIncrement
else:
p = p+2*dy
x+=xIncrement
PutPixle(win,x,y)
else:
xIncrement = 0
yIncrement = 0
if x1<x2:
xIncrement = 1
else:
xIncrement = -1
if y1<y2:
yIncrement = 1
else:
yIncrement = -1
p = 2*dx-dy
while y != y2:
if p>0:
p=p+2*(dx-dy)
x+=xIncrement
else:
p = p+2*dx
y+=yIncrement
PutPixle(win,x,y)
PutPixle(win,x2,y2)
def PutPixle(win, x, y):
newx, newy = ConvertPixel(x,y)
points[int(newx)][int(newy)]="bc"
print(newx,newy)
pt = Point(newx,newy)
pt.draw(win)
def flood_fill(window):
print("Click anywhere inside the polgon to fill it::\n")
seed_point=(window.getMouse())
lis=[]
lis.append(seed_point)
fc="fc"
oc="blue"
while(len(lis)!=0):
pt=lis[len(lis)-1]
lis.pop()
x = int(pt.getX())
y = int(pt.getY())
if(points[x][y]!=fc and points[x][y]==oc):
window.plot(x,y,"red")
points[x][y]=fc
pt=Point(x+1,y)
if(points[x+1][y]==oc and points[x+1][y]!=fc):
lis.append(pt)
pt=Point(x,y+1)
if(points[x][y+1]==oc and points[x][y+1]!=fc):
lis.append(pt)
pt=Point(x-1,y)
if(points[x-1][y]==oc and points[x-1][y]!=fc):
lis.append(pt)
pt=Point(x,y-1)
if(points[x][y-1]==oc and points[x][y-1]!=fc):
lis.append(pt)
def boundary_fill(window):
print("Click anywhere inside the polgon to fill it::\n")
seed_point=(window.getMouse())
lis=[]
lis.append(seed_point)
fc="fc"
bc="bc"
while(len(lis)!=0):
pt=lis[len(lis)-1]
lis.pop()
x = int(pt.getX())
y = int(pt.getY())
if(points[x][y]!=fc and points[x][y]!=bc):
window.plot(x,y,"red")
points[x][y]=fc
pt=Point(x,y+1)
if(points[x][y+1]!=bc and points[x][y+1]!=fc):
lis.append(pt)
pt=Point(x+1,y)
if(points[x+1][y]!=bc and points[x+1][y]!=fc):
lis.append(pt)
pt=Point(x,y-1)
if(points[x][y-1]!=bc and points[x][y-1]!=fc):
lis.append(pt)
pt=Point(x-1,y)
if(points[x-1][y]!=bc and points[x-1][y]!=fc):
lis.append(pt)
def FloodFillInit():
for i in range(window_w):
points.append([])
for j in range(window_h):
points[i].append("blue")
print("Enter no. of sides in polygon:")
s=int(input("Enter sides:"))
print("Enter the points:")
po=[]
for i in range(s):
print("enter x coordinate of point-",i+1,":")
x=int(input())
print("enter y coordinate of point-",i+1,":")
y=int(input())
a=[x,y]
po.append(a)
window=GraphWin("Flood Fill Polygon Filling Algorithm",window_w,window_h)
window.setBackground("blue")
DrawAxis(window)
for i in range(s):
x1 = po[i][0]
y1 = po[i][1]
x2 = po[(i+1)%s][0]
y2 = po[(i+1)%s][1]
BresenhamLine(x1,y1,x2,y2,window)
flood_fill(window)
window.getMouse()
window.close()
def BoundaryFillInit():
for i in range(window_w):
points.append([])
for j in range(window_h):
points[i].append(" ")
print("Enter no. of sides in polygon:")
s=int(input("Enter sides:"))
print("Enter the points:")
po=[]
for i in range(s):
print("enter x coordinate of point-",i+1,":")
x=int(input())
print("enter y coordinate of point-",i+1,":")
y=int(input())
a=[x,y]
po.append(a)
window=GraphWin("Boundary Fill Polygon Filling Algorithm",window_w,window_h) #for viewport(device coordinates)
DrawAxis(window)
for i in range(s):
x1 = po[i][0]
y1 = po[i][1]
x2 = po[(i+1)%s][0]
y2 = po[(i+1)%s][1]
BresenhamLine(x1,y1,x2,y2,window)
boundary_fill(window)
window.getMouse()
window.close()
def main():
option = int(input("Enter 1 for FloodFill and Enter 2 for BoundaryFill Algorithm \n"))
if option==1:
FloodFillInit()
elif option==2:
BoundaryFillInit()
if __name__ == "__main__":
main()