-
Notifications
You must be signed in to change notification settings - Fork 0
/
dda.cpp
181 lines (177 loc) · 3.75 KB
/
dda.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <iostream>
#include <graphics.h>
#include <stdlib.h>
using namespace std;
class dcircle
{
private:
int x0, y0;
public:
dcircle()
{
x0 = 0;
y0 = 0;
}
void setoff(int xx, int yy)
{
x0 = xx;
y0 = yy;
}
void drawc(int x1, int y1, int r)
{
float d;
int x, y;
x = 0;
y = r;
d = 3 - 2 * r;
do
{
putpixel(x1 + x0 + x, y0 + y - y1, 15);
putpixel(x1 + x0 + y, y0 + x - y1, 15);
putpixel(x1 + x0 + y, y0 - x - y1, 15);
putpixel(x1 + x0 + x, y0 - y - y1, 15);
putpixel(x1 + x0 - x, y0 - y - y1, 15);
putpixel(x1 + x0 - y, y0 - x - y1, 15);
putpixel(x1 + x0 - y, y0 + x - y1, 15);
putpixel(x1 + x0 - x, y0 + y - y1, 15);
if (d <= 0)
{
d = d + 4 * x + 6;
}
else
{
d = d + 4 * (x - y) + 10;
y = y - 1;
}
x = x + 1;
} while (x < y);
}
};
class pt
{
protected:
int xco, yco, color;
public:
pt()
{
xco = 0, yco = 0, color = 15;
}
void setco(int x, int y)
{
xco = x;
yco = y;
}
void setcolor(int c)
{
color = c;
}
void draw()
{
putpixel(xco, yco, color);
}
};
class dline : public pt
{
private:
int x2, y2;
public:
dline() : pt()
{
x2 = 0;
y2 = 0;
}
void setline(int x, int y, int xx, int yy)
{
pt::setco(x, y);
x2 = xx;
y2 = yy;
}
void drawl(int colour)
{
float x, y, dx, dy, length;
int i;
pt::setcolor(colour);
dx = abs(x2 - xco);
dy = abs(y2 - yco);
if (dx >= dy)
{
length = dx;
}
else
{
length = dy;
}
dx = (x2 - xco) / length;
dy = (y2 - yco) / length;
x = xco + 0.5;
y = yco + 0.5;
i = 1;
while (i <= length)
{
pt::setco(x, y);
pt::draw();
x = x + dx;
y = y + dy;
i = i + 1;
}
pt::setco(x, y);
pt::draw();
}
};
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, NULL);
int x, y, r, x1, x2, y1, y2, xmax, ymax, xmid, ymid, n, i;
dcircle c;
cout << "\nenter coordinates of centre of circle : ";
cout << "\n enter the value of x : ";
cin >> x;
cout << "\nenter the value of y : ";
cin >> y;
cout << "\nenter the value of radius : ";
cin >> r;
xmax = getmaxx();
ymax = getmaxy();
xmid = xmax / 2;
ymid = ymax / 2;
setcolor(1);
c.setoff(xmid, ymid);
line(xmid, 0, xmid, ymax);
line(0, ymid, xmax, ymid);
setcolor(15);
c.drawc(x, y, r);
pt p1;
p1.setco(100, 100);
p1.setcolor(14);
dline l;
l.setline(x1 + xmid, ymid - y1, x2 + xmid, ymid - y2);
cout << "Enter Total Number of lines : ";
cin >> n;
for (i = 0; i < n; i++)
{
cout << "Enter co-ordinates of point x1 : ";
cin >> x1;
cout << "enter coordinates of point y1 : ";
cin >> y1;
cout << "Enter co-ordinates of point x2 : ";
cin >> x2;
cout << "enter coordinates of point y2 : ";
cin >> y2;
l.setline(x1 + xmid, ymid - y1, x2 + xmid, ymid - y2);
l.drawl(15);
}
cout << "\nEnter coordinates of centre of circle : ";
cout << "\n Enter the value of x : ";
cin >> x;
cout << "\nEnter the value of y : ";
cin >> y;
cout << "\nEnter the value of radius : ";
cin >> r;
setcolor(5);
c.drawc(x, y, r);
getch();
delay(200);
closegraph();
return 0;
}