-
Notifications
You must be signed in to change notification settings - Fork 0
/
transformation.cpp
235 lines (182 loc) · 4.74 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
#include <GL/freeglut.h>
#include <GL/gl.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int factor[3][3] = { {0, 0, 0},
{0, 0, 0},
{0, 0, 0}
};
float factor1[3][3] = { {0, 0, 0},
{0, 0, 0},
{0, 0, 0}
};
int res[3][1] = {{0},{0},{0}};
float res1[3][1] = {{0},{0},{0}};
void multiply(int mat1[][3], int mat2[][1], int res[][1])
{
int i, j, k;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 1; j++)
{
res[i][j] = 0;
for (k = 0; k < 3; k++)
res[i][j] += mat1[i][k]*mat2[k][j];
}
}
}
void multiply1(float mat1[][3], float mat2[][1], float res[][1])
{
int i, j, k;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 1; j++)
{
res[i][j] = 0;
for (k = 0; k < 3; k++)
res[i][j] += mat1[i][k]*mat2[k][j];
}
}
}
void translation(int tx,int ty)
{
factor[0][0] = 1;
factor[1][1] = 1;
factor[2][2] = 1;
factor[0][2] = tx;
factor[1][2] = ty;
}
void rotation(int angle1)
{
float val = angle1*3.14/180;
//float val = angle1;
factor1[2][2] = 1;
factor1[0][0] = cos(val);
factor1[0][1] = -sin(val);
factor1[1][0] = sin(val);
factor1[1][1] = cos(val);
}
void scaling(int sx,int sy)
{
factor[0][0] = sx;
factor[1][1] = sy;
factor[2][2] = 1;
factor[0][2] = 0;
factor[1][2] = 0;
}
void init()
{
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0,1920,0,1080);
}
void draw()
{
//glClear only clears the colours on the screen
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
//to give color to the polygon
glColor3f(1.0,0.0,0.0);
glVertex2i(100,100);
glVertex2i(200,100);
//glVertex2f(0.0,0.8);
glVertex2i(200,200);
glVertex2i(100,200);
glEnd();
int store[8] = {0,0,0,0,0,0,0,0};
translation(25,25);
int input1[3][1] = {{100},{100},{1}};
int input2[3][1] = {{200},{100},{1}};
int input3[3][1] = {{200},{200},{1}};
int input4[3][1] = {{100},{200},{1}};
multiply(factor,input1,res);
store[0] = res[0][0];
store[1] = res[1][0];
multiply(factor,input2,res);
store[2] = res[0][0];
store[3] = res[1][0];
multiply(factor,input3,res);
store[4] = res[0][0];
store[5] = res[1][0];
multiply(factor,input4,res);
store[6] = res[0][0];
store[7] = res[1][0];
glBegin(GL_POLYGON);
//to give color to the polygon
glColor3f(0,1.0,0.0);
glVertex2i(store[0],store[1]);
glVertex2i(store[2],store[3]);
//glVertex2f(0.0,0.8);
glVertex2i(store[4],store[5]);
glVertex2i(store[6],store[7]);
glEnd();
//rotation
rotation(30);
float input5[3][1] = {{100},{100},{1}};
float input6[3][1] = {{200},{100},{1}};
float input7[3][1] = {{200},{200},{1}};
float input8[3][1] = {{100},{200},{1}};
float store1[8] = {0,0,0,0,0,0,0,0};
multiply1(factor1,input5,res1);
store1[0] = res1[0][0];
store1[1] = res1[1][0];
multiply1(factor1,input6,res1);
store1[2] = res1[0][0];
store1[3] = res1[1][0];
multiply1(factor1,input7,res1);
store1[4] = res1[0][0];
store1[5] = res1[1][0];
multiply1(factor1,input8,res1);
store1[6] = res1[0][0];
store1[7] = res1[1][0];
glBegin(GL_POLYGON);
//to give color to the polygon
glColor3f(0,0.0,1.0);
glVertex2f(store1[0],store1[1]);
glVertex2f(store1[2],store1[3]);
//glVertex2f(0.0,0.8);
glVertex2f(store1[4],store1[5]);
glVertex2f(store1[6],store1[7]);
glEnd();
scaling(2,2);
int input9[3][1] = {{100},{100},{1}};
int input10[3][1] = {{200},{100},{1}};
int input11[3][1] = {{200},{200},{1}};
int input12[3][1] = {{100},{200},{1}};
multiply(factor,input9,res);
store[0] = res[0][0];
store[1] = res[1][0];
multiply(factor,input10,res);
store[2] = res[0][0];
store[3] = res[1][0];
multiply(factor,input11,res);
store[4] = res[0][0];
store[5] = res[1][0];
multiply(factor,input12,res);
store[6] = res[0][0];
store[7] = res[1][0];
glBegin(GL_POLYGON);
//to give color to the polygon
glColor3f(1.0,1.0,0.0);
glVertex2i(store[0],store[1]);
glVertex2i(store[2],store[3]);
//glVertex2f(0.0,0.8);
glVertex2i(store[4],store[5]);
glVertex2i(store[6],store[7]);
glEnd();
//glFlush clears every buffer on the screen
glFlush();
}
int main(int argc,char **argv)
{
//initializing the glut library
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_SINGLE);
glutInitWindowSize(1920,1080);
glutInitWindowPosition(1,1);
glutCreateWindow("opengl");
init();
glutDisplayFunc(draw);
glutMainLoop();
return 0;
}