Sem 4‎ > ‎CG LAB‎ > ‎

Z: Transformations

posted Apr 7, 2012, 1:27 AM by Neil Mathew   [ updated Apr 7, 2012, 3:23 AM ]

All the programs follow a basic layout, except for the transformation.
And the only change in the transformation is the matrix and it's input variables.

One thing that bothered me is the order since ma'am taught us formulas using Column Major.

However, for shapes with sides greater than the number 3, one tends to get confused with the order.
However, to avoid getting confused, I've used Row Major and changed the order 
but retained the formulas as given in the notebook





ALL ROUND PROGRAM:

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
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
 
#include<math.h> //for rotate
 
/* Every Such Program are divided into these parts (Functions):
 
1. Common Variables = P P2 n T , Tx or Sx or Angle = GLOBAL
 
2. Input the Required Values = INPUT()
 
[ Changing from Translation Scaling, etc EDIT only 1, 2 ]
 
3. Transform = change()
 
4. Draw the Lines Before and After Transformation = Draw()
 
5. Matrix Show  = ShowM() ShowT()
               (IMPORTANT B/C you find your mistakes here.)
                MATRICES. MAKE SURE ALL YOUR CELLS ARE FILLED!!!!
 
 
------Extras:
 
>>ROTATION requires matrices & angle TO BE DOUBLE (floating type)
                others can be int. (exc Reflect which depends on Rotate.
 
>>ROTATION requires conversion of inputted DEGREE to RADIAN
                180 deg = pi rad
                1 deg = pi / 180 * 1 rad
                x deg = pi / 180 * x rad
 
>>ROTATION requires Math.h headerfile
 
>> If Ma'am asks you to do multiple transformations.
        Add the extra bits of INPUT into Change()
        and duplicate Change to its types
*/
 
double P[3][10], P2[3][10], n, T[3][3];
 
int Tx,  Ty; //For Translation
int Sx,  Sy; //For Scaling
double angle;   //For Rotation
double m,b;
int Fx, Fy; //Fixed point
 
 
void INPUT()
{
cout<<"\n Enter no of sides of Shape: ";
cin>>n;
               //i for each point
cout<<"\n Enter the coordinates of Triangle";
for(int i=0; i<n; i++)
{
cout<<"\n"<<i+1<<" (x,y) = ";
cin>>P[0][i];
cin>>P[1][i];
 
P[2][i]=1; //LAST ROW
}
 
/*TRANSLATION:
cout<<"\n To Translate:";
cout<<"\n Enter Tx Ty ";
cin>>Tx>>Ty;
 
T[0][0]=1; T[0][1]=0; T[0][2]=Tx;
T[1][0]=0; T[1][1]=1; T[1][2]=Ty;
T[2][0]=0; T[2][1]=0; T[2][2]=1; */
 
 
 
 
/*SCALING
cout<<"\n To Scale:";
cout<<"\n Enter Sx Sy ";
cin>>Sx>>Sy;
 
T[0][0]=Sx; T[0][1]=0; T[0][2]=0;
T[1][0]=0; T[1][1]=Sy; T[1][2]=0;
T[2][0]=0; T[2][1]=0; T[2][2]=1; */
 
 
 
 
 
 
/*ROTATION
cout<<"\n To Rotate:";
cout<<"\n Enter angle: ";
cin>>angle;
 
//<<<<<<<<<<<<<<CONVERT DEGREE TO RADIAN + call MATH.h
angle = ( ( (22.0/7.0)/180.0 ) * angle );
 
cout<<( cos( angle ) );
 
T[0][0]=cos (angle);    T[0][1]= - sin (angle); T[0][2]=0;
T[1][0]=sin (angle);    T[1][1]= cos (angle);   T[1][2]=0;
T[2][0]=0;              T[2][1]= 0;             T[2][2]=1;
*/
 
 
 
//REFLECTION:
cout<<"\n To Reflect:";
cout<<"\n Enter wrt eq (Y= m X + b) value of m & b: ";
cin>>m>>b;
 
cout<<( cos( angle ) );
 
T[0][0]=(1-m*m)/(1+m*m); T[0][1]= 2*m/ (1+m*m);         T[0][2]= - (2*m*b)/(1+m*m);
T[1][0]=2*m/(1+m*m);     T[1][1]= (m*m-1)/(1+m*m);      T[1][2]= (2*b)/(1+m*m);
T[2][0]=0;               T[2][1]= 0;                    T[2][2]= 1;
 
 
/*^^^^^^^^^^^^^^^^^^^^^^^^^ WRT POINT
 
cout<<"\n Enter the fixed points: (x,y)";
cin>>Fx>>Fy; */
 
 
 
 
/*SCALING wrt POINT
cout<<"\n To Scale:";
cout<<"\n Enter Sx Sy ";
cin>>Sx>>Sy;
 
T[0][0]=Sx; T[0][1]=0; T[0][2]=-Fx*Sx + Fx;
T[1][0]=0; T[1][1]=Sy; T[1][2]=-Fy*Sy + Fy;
T[2][0]=0; T[2][1]=0; T[2][2]=1; */
 
 
 
 
 
 
 
/*ROTATION wrt POINT
cout<<"\n To Rotate:";
cout<<"\n Enter angle: ";
cin>>angle;
 
//<<<<<<<<<<<<<<CONVERT DEGREE TO RADIAN + call MATH.h
angle = ( ( (22.0/7.0)/180.0 ) * angle );
 
cout<<( cos( angle ) );
 
T[0][0]=cos (angle);    T[0][1]= - sin (angle); T[0][2]=-(Fx*T[0][0]+Fy*T[0][1])+Fx;
T[1][0]=sin (angle);    T[1][1]= cos (angle);   T[1][2]=-(Fx*T[1][0]+Fy*T[1][1])+Fy;
T[2][0]=0;              T[2][1]= 0;             T[2][2]=1;
*/
 
}
 
