-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransformation.cpp
242 lines (220 loc) · 6.92 KB
/
transformation.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
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
#include<iostream>
#include<graphics.h>
#include<conio.h>
#include<math.h>
using namespace std;
int mat[50][50];
float result[3][3];
float FinalResult[3][1];
int multiply3x3matrix(float A[3][3], float B[3][3])
{
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
result[i][j] = 0;
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
for(int k=0; k<3; k++)
result[i][j] += A[i][k]*B[k][j];
}
int multiplyFinalMatrix(float C[3][3], float D[3][1])
{
for(int i=0; i<3; i++)
for(int j=0; j<1; j++)
{
FinalResult[i][j] = 0;
for(int k=0; k<3; k++)
FinalResult[i][j] += C[i][k]*D[k][j];
}
}
void translate()
{
float tx, ty, x, y;
float temp[3][3];
cout<<"Enter translation vector(tx and ty): ";
cin>>tx>>ty;
cout<<"Choose point of translation: ";
cin>>x>>y;
float translateMatrix[3][3] = {1, 0, tx, 0, 1, ty, 0, 0, 1};
float translatePoint[3][3] = {1, 0, -x, 0, 1, -y, 0, 0, 1};
float translateInverse[3][3] = {1, 0, x, 0, 1, y, 0, 0, 1};
multiply3x3matrix(translateInverse, translateMatrix);
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
temp[i][j] = result[i][j];
multiply3x3matrix(temp, translatePoint);
cout<<endl;
float A[3][1];
for(int i=0; i<3; i++)
A[i][0] = mat[i][0];
float B[3][1];
for(int i=0; i<3; i++)
B[i][0] = mat[i][1];
float C[3][1];
for(int i=0; i<3; i++)
C[i][0] = mat[i][2];
multiplyFinalMatrix(result, A);
for(int i=0; i<3; i++)
for(int j=0; j<1; j++)
A[i][j] = FinalResult[i][j];
multiplyFinalMatrix(result, B);
for(int i=0; i<3; i++)
for(int j=0; j<1; j++)
B[i][j] = FinalResult[i][j];
multiplyFinalMatrix(result, C);
for(int i=0; i<3; i++)
for(int j=0; j<1; j++)
C[i][j] = FinalResult[i][j];
setcolor(11);
line(A[0][0], A[1][0], B[0][0], B[1][0]);
line(B[0][0], B[1][0], C[0][0], C[1][0]);
line(C[0][0], C[1][0], A[0][0], A[1][0]);
}
void scaling()
{
float Sx, Sy, x, y;
float temp[3][3], theta;
cout<<"Enter Scaling vector(Sx and Sy): ";
cin>>Sx>>Sy;
cout<<"Choose point of translation: ";
cin>>x>>y;
cout<<"Enter theta in degrees: ";
cin>>theta;
theta *= (3.14159265359/180);
float RotateMatrixA[3][3] = {cos(theta), -sin(theta), 0, sin(theta), cos(theta), 0, 0, 0, 1};
float RotateMatrixC[3][3] = {cos(theta), sin(theta), 0, -sin(theta), cos(theta), 0, 0, 0, 1};
float ScalingMatrix[3][3] = {Sx, 0, 0, 0, Sy, 0, 0, 0, 1};
float translatePoint[3][3] = {1, 0, -x, 0, 1, -y, 0, 0, 1};
float translateInverse[3][3] = {1, 0, x, 0, 1, y, 0, 0, 1};
multiply3x3matrix(translateInverse, RotateMatrixA);
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
temp[i][j] = result[i][j];
multiply3x3matrix(temp, ScalingMatrix);
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
temp[i][j] = result[i][j];
multiply3x3matrix(temp, RotateMatrixC);
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
temp[i][j] = result[i][j];
multiply3x3matrix(temp, translatePoint);
cout<<endl;
float A[3][1];
for(int i=0; i<3; i++)
A[i][0] = mat[i][0];
float B[3][1];
for(int i=0; i<3; i++)
B[i][0] = mat[i][1];
float C[3][1];
for(int i=0; i<3; i++)
C[i][0] = mat[i][2];
multiplyFinalMatrix(result, A);
for(int i=0; i<3; i++)
for(int j=0; j<1; j++)
A[i][j] = FinalResult[i][j];
multiplyFinalMatrix(result, B);
for(int i=0; i<3; i++)
for(int j=0; j<1; j++)
B[i][j] = FinalResult[i][j];
multiplyFinalMatrix(result, C);
for(int i=0; i<3; i++)
for(int j=0; j<1; j++)
C[i][j] = FinalResult[i][j];
setcolor(14);
line(A[0][0], A[1][0], B[0][0], B[1][0]);
line(B[0][0], B[1][0], C[0][0], C[1][0]);
line(C[0][0], C[1][0], A[0][0], A[1][0]);
}
void rotation()
{
int x, y;
float temp[3][3];
double theta;
cout<<"Enter theta in degrees: ";
cin>>theta;
theta *= (3.14159265359/180);
int choice;
cout<<"\nChoose 1.Clockwise or 2.Anticlockwise: ";
cin>>choice;
cout<<"Choose point of translation: ";
cin>>x>>y;
float RotateMatrix[3][3];
float RotateMatrixA[3][3] = {cos(theta), -sin(theta), 0, sin(theta), cos(theta), 0, 0, 0, 1};
float RotateMatrixC[3][3] = {cos(theta), sin(theta), 0, -sin(theta), cos(theta), 0, 0, 0, 1};
if(choice == 1)
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
RotateMatrix[i][j] = RotateMatrixA[i][j];
else
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
RotateMatrix[i][j] = RotateMatrixC[i][j];
float translatePoint[3][3] = {1, 0, -x, 0, 1, -y, 0, 0, 1};
float translateInverse[3][3] = {1, 0, x, 0, 1, y, 0, 0, 1};
multiply3x3matrix(translateInverse, RotateMatrix);
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
temp[i][j] = result[i][j];
multiply3x3matrix(temp, translatePoint);
cout<<endl;
float A[3][1];
for(int i=0; i<3; i++)
A[i][0] = mat[i][0];
float B[3][1];
for(int i=0; i<3; i++)
B[i][0] = mat[i][1];
float C[3][1];
for(int i=0; i<3; i++)
C[i][0] = mat[i][2];
multiplyFinalMatrix(result, A);
for(int i=0; i<3; i++)
for(int j=0; j<1; j++)
A[i][j] = FinalResult[i][j];
multiplyFinalMatrix(result, B);
for(int i=0; i<3; i++)
for(int j=0; j<1; j++)
B[i][j] = FinalResult[i][j];
multiplyFinalMatrix(result, C);
for(int i=0; i<3; i++)
for(int j=0; j<1; j++)
C[i][j] = FinalResult[i][j];
setcolor(4);
line(A[0][0], A[1][0], B[0][0], B[1][0]);
line(B[0][0], B[1][0], C[0][0], C[1][0]);
line(C[0][0], C[1][0], A[0][0], A[1][0]);
}
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
int i, j;
cout<<"Enter the co-ordinates of triangle ABC:\n";
cout<<"Enter ax, bx, cx: ";
for(i=0; i<3; i++)
cin>>mat[0][i];
cout<<"Enter ay, by, cy: ";
for(i=0; i<3; i++)
cin>>mat[1][i];
for(i=0; i<3; i++)
mat[2][i] = 1;
line(mat[0][0], mat[1][0], mat[0][1], mat[1][1]);
line(mat[0][1], mat[1][1], mat[0][2], mat[1][2]);
line(mat[0][2], mat[1][2], mat[0][0], mat[1][0]);
int choice=1;
do{
cout<<"\nEnter your choice:\t1.Translation\t2.Scaling\t3.Rotation\t";
cin>>choice;
switch(choice)
{
case 1:
translate(); break;
case 2:
scaling(); break;
case 3:
rotation(); break;
}
}while(choice!=0);
getch();
closegraph();
return 0;
}