-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2D_Transformations.c
More file actions
365 lines (343 loc) · 13.6 KB
/
Copy path2D_Transformations.c
File metadata and controls
365 lines (343 loc) · 13.6 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>
void grid()
{
line(0, 240, 640, 240);
line(320, 0, 320, 480);
}
void draw(int c00, int c01, int c10, int c11, int c20, int c21)
{
line(320+c00, 240-c01, 320+c10, 240-c11);
line(320+c00, 240-c01, 320+c20, 240-c21);
line(320+c10, 240-c11, 320+c20, 240-c21);
}
void matrixMulti(double a[10][10], double b[10][10], double c[10][10], int m, int n, int p)
{
int i, j, k;
for (i = 0; i < m; i++)
{
for (j = 0; j < p; j++)
{
c[i][j] = 0;
for (k = 0; k < n; k++)
{
c[i][j] += a[i][k] * b[k][j];
}
}
}
}
void addElements(double a[10][10], double num1, double num2, double num3,
double num4, double num5, double num6,
double num7, double num8, double num9)
{
a[0][0] = num1;
a[0][1] = num2;
a[0][2] = num3;
a[1][0] = num4;
a[1][1] = num5;
a[1][2] = num6;
a[2][0] = num7;
a[2][1] = num8;
a[2][2] = num9;
}
void main()
{
int gd = DETECT, gm;
int x1, y1, x2, y2, x3, y3, tx, ty, theta, ch,
rot1, rot2, sx, sy, sc1, sc2, slope, c, r1, r2, sh;
double thetaRad = 0, thetaRef = 0;
double mat1[10][10], mat2[10][10], result1[10][10], result2[10][10];
clrscr();
printf("Enter the coordinates of triangle\n");
printf("Point 1: ");
scanf("%d%d", &x1, &y1);
printf("Point 2: ");
scanf("%d%d", &x2, &y2);
printf("Point 3: ");
scanf("%d%d", &x3, &y3);
do {
printf("******************MENU******************\n");
printf("1. Translation\n");
printf("2. Rotation about origin\n");
printf("3. Rotation about a fixed point\n");
printf("4. Scaling about origin\n");
printf("5. Scaling about a fixed point\n");
printf("6. Reflection about x-axis\n");
printf("7. Reflection about y-axis\n");
printf("8. Reflection about origin\n");
printf("9. Reflection about any line y = mx + c\n");
printf("10. Shearing in x-direction\n");
printf("11. Shearing in y-direction\n");
printf("12. Exit\n");
printf("\nEnter your choice: ");
scanf("%d", &ch);
if (ch == 1)
{
printf("Enter the translation coordinates: ");
scanf("%d%d", &tx, &ty);
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
grid();
draw(x1, y1, x2, y2, x3, y3);
addElements(mat1, x1, x2, x3, y1, y2, y3, 1, 1, 1);
addElements(mat2, 1, 0, tx, 0, 1, ty, 0, 0, 1);
matrixMulti(mat2, mat1, result1, 3, 3, 3);
delay(2000);
setcolor(14);
draw((int)result1[0][0], (int)result1[1][0],
(int)result1[0][1], (int)result1[1][1],
(int)result1[0][2], (int)result1[1][2]);
getch();
closegraph();
}
else if (ch == 2)
{
printf("Enter the angle (in degrees) by which you want to rotate the triangle: ");
scanf("%d", &theta);
thetaRad = (double)theta * 3.14 / 180.0;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
grid();
draw(x1, y1, x2, y2, x3, y3);
addElements(mat1, cos(thetaRad), -sin(thetaRad), 0,
sin(thetaRad), cos(thetaRad), 0,
0, 0, 1);
addElements(mat2, x1, x2, x3, y1, y2, y3, 1, 1, 1);
matrixMulti(mat1, mat2, result1, 3, 3, 3);
delay(2000);
setcolor(14);
draw((int)result1[0][0], (int)result1[1][0],
(int)result1[0][1], (int)result1[1][1],
(int)result1[0][2], (int)result1[1][2]);
getch();
closegraph();
}
else if (ch == 3)
{
printf("Enter the coordinates of the point about which you want to rotate the triangle: ");
scanf("%d%d", &rot1, &rot2);
printf("Enter the angle (in degrees) by which you want to rotate the triangle: ");
scanf("%d", &theta);
thetaRad = (double)theta * 3.14 / 180.0;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
grid();
draw(x1, y1, x2, y2, x3, y3);
// Translate to origin
addElements(mat1, x1, x2, x3, y1, y2, y3, 1, 1, 1);
addElements(mat2, 1, 0, -rot1, 0, 1, -rot2, 0, 0, 1);
matrixMulti(mat2, mat1, result1, 3, 3, 3);
// Rotate
addElements(mat1, cos(thetaRad), -sin(thetaRad), 0,
sin(thetaRad), cos(thetaRad), 0,
0, 0, 1);
addElements(mat2, result1[0][0], result1[0][1], result1[0][2],
result1[1][0], result1[1][1], result1[1][2],
1, 1, 1);
matrixMulti(mat1, mat2, result2, 3, 3, 3);
// Translate back
addElements(mat1, 1, 0, rot1, 0, 1, rot2, 0, 0, 1);
addElements(mat2, result2[0][0], result2[0][1], result2[0][2],
result2[1][0], result2[1][1], result2[1][2],
1, 1, 1);
matrixMulti(mat1, mat2, result1, 3, 3, 3);
delay(2000);
setcolor(14);
draw((int)result1[0][0], (int)result1[1][0],
(int)result1[0][1], (int)result1[1][1],
(int)result1[0][2], (int)result1[1][2]);
getch();
closegraph();
}
else if (ch == 4)
{
printf("Enter the scaling factors: ");
scanf("%d%d", &sx, &sy);
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
grid();
draw(x1, y1, x2, y2, x3, y3);
addElements(mat1, sx, 0, 0, 0, sy, 0, 0, 0, 1);
addElements(mat2, x1, x2, x3, y1, y2, y3, 1, 1, 1);
matrixMulti(mat1, mat2, result1, 3, 3, 3);
delay(2000);
setcolor(14);
draw((int)result1[0][0], (int)result1[1][0],
(int)result1[0][1], (int)result1[1][1],
(int)result1[0][2], (int)result1[1][2]);
getch();
closegraph();
}
else if (ch == 5)
{
printf("Enter the coordinates of the point about which you want to scale the triangle: ");
scanf("%d%d", &sc1, &sc2);
printf("Enter the scaling factors: ");
scanf("%d%d", &sx, &sy);
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
grid();
draw(x1, y1, x2, y2, x3, y3);
// Translate to origin
addElements(mat1, x1, x2, x3, y1, y2, y3, 1, 1, 1);
addElements(mat2, 1, 0, -sc1, 0, 1, -sc2, 0, 0, 1);
matrixMulti(mat2, mat1, result1, 3, 3, 3);
// Scale
addElements(mat1, sx, 0, 0, 0, sy, 0, 0, 0, 1);
addElements(mat2, result1[0][0], result1[0][1], result1[0][2],
result1[1][0], result1[1][1], result1[1][2],
1, 1, 1);
matrixMulti(mat1, mat2, result2, 3, 3, 3);
// Translate back
addElements(mat1, 1, 0, sc1, 0, 1, sc2, 0, 0, 1);
addElements(mat2, result2[0][0], result2[0][1], result2[0][2],
result2[1][0], result2[1][1], result2[1][2],
1, 1, 1);
matrixMulti(mat1, mat2, result1, 3, 3, 3);
delay(2000);
setcolor(14);
draw((int)result1[0][0], (int)result1[1][0],
(int)result1[0][1], (int)result1[1][1],
(int)result1[0][2], (int)result1[1][2]);
getch();
closegraph();
}
else if (ch == 6)
{
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
grid();
draw(x1, y1, x2, y2, x3, y3);
addElements(mat1, 1, 0, 0, 0, -1, 0, 0, 0, 1);
addElements(mat2, x1, x2, x3, y1, y2, y3, 1, 1, 1);
matrixMulti(mat1, mat2, result1, 3, 3, 3);
delay(2000);
setcolor(14);
draw((int)result1[0][0], (int)result1[1][0],
(int)result1[0][1], (int)result1[1][1],
(int)result1[0][2], (int)result1[1][2]);
getch();
closegraph();
}
else if (ch == 7)
{
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
grid();
draw(x1, y1, x2, y2, x3, y3);
addElements(mat1, -1, 0, 0, 0, 1, 0, 0, 0, 1);
addElements(mat2, x1, x2, x3, y1, y2, y3, 1, 1, 1);
matrixMulti(mat1, mat2, result1, 3, 3, 3);
delay(2000);
setcolor(14);
draw((int)result1[0][0], (int)result1[1][0],
(int)result1[0][1], (int)result1[1][1],
(int)result1[0][2], (int)result1[1][2]);
getch();
closegraph();
}
else if (ch == 8)
{
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
grid();
draw(x1, y1, x2, y2, x3, y3);
addElements(mat1, -1, 0, 0, 0, -1, 0, 0, 0, 1);
addElements(mat2, x1, x2, x3, y1, y2, y3, 1, 1, 1);
matrixMulti(mat1, mat2, result1, 3, 3, 3);
delay(2000);
setcolor(14);
draw((int)result1[0][0], (int)result1[1][0],
(int)result1[0][1], (int)result1[1][1],
(int)result1[0][2], (int)result1[1][2]);
getch();
closegraph();
}
else if (ch == 9)
{
printf("Enter the values of m and c respectively: ");
scanf("%d%d", &slope, &c);
thetaRef = atan((double)slope) * 180.0 / 3.14;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
grid();
draw(x1, y1, x2, y2, x3, y3);
// Translate to y-intercept
addElements(mat1, 1, 0, 0, 0, 1, -c, 0, 0, 1);
addElements(mat2, x1, x2, x3, y1, y2, y3, 1, 1, 1);
matrixMulti(mat1, mat2, result1, 3, 3, 3);
// Rotate to align with x-axis
addElements(mat1, cos(thetaRef), sin(thetaRef), 0,
-sin(thetaRef), cos(thetaRef), 0,
0, 0, 1);
addElements(mat2, result1[0][0], result1[0][1], result1[0][2],
result1[1][0], result1[1][1], result1[1][2],
1, 1, 1);
matrixMulti(mat1, mat2, result2, 3, 3, 3);
// Reflect about x-axis
addElements(mat1, 1, 0, 0, 0, -1, 0, 0, 0, 1);
addElements(mat2, result2[0][0], result2[0][1], result2[0][2],
result2[1][0], result2[1][1], result2[1][2],
1, 1, 1);
matrixMulti(mat1, mat2, result1, 3, 3, 3);
// Rotate back
addElements(mat1, cos(thetaRef), -sin(thetaRef), 0,
sin(thetaRef), cos(thetaRef), 0,
0, 0, 1);
addElements(mat2, result1[0][0], result1[0][1], result1[0][2],
result1[1][0], result1[1][1], result1[1][2],
1, 1, 1);
matrixMulti(mat1, mat2, result2, 3, 3, 3);
// Translate back
addElements(mat1, 1, 0, 0, 0, 1, c, 0, 0, 1);
addElements(mat2, result2[0][0], result2[0][1], result2[0][2],
result2[1][0], result2[1][1], result2[1][2],
1, 1, 1);
matrixMulti(mat1, mat2, result1, 3, 3, 3);
delay(2000);
setcolor(14);
draw((int)result1[0][0], (int)result1[1][0],
(int)result1[0][1], (int)result1[1][1],
(int)result1[0][2], (int)result1[1][2]);
getch();
closegraph();
}
else if (ch == 10)
{
printf("Enter the shearing parameter: ");
scanf("%d", &sh);
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
grid();
draw(x1, y1, x2, y2, x3, y3);
addElements(mat1, 1, sh, 0, 0, 1, 0, 0, 0, 1);
addElements(mat2, x1, x2, x3, y1, y2, y3, 1, 1, 1);
matrixMulti(mat1, mat2, result1, 3, 3, 3);
delay(2000);
setcolor(14);
draw((int)result1[0][0], (int)result1[1][0],
(int)result1[0][1], (int)result1[1][1],
(int)result1[0][2], (int)result1[1][2]);
getch();
closegraph();
}
else if (ch == 11)
{
printf("Enter the shearing parameter: ");
scanf("%d", &sh);
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
grid();
draw(x1, y1, x2, y2, x3, y3);
addElements(mat1, 1, 0, 0, sh, 1, 0, 0, 0, 1);
addElements(mat2, x1, x2, x3, y1, y2, y3, 1, 1, 1);
matrixMulti(mat1, mat2, result1, 3, 3, 3);
delay(2000);
setcolor(14);
draw((int)result1[0][0], (int)result1[1][0],
(int)result1[0][1], (int)result1[1][1],
(int)result1[0][2], (int)result1[1][2]);
getch();
closegraph();
}
else if (ch == 12)
{
printf("Exiting the program\n");
}
else
{
printf("Invalid input \nTry again\n");
}
} while (ch != 12);
}