Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Parth1906
GitHub Repository: Parth1906/SPPU-2019-Pattern-SE-COMP-Computer-Graphics-Practicals
Path: blob/main/6. Scan Fill Algorithm.cpp
724 views
1
/*
2
Write C++ program to draw a concave polygon and fill it with desired color using scan fill algorithm.
3
*/
4
5
6
7
#include <conio.h>
8
#include <iostream>
9
#include <graphics.h>
10
#include <stdlib.h>
11
using namespace std;
12
13
class point
14
{
15
public:
16
int x,y;
17
};
18
19
class poly
20
{
21
private:
22
point p[20];
23
int inter[20],x,y;
24
int v,xmin,ymin,xmax,ymax;
25
public:
26
int c;
27
void read();
28
void calcs();
29
void display();
30
void ints(float);
31
void sort(int);
32
};
33
34
35
void poly::read()
36
{
37
int i;
38
cout<<"\n\t SCAN_FILL ALGORITHM";
39
cout<<"\n Enter the no of vertices of polygon:";
40
cin>>v;
41
if(v>2)
42
{
43
for(i=0;i<v; i++)
44
{
45
cout<<"\nEnter the co-ordinate no.- "<<i+1<<" : ";
46
cout<<"\n\tx"<<(i+1)<<"=";
47
cin>>p[i].x;
48
cout<<"\n\ty"<<(i+1)<<"=";
49
cin>>p[i].y;
50
}
51
p[i].x=p[0].x;
52
p[i].y=p[0].y;
53
xmin=xmax=p[0].x;
54
ymin=ymax=p[0].y;
55
}
56
else
57
cout<<"\n Enter valid no. of vertices.";
58
}
59
60
void poly::calcs()
61
{ //MAX,MIN
62
for(int i=0;i<v;i++)
63
{
64
if(xmin>p[i].x)
65
xmin=p[i].x;
66
if(xmax<p[i].x)
67
xmax=p[i].x;
68
if(ymin>p[i].y)
69
ymin=p[i].y;
70
if(ymax<p[i].y)
71
ymax=p[i].y;
72
}
73
}
74
75
void poly::display()
76
{
77
int ch1;
78
char ch='y';
79
float s,s2;
80
do
81
{
82
cout<<"\n\nMENU:";
83
cout<<"\n\n\t1 . Scan line Fill ";
84
cout<<"\n\n\t2 . Exit ";
85
cout<<"\n\nEnter your choice:";
86
cin>>ch1;
87
switch(ch1)
88
{
89
case 1:
90
s=ymin+0.01;
91
delay(100);
92
cleardevice();
93
while(s<=ymax)
94
{
95
ints(s);
96
sort(s);
97
s++;
98
}
99
break;
100
case 2:
101
exit(0);
102
}
103
104
cout<<"Do you want to continue?: ";
105
cin>>ch;
106
}while(ch=='y' || ch=='Y');
107
}
108
109
void poly::ints(float z)
110
{
111
int x1,x2,y1,y2,temp;
112
c=0;
113
for(int i=0;i<v;i++)
114
{
115
x1=p[i].x;
116
y1=p[i].y;
117
x2=p[i+1].x;
118
y2=p[i+1].y;
119
if(y2<y1)
120
{
121
temp=x1;
122
x1=x2;
123
x2=temp;
124
temp=y1;
125
y1=y2;
126
y2=temp;
127
}
128
if(z<=y2&&z>=y1)
129
{
130
if((y1-y2)==0)
131
x=x1;
132
else
133
{
134
x=((x2-x1)*(z-y1))/(y2-y1);
135
x=x+x1;
136
}
137
if(x<=xmax && x>=xmin)
138
inter[c++]=x;
139
}
140
}
141
}
142
143
void poly::sort(int z)
144
{
145
int temp,j,i;
146
147
for(i=0;i<v;i++)
148
{
149
line(p[i].x,p[i].y,p[i+1].x,p[i+1].y);
150
}
151
delay(100);
152
for(i=0; i<c;i+=2)
153
{
154
delay(100);
155
line(inter[i],z,inter[i+1],z);
156
}
157
}
158
159
int main()
160
{
161
int cl;
162
initwindow(500,600);
163
cleardevice();
164
poly x;
165
x.read();
166
x.calcs();
167
cleardevice();
168
cout<<"\n\tEnter the colour u want:(0-15)->"; //Selecting colour
169
cin>>cl;
170
setcolor(cl);
171
x.display();
172
closegraph();
173
getch();
174
return 0;
175
}
176
177