/
BFILL.CPP
155 lines (155 loc) · 2.94 KB
/
BFILL.CPP
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
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<malloc.h>
typedef struct Point Point;
struct Point
{
int xco,yco;
Point *next;
};
Point *head=NULL,*tail=NULL;
int boundary=WHITE;
void FloodFill();
void Enqueue(int a,int b);
Point * Dequeue();
void DoTheNeeded(int a,int b);
int IsPresent(int a,int b);
void main()
{
int x[10],y[10],nedges;
int gd=DETECT,gm,count,xin,yin,bg=4;
printf("\nEnter the number of edges you will like to have in your polygon");
scanf("%d",&nedges);
for(;nedges<=2;)
{
printf("\nPlease enter the correct number of edges");
scanf("%d",&nedges);
}
for(count=0;count<nedges;count++)
{
printf("\nEnter the x co-ordinate of the %d vertex",count+1);
scanf("%d",&x[count]);
printf("\nEnter the y co-ordinate of the %d vextex",count+1);
scanf("%d",&y[count]);
}
initgraph(&gd,&gm,"C:\\TC\\BGI");
for(count=0;count<nedges;count++)
{
line(x[count],y[count],x[(count+1)%nedges],y[(count+1)%nedges]);
}
FloodFill();
printf("\nEnd of process");
getch();
}
void FloodFill()
{
int xin,yin,current,bg=4,count=0;
Point *Consider;
printf("\nEnter the x-cordinate of internal point of the polygon");
scanf("\n%d",&xin);
printf("\nEnter the y-cordinate of internal point of the polygon");
scanf("\n%d",&yin);
do
{
if(head==NULL)
{
DoTheNeeded(xin,yin);
putpixel(xin,yin,bg);
}
else
{
Consider=Dequeue();
DoTheNeeded(Consider->xco,Consider->yco);
putpixel(Consider->xco,Consider->yco,bg);
free(Consider);
}
count++;
}while(head!=NULL);
}
void DoTheNeeded(int x,int y)
{
if(getpixel(x-1,y)==boundary /*|| getpixel(x-1,y+1)==boundary|| getpixel(x-1,y-1)==boundary*/ || getpixel(x,y+1)==boundary || getpixel(x,y-1)==boundary/*|| getpixel(x+1,y+1)==boundary || getpixel(x+1,y-1)==boundary*/ ||getpixel(x+1,y)==boundary)
{
return;
}
if(getpixel(x-1,y)!=boundary)
{
if(IsPresent(x-1,y))
Enqueue(x-1,y);
}
/*if(getpixel(x-1,y+1)!=boundary)
{
if(IsPresent(x-1,y+1))
Enqueue(x-1,y+1);
}
if(getpixel(x-1,y-1)!=boundary)
{
if(IsPresent(x-1,y-1))
Enqueue(x-1,y-1);
}*/
if(getpixel(x,y+1)!=boundary)
{
if(IsPresent(x,y+1))
Enqueue(x,y+1);
}
if(getpixel(x,y-1)!=boundary)
{
if(IsPresent(x,y-1))
Enqueue(x,y-1);
}
/*if(getpixel(x+1,y+1)!=boundary)
{
if(IsPresent(x+1,y+1))
Enqueue(x+1,y+1);
}*/
if(getpixel(x+1,y)!=boundary)
{
if(IsPresent(x+1,y))
Enqueue(x+1,y);
}
/*if(getpixel(x+1,y-1)!=boundary)
{
if(IsPresent(x+1,y-1))
Enqueue(x+1,y-1);
}*/
}
void Enqueue(int a,int b)
{
Point *Raw;
Raw=(Point*)malloc(sizeof(Point));
if(Raw==NULL)
printf("\nMemory could not be allocated");
if(head==NULL)
{
head=Raw;
tail=head;
}
else
{
tail->next=Raw;
tail=tail->next;
}
tail->xco=a;
tail->yco=b;
tail->next=NULL;
}
Point* Dequeue()
{
Point *re;
re=head;
head=head->next;
return re;
}
int IsPresent(int a,int b)
{
Point *check;
for(check=head;check!=NULL;check=check->next)
{
if(a==check->xco && b==check->yco)
{
return 0;
}
}
return 1;
}