Scanfill 1
Scanfill 1
h>
2 #include <iostream>
3 #include <graphics.h>
4 #include <stdlib.h>
5 using namespace std;
6 class point
7 {
8 public:
9 int x,y;
10 };
11 class poly
12 {
13 private:
14 point p[20];
15 int inter[20],x,y;
16 int v,xmin,ymin,xmax,ymax;
17 public:
18 int c;
19 void read();
20 void calcs();
21 void display();
22 void ints(float);
23 void sort(int);
24 };
25 void poly::read()
26 {
27 int i;
28 cout<<"\n Scan Fill Algoritm";
29 cout<<"\n Enter number of Vertices of Polygon";
30 cin>>v;
31 if(v>2)
32 {
33 for(i=0;i<v;i++)//Accept the vertics
34 {
35 cout<<"\n Enter co-ordinate no. "<<i+1<<":";
36 cout<<"\n \tx"<<(i+1)<<"=";
37 cin>>p[i].x;
38 cout<<"\n\ty"<<(i+1)<<"=";
39 cin>>p[i].y;
40 }
41 p[i].x=p[0].x;
42 p[i].y=p[0].y;
43 xmin=xmax=p[0].x;
44 ymin=ymax=p[0].y;
45 }
46 else
47 cout<<"\n Enter valid no of vertices ";
48 }
49 void poly::calcs()
50 {
51 for(int i =0;i<v;i++)
52 {
53 if(xmin>p[i].x)
54 xmin=p[i].x;
55 if(xmax<p[i].x)
56 xmax=p[i].x;
57 if(ymin>p[i].y)
58 ymin=p[i].y;
59 if(ymax=p[i].y)
60 ymax=p[i].y;
61 }
62 }
63 void poly::display()
64 {
65 int ch1;
66 char ch='y';
67 float s,s2;
68 do
69 {
70 cout<<"\n\n MEnu:";
71 cout<<"\n\n\t1.Scan line fill";
72 cout<<"\n\nt2.Exit";
73 cout<<"\n\n Enter your choice:";
74 cin>>ch1;
75 switch(ch1)
76 {
77 case 1:
78 s=ymin+0.01;
79 delay(100);
80 cleardevice();
81 while(s<=ymax)
82 {
83 ints(s);
84 sort(s);
85 s++;
86 }
87 break;
88 case 2:
89 exit(0);
90 }
91 cout<<"Do you want to continue?:";
92 cin>>ch;
93 }
94 while(ch=='y' || ch=='Y');
95 }
96 void poly::ints(float z)
97 {
98 int x1,x2,y1,y2,temp;
99 c=0;
100 for(int i=0;i<v;i++)
101 {
102 x1=p[i].x;
103 y1=p[i].y;
104 x2=p[i+1].x;
105 y2=p[i+1].y;
106 if(y2<y1)
107 {
108 temp=x1;
109 x1=x2;
110 x2=temp;
111 temp=y1;
112 y1=y2;
113 y2=temp;
114 }
115 if(z<=y&&z>=y1)
116 {
117 if((y1-y2)==0)
118 x=x1;
119 else
120 {
121 x=((x2-x1)*(z-y1))/(y2-y1);
122 x=x+x1;
123 }
124 if(x<=xmax && x>=xmin)
125 inter[c++]=x;
126 }
127 }
128 }
129 void poly::sort(int z)
130 {
131 int temp,j,i;
132 for(i=0;i<v;i++)
133 {
134 line(p[i].x,p[i].y,p[i+1].x,p[i+1].y);
135 }
136 delay(100);
137 for(i=0;i<c;i+=2)
138 {
139 delay(100);
140 line(inter[i],z,inter[i+1],z);
141 }
142 }
143 int main()
144 {
145 int cl;
146 int gd=DETECT,gm;
147 initgraph(&gd,&gm,NULL);
148 cleardevice();
149 poly x;
150 x.read();
151 x.calcs();
152 cleardevice();
153 cout<<"\n\t Enter the color You want :(in range 0 to 15)->";
154 cin>>cl;
155 setcolor(cl);
156 x.display();
157
158 closegraph();
159 getch();
160 return 0;
161 }
162