-
Notifications
You must be signed in to change notification settings - Fork 6
/
09. tranformation.cpp
executable file
·232 lines (202 loc) · 4.97 KB
/
09. tranformation.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
// C code to implement basic
// transformations in OPENGL
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <GL/glut.h>
// window size
#define maxWD 640
#define maxHT 480
// rotation speed
#define thetaSpeed 0.05
int matX[4],matY[4];
int new_matX[4],new_matY[4];
// this creates delay between two actions
void delay(unsigned int mseconds)
{
clock_t goal = mseconds + clock();
while (goal > clock())
;
}
// this is a basic init for the glut window
void myInit(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, maxWD, 0.0, maxHT);
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
// this function just draws a point
void drawPoint(int x, int y)
{
glPointSize(4.0);
glColor3f(0.0f, 0.0f, 1.0f);
glBegin(GL_LINES);
glVertex2i(x, y);
glEnd();
}
void drawP(int matX[4], int matY[4])
{
glPointSize(1.0);
glColor3f(0.0f, 0.0f, 1.0f);
glBegin(GL_POLYGON);
glVertex2i(matX[0], matY[0]);
glVertex2i(matX[1], matY[1]);
glVertex2i(matX[2], matY[2]);
glVertex2i(matX[3], matY[3]);
glEnd();
}
void drawSq(int matX[4],int matY[4])
{
int i,j;
for(i=0;i<4;i++)
{
drawPoint(matX[i],matY[i]);
}
}
void rotateAroundPt(int matX[4],int matY[4], int angle, int cx, int cy)
{
float theta = 0.0;
// while (1)
{
delay(10);
glClear(GL_COLOR_BUFFER_BIT);
drawP(matX,matY);
// update theta anticlockwise rotation
int xf[4], yf[4];
theta = ((float)angle * 3.14159) / 180.0;
// check overflow
/* if (theta >= (2.0 * 3.14159))
theta = theta - (2.0 * 3.14159);
*/
// actual calculations..
for(int i=0;i<4;i++)
{
xf[i] = (matX[i] * cos(theta)) - (matY[i] * sin(theta));
yf[i] = (matX[i] * sin(theta)) + (matY[i] * cos(theta));
}
// drawing the centre point
drawP(xf, yf);
// drawing the rotating point
drawP(xf, yf);
glFlush();
// creating a delay
// so that the point can be noticed
}
}
// this function will translate the point
void translatePoint(int matX[4], int matY[4], int tx, int ty)
{
int fx[4],fy[4];
for(int i=0;i<4;i++)
{
fx[i] = matX[i];
fy[i] = matY[i];
}
//while (1)
{
delay(10);
glClear(GL_COLOR_BUFFER_BIT);
drawP(matX, matY); // drawing the point
// update
for(int i=0;i<4;i++)
{
fx[i] = fx[i] + tx;
fy[i] = fy[i] + ty;
}
// check overflow to keep point in screen
/*if (px > maxWD || px < 0 || py > maxHT || py < 0) {
px = fx;
py = fy;
}*/
drawP(fx, fy); // drawing the point
glFlush();
// creating a delay
// so that the point can be noticed
}
}
// this function draws
void scalePoint(int matX[4], int matY[4], int sx, int sy)
{
int fx[4], fy[4];
//while (1)
{
delay(500);
glClear(GL_COLOR_BUFFER_BIT);
drawP(matX,matY);
// update
for(int i=0;i<4;i++)
{
fx[i] = matX[i] * sx;
fy[i] = matY[i] * sy;
}
drawP(fx, fy); // drawing the point
glFlush();
// creating a delay
// so that the point can be noticed
}
}
// Actual display function
void myDisplay(void)
{
int opt,i;
int tx,ty;
int rotX,rotY,angle;
int sx,sy;
printf("\nEnter the four coordinates of the square : \n");
for(i=0;i<4;i++)
{
scanf("%d",&matX[i]);
scanf("%d",&matY[i]);
}
printf("\nThe four coordinates of the square are : \n");
glClear(GL_COLOR_BUFFER_BIT);
drawP(matX,matY);
glFlush();
while(1){
printf("\nEnter\n\t<1> for translation"
"\n\t<2> for rotation"
"\n\t<3> for scaling\n\t:");
scanf("%d", &opt);
printf("\nGo to the window...");
switch (opt) {
case 1:
printf("\nEnter the X displacement : ");
scanf("%d",&tx);
printf("\nEnter the Y displacement : ");
scanf("%d",&ty);
translatePoint(matX, matY, tx, ty);
break;
case 2:
printf("Enter the angle of rotation : ");
scanf("%d",&angle);
/*printf("\nEnter the point of rotation : \n");
scanf("%d",&rotX);
scanf("%d",&rotY);*/
rotateAroundPt(matX,matY,angle,maxWD / 2, maxHT / 2);
// point will circle around
// the centre of the window
break;
case 3:
printf("\nEnter the X scaling : ");
scanf("%d",&sx);
printf("\nEnter the Y scaling: ");
scanf("%d",&sy);
scalePoint(matX, matY, sx, sy);
break;
}
}
}
void main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(maxWD, maxHT);
glutInitWindowPosition(100, 150);
glutCreateWindow("Transforming point");
glutDisplayFunc(myDisplay);
myInit();
glutMainLoop();
}