void Change()
{
            /*here: i for Rows (x,y,1)
                    j for Columns (each point)
                    k for multiplication of matrices (common) */
 
for(int i=0; i<3; i++)
{
for(int j=0; j<n; j++)
{
P2[i][j]=0;
 
for(int k=0; k<3; k++)
{
P2[i][j] += T[i][k] * P[k][j];
}
 
}
}
 
}
 
void ShowM(double A[3][10])
{
 
for(int i=0; i<3; i++)
{
for(int j=0; j<n; j++)
{
cout<<" "<<A[i][j];
}
cout<<"\n";
}
}
 
void ShowT()
{
 
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
cout<<" "<<T[i][j];
}
cout<<"\n";
}
}
 
void Draw( double A[3][10] )
{
 
//MID POINT
int X= getmaxx()*0.5;
int Y= getmaxy()*0.5;
 
        //i for each point
for(int i=0; i< ( n - 1 ); i++)
{
line(X + A[0][i],Y + A[1][i], X + A[0][i+1], Y+ A[1][i+1]);
}
line(X + A[0][i], Y + A[1][i], X + A[0][0],Y + A[1][0]);
 
 
//below so axis is not green, or blue
setcolor(DARKGRAY);
//DRAW AXIS:
line(0,                 getmaxy()*0.5,
     getmaxx(),         getmaxy()*0.5);
 
line(getmaxx()*0.5,     0,
     getmaxx()*0.5,     getmaxy());
}
 
 
 
void main()
{
int gdriver=DETECT,gmode;
 
//INPUT & INITIALIZATION:::::::::::::::::::::::::::::::::::
INPUT();
 
//THE TRANSFORM::::::::::::::::::::::::::::::::::::::::::::::
Change();
 
//SHOW MATRICES::::::::::::::::::::::::::::::::::::::::::::::
 
cout<<"\n Show Matrices:";
 
cout<<"\n\nORIGINAL:\n";
ShowM(P);
 
cout<<"\nTRANSFORMATION MAT:\n";
ShowT();
 
cout<<"\n\nTRANSFORMED:\n";
ShowM(P2);
 
cout<<"\n Draw? ";
getch();
 
//DRAW::::::::::::::::::::::::::::::::::::::::::::::::::::::::
initgraph(&gdriver, &gmode, "C:/TC/BGI");
cleardevice();
 
//Before
setcolor(GREEN);
Draw(P);
 
//After
setcolor(CYAN);
Draw(P2);
 
getch();
closegraph();
}
 
 
 
 

Comments