-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCG_practical2_concave_polygon.cpp
163 lines (160 loc) · 3.25 KB
/
CG_practical2_concave_polygon.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
156
157
158
159
160
161
162
163
/* 1. Write C++ program to draw a concave polygon and fill it with desired color
using scan fill algorithm. Apply the concept of inheritance. */
#include <conio.h>
#include <iostream>
#include <graphics.h>
#include <stdlib.h>
using namespace std;
class point
{
public:
int x, y;
};
class poly
{
private:
point p[20];
int inter[20], x, y;
int v, xmin, ymin, xmax, ymax;
public:
int c;
void read();
void calcs();
void display();
void ints(float);
void sort(int);
};
void poly::read()
{
int i;
cout << "\n Scan Fill Algorithm ";
cout << "\n Enter Number Of Vertices Of Polygon: ";
cin >> v;
if (v > 2)
{
for (i = 0; i < v; i++) // ACCEPT THE VERTICES
{
cout << "\nEnter co-ordinate no. " << i + 1 << " : ";
cout << "\n\tx" << (i + 1) << "=";
cin >> p[i].x;
cout << "\n\ty" << (i + 1) << "=";
cin >> p[i].y;
}
p[i].x = p[0].x;
p[i].y = p[0].y;
xmin = xmax = p[0].x;
ymin = ymax = p[0].y;
}
else
cout << "\n Enter valid no. of vertices.";
}
void poly::calcs()
{
for (int i = 0; i < v; i++)
{
if (xmin > p[i].x)
xmin = p[i].x;
if (xmax < p[i].x)
xmax = p[i].x;
if (ymin > p[i].y)
ymin = p[i].y;
if (ymax < p[i].y)
ymax = p[i].y;
}
}
void poly::display()
{
int ch1;
char ch = 'y';
float s, s2;
do
{
cout << "\n\nMENU:";
cout << "\n\n\t1 . Scan line Fill ";
cout << "\n\n\t2 . Exit ";
cout << "\n\nEnter your choice:";
cin >> ch1;
switch (ch1)
{
case 1:
s = ymin + 0.01;
delay(100);
cleardevice();
while (s <= ymax)
{
ints(s);
sort(s);
s++;
}
break;
case 2:
exit(0);
}
cout << "Do you want to continue?: ";
cin >> ch;
} while (ch == 'y' || ch == 'Y');
}
void poly::ints(float z)
{
int x1, x2, y1, y2, temp;
c = 0;
for (int i = 0; i < v; i++)
{
x1 = p[i].x;
y1 = p[i].y;
x2 = p[i + 1].x;
y2 = p[i + 1].y;
if (y2 < y1)
{
temp = x1;
x1 = x2;
x2 = temp;
temp = y1;
y1 = y2;
y2 = temp;
}
if (z <= y2 && z >= y1)
{
if ((y1 - y2) == 0)
x = x1;
else
{
x = ((x2 - x1) * (z - y1)) / (y2 - y1);
x = x + x1;
}
if (x <= xmax && x >= xmin)
inter[c++] = x;
}
}
}
void poly::sort(int z) // sorting
{
int temp, j, i;
for (i = 0; i < v; i++)
{
line(p[i].x, p[i].y, p[i + 1].x, p[i + 1].y);
}
delay(100);
for (i = 0; i < c; i += 2)
{
delay(100);
line(inter[i], z, inter[i + 1], z);
}
}
int main() // main
{
int cl;
initwindow(500, 600);
cleardevice();
poly x;
x.read();
x.calcs();
cleardevice();
cout << "\n\tEnter The Color You Want :(In Range 0 To 15 )->"; // selecting color
cin >> cl;
setcolor(cl);
x.display();
closegraph(); // closing graph
getch();
return 0;
}