-
Notifications
You must be signed in to change notification settings - Fork 0
/
7. 2D Transformations
443 lines (441 loc) · 10.7 KB
/
7. 2D Transformations
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/*************************************************************************
TITLE: Program to implement 2D Transformations.
*************************************************************************/
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void disp(int n,float c[][3])
{
float maxx,maxy;
int i;
maxx=getmaxx();
maxy=getmaxy();
maxx=maxx/2;
maxy=maxy/2;
i=0;
while(i<n-1)
{
line(maxx+c[i][0],maxy-c[i][1],maxx+c[i+1][0],maxy-c[i+1][1]);
i++;
}
i=n-1;
line(maxx+c[i][0],maxy-c[i][1],maxx+c[0][0],maxy-c[0][1]);
setcolor(WHITE);
line(0,maxy,maxx*2,maxy);
line(maxx,0,maxx,maxy*2);
setcolor(20);
}
void mul(int n,float b[][3],float c[][3],float a[][3])
{
int i,j,k;
for(i=0;i<n;i++)
for(j=0;j<3;j++)
a[i][j]=0;
for(i=0;i<n;i++)
for(j=0;j<3;j++)
for(k=0;k<3;k++)
{
a[i][j]=a[i][j]+(c[i][k]*b[k][j]);
}
}
void translation(int n,float c[][3],float tx,float ty)
{
int i;
for(i=0;i<n;i++)
{
c[i][0]=c[i][0]+tx;
c[i][1]=c[i][1]+ty;
}
}
void scaling(int n,float c[][3],float sx,float sy)
{
float b[5][3],a[5][3];
int i,j;
for(j=0;j<3;j++)
b[i][j]=0;
b[0][0]=sx;
b[1][1]=sy;
b[2][2]=1;
mul(n,b,c,a);
setcolor(10);
disp(n,a);
}
void rotation(int n,float c[][3],float ra)
{
int i=0,j;
float b[5][3],xp,yp,a[5][3];
xp=c[0][0];
yp=c[0][1];
for(i=0;i<3;i++)
for(j=0;j<3;j++)
b[i][j]=0;
b[0][0]=b[1][1]=cos(ra*3.14/180);
b[0][1]=sin(ra*3.14/180);
b[1][0]=-sin(ra*3.14/180);;
b[2][0]=(-xp*cos(ra*3.14/180))+(yp*sin(ra*3.14/180))+xp;
b[2][1]=(-xp*sin(ra*3.14/180))-(yp*cos(ra*3.14/180))+yp;
b[2][2]=1;
mul(n,b,c,a);
setcolor(10);
disp(n,a);
}
void rotate_fixedpoint(int n,float c[][3],float fx,float fy,float fpr)
{
//Rotate wrt fixed point
int i=0,j;
float b[5][3],xp,yp,a[5][3];
for(i=0;i<3;i++)
for(j=0;j<3;j++)
b[i][j]=0;
b[0][0]=b[1][1]=cos(fpr*3.14/180);
b[0][1]=sin(fpr*3.14/180);
b[1][0]=-sin(fpr*3.14/180);
b[2][2]=1;
b[2][0]=-(fx*cos(fpr*3.14/180))+(fy*sin(fpr*3.14/180))+fx;
b[2][1]=-(fx*cos(fpr*3.14/180))-(fy*sin(fpr*3.14/180))+fy;
mul(n,b,c,a);
setcolor(10);
disp(n,a);
}
void scale_fixedpoint(int n,float c[][3],float fx,float fy,float sx,float sy)
{
//Scale wrt a fixed point
int i=0,j;
float b[5][3],xp,yp,a[5][3];
for(i=0;i<3;i++)
for(j=0;j<3;j++)
b[i][j]=0;
b[0][0]=sx;
b[1][1]=sy;
b[2][2]=1;
b[2][0]=-(fx*sx)+fx;
b[2][1]=-(fy*sy)+fy;
mul(n,b,c,a);
setcolor(10);
disp(n,a);
}
void refthx(int n,float c[][3])
{
//Reflect along x axis
int i=0,j;
float b[5][3],a[5][3];
for(i=0;i<3;i++)
for(j=0;j<3;j++)
b[i][j]=0;
b[0][0]=b[2][2]=1;
b[1][1]=-1;
mul(n,b,c,a);
setcolor(10);
disp(n,a);
}
void refthy(int n,float c[][3])
{
//Reflect along y axis
int i=0,j;
float b[5][3],a[5][3];
for(i=0;i<3;i++)
for(j=0;j<3;j++)
b[i][j]=0;
b[1][1]=b[2][2]=1;
b[0][0]=-1;
mul(n,b,c,a);
setcolor(10);
disp(n,a);
}
void reforg(int n,float c[][3])
{
//Reflect along origin
int i=0,j;
float b[5][3],a[5][3];
for(i=0;i<3;i++)
for(j=0;j<3;j++)
b[i][j]=0;
b[0][0]=b[1][1]=-1;
b[2][2]=1;
mul(n,b,c,a);
setcolor(10);
disp(n,a);
}
void refthyx(int n,float c[][3])
{
//Reflect along line y=x
int i=0,j;
float b[5][3],a[5][3];
for(i=0;i<3;i++)
for(j=0;j<3;j++)
b[i][j]=0;
b[0][1]=b[1][0]=b[2][2]=1;
mul(n,b,c,a);
setcolor(10);
disp(n,a);
}
void refthynegx(int n,float c[][3])
{
//Reflect along line y=-x
int i=0,j;
float b[5][3],a[5][3];
for(i=0;i<3;i++)
for(j=0;j<3;j++)
b[i][j]=0;
b[0][1]=b[1][0]=-1;
b[2][2]=1;
mul(n,b,c,a);
setcolor(10);
disp(n,a);
}
void shearx(int n,float c[][3],float shx)
{
//Shearing along X
int i=0,j;
float b[5][3],a[5][3];
for(i=0;i<3;i++)
for(j=0;j<3;j++)
b[i][j]=0;
b[0][0]=b[1][1]=b[2][2]=1;
b[1][0]=shx;
mul(n,b,c,a);
setcolor(10);
disp(n,a);
}
void sheary(int n,float c[][3],float shy)
{
//Shearing along Y
int i=0,j;
float b[5][3],a[5][3];
for(i=0;i<3;i++)
for(j=0;j<3;j++)
b[i][j]=0;
b[0][0]=b[1][1]=b[2][2]=1;
b[0][1]=shy;
mul(n,b,c,a);
setcolor(10);
disp(n,a);
}
void shearxy(int n,float c[][3],float shx,float shy)
{
//Shearing along XY
int i=0,j;
float b[5][3],a[5][3];
for(i=0;i<3;i++)
for(j=0;j<3;j++)
b[i][j]=0;
b[0][0]=b[1][1]=b[2][2]=1;
b[0][1]=shy;
b[1][0]=shx;
mul(n,b,c,a);
setcolor(10);
disp(n,a);
}
int main()
{
int ccc,gd=DETECT,gm;
char continuechar='Y';
float c[5][3];
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
do
{
int i,j,k,cho,n;
float tx,ty,sx,sy,ra;
cout<<"\nEnter no. of vertices: ";
cin>>n;
for (i=0;i<n;i++)
{
cout<<"Enter coordinates: ";
cin>>c[i][0]>>c[i][1];
c[i][2]=1;
}
//Select Transformation
cout<<"\n ---MENU---";
cout<<"\n 1)Translation";
cout<<"\n 2)Scaling";
cout<<"\n 3)Rotation";
cout<<"\n 4)Reflection";
cout<<"\n 5)Shear";
cout<<"\n 6)Fixed Point Rotation & Scaling";
cout<<"\nEnter your Choice:";
cin>>cho;
switch(cho)
{
//Translation
case 1:
cout<<"\nEnter translation factor for X and Y axis: ";
cin>>tx>>ty;
cleardevice();
setcolor(15);
disp(n,c);
translation(n,c,tx,ty);
setcolor(10);
disp(n,c);
getch();
break;
//Scaling
case 2:
cout<<"\nEnter scaling factor for X and Y axis: ";
cin>>sx>>sy;
cleardevice();
setcolor(15);
disp(n,c);
scaling(n,c,sx,sy);
getch();
break;
//Rotation
case 3:
cout<<"\nEnter rotation factor: ";
cin>>ra;
cleardevice();
setcolor(15);
disp(n,c);
rotation(n,c,ra);
getch();
break;
//Reflecton
case 4:
int ch;
cout<<"\n ---MENU---";
cout<<"\n 1)Reflection about x axis";
cout<<"\n 2)Reflection about y axis";
cout<<"\n 3)Reflection about origin";
cout<<"\n 4)Reflection about the line y=x";
cout<<"\n 5)Reflection about the line y=-x";
cout<<"\nEnter your Choice: ";
cin>>ch;
//Reflect the diagram
switch(ch)
{
case 1:
//Reflect about x axisc
cleardevice();
setcolor(15);
disp(n,c);
refthx(n,c);
getch();
break;
case 2:
//Reflect about y axis
cleardevice();
setcolor(15);
disp(n,c);
refthy(n,c);
getch();
break;
case 3:
//Reflect about origin
cleardevice();
setcolor(15);
disp(n,c);
reforg(n,c);
getch();
break;
case 4:
//Reflect about line y=x
cleardevice();
setcolor(15);
disp(n,c);
refthyx(n,c);
getch();
break;
case 5:
//Reflect about line y=-x
cleardevice();
setcolor(15);
disp(n,c);
refthynegx(n,c);
getch();
break;
}
break;
//Shearing
case 5:
int cha;
float shx,shy;
//Shearing transformation
cout<<"\n ---MENU---";
cout<<"\n 1)X-shear";
cout<<"\n 2)Y-shear";
cout<<"\n 3)XY-shear";
cout<<"\nEnter your Choice: ";
cin>>cha;
switch(cha)
{
case 1:
//Enter shearing factor ShX
cout<<"\nEnter Shear factor: ";
cin>>shx;
cleardevice();
setcolor(15);
disp(n,c);
shearx(n,c,shx);
getch();
break;
case 2:
//Enter shearing factor Shy
cout<<"\nEnter Shear factor: ";
cin>>shy;
cleardevice();
setcolor(15);
disp(n,c);
shearx(n,c,shy);
getch();
break;
case 3:
//Enter shearing factors Shx and Shy
cout<<"\nEnter Shear factor Shx and Shy: ";
cin>>shx>>shy;
cleardevice();
setcolor(15);
disp(n,c);
shearxy(n,c,shx,shy);
getch();
break;
default:
cout<<"\nInvalid Choice !!!";
break;
}
break;
case 6:
//Fixed point transformation
float fx,fy;
float fpr,fpsx,fpsy;
int ch1;
cout<<"\nEnter the coordinates of fixed point:";
cin>>fx>>fy;
cout<<"1.Fixed Point Rotation\n2.Fixed Point Scaling\nChoose transformation: ";
cin>>ch1;
switch(ch1)
{
case 1:
cout<<"\nEnter rotation factor: ";
cin>>fpr;
cleardevice();
setcolor(15);
disp(n,c);
rotate_fixedpoint(n,c,fx,fy,fpr);
getch();
break;
case 2:
cout<<"\nEnter scaling factor along x and y axis: ";
cin>>sx>>sy;
cleardevice();
setcolor(15);
disp(n,c);
scale_fixedpoint(n,c,fx,fy,sx,sy);
getch();
break;
default:
cout<<"\nInvalid choice!!";
break;
}
break;
default:
cout<<"\nInvalid Choice !!!";
break;
}
//Continue or not
cout<<"\n\n\n\n\n\n\n\n\n\nDo you want to continue(Y/N):";
cin>>continuechar;
}while(continuechar=='y'||continuechar=='Y');
getch();
closegraph();
return 0;
}