-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2dcomp.cpp
More file actions
218 lines (196 loc) · 5.47 KB
/
2dcomp.cpp
File metadata and controls
218 lines (196 loc) · 5.47 KB
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
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <iostream>
#include <vector>
#include <GL/glut.h>
using namespace std;
int pntX1, pntY1, choice = 0, edges;
vector<int> pntX;
vector<int> pntY;
int shx, shy;
int res[3][1] = {0};
const int REFLECTION_MATRIX[4][3][3] = {{{-1, 0, 0}, {0, -1, 0}, {0, 0, 1}}, {{1, 0, 0}, {0, -1, 0}, {0, 0, 1}}, {{-1, 0, 0}, {0, 1, 0}, {0, 0, 1}}, {{0, 1, 0}, {1, 0, 0}, {0, 0, 1}}};
enum LINE_TYPE{
ORIGIN, X_AXIS, Y_AXIS, XY_LINE
};
void mutliplyMatrices(const int tr[3][3], const int pt[3][1]){
memset(res, 0, 3 * 1 * sizeof(int));
for(int i = 0; i < 3; i++)
for(int j = 0; j < 1; j++)
for(int k = 0; k < 3; k++)
res[i][j] += tr[i][k] * pt[k][j];
}
void drawPolygon()
{
glBegin(GL_QUADS);
glColor3f(0.0, 1.0, 0.0);
for (int i = 0; i < 4; i++)
{
glVertex2i(pntX[i], pntY[i]);
}
glEnd();
}
void drawReflection(LINE_TYPE lt){
glBegin(GL_QUADS);
glColor3f(1.0, 0.0, 0.0);
for (int i = 0; i < 4; i++)
{
int pt_arr[3][1] = {{pntX[i]}, {pntY[i]}, {1}};
mutliplyMatrices(REFLECTION_MATRIX[lt],pt_arr);
glVertex2i(res[0][0], res[1][0]);
}
glEnd();
}
void drawShear(LINE_TYPE lt){
glBegin(GL_QUADS);
glColor3f(1.0, 0.0, 0.0);
if(lt == X_AXIS){
int shear_matrix [3][3] = {{1, shx, 0}, {0, 1, 0}, {0, 0, 1}};
for (int i = 0; i < 4; i++)
{
int pt_arr[3][1] = {{pntX[i]}, {pntY[i]}, {1}};
mutliplyMatrices(shear_matrix, pt_arr);
glVertex2i(res[0][0], res[1][0]);
}
glEnd();
}
else if(lt == Y_AXIS){
int shear_matrix [3][3] = {{1, 0, 0}, {shy, 1, 0}, {0, 0, 1}};
for (int i = 0; i < 4; i++)
{
int pt_arr[3][1] = {{pntX[i]}, {pntY[i]}, {1}};
mutliplyMatrices(shear_matrix, pt_arr);
glVertex2i(res[0][0], res[1][0]);
}
glEnd();
}
}
void drawLine(LINE_TYPE lt){
glPushAttrib(GL_ENABLE_BIT);
glLineStipple(1, 0xAAAA);
glLineWidth(2);
glEnable(GL_LINE_STIPPLE);
glBegin(GL_LINES);
glColor3f(0.0, 0.0, 1.0);
switch(lt){
case X_AXIS: {
glVertex2i(-700, 0);
glVertex2i(700, 0);
}
break;
case Y_AXIS: {
glVertex2i(0, -700);
glVertex2i(0, 700);
}
break;
case XY_LINE: {
glVertex2i(-700, -700);
glVertex2i(700, 700);
}
break;
}
glPopAttrib();
glEnd();
}
void myInit(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glColor3f(0.0f, 0.0f, 0.0f);
glPointSize(3.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-640.0, 640.0, -480.0, 480.0);
}
void myDisplay(void)
{
// Initial Polygon
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 0.0, 0.0);
drawPolygon();
glFlush();
while (true)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 0.0, 0.0);
cout << "Enter your choice:\n\n";
cout << "1. Reflection about origin" << endl;
cout << "2. Reflection about X-axis" << endl;
cout << "3. Reflection about Y-axis" << endl;
cout << "4. Reflection about X=Y" << endl;
cout << "5. Shearing along X-axis" << endl;
cout << "6. Shearing along Y-axis" << endl;
cout << "7. Exit" << endl;
cout << "------------------" << endl;
cin >> choice;
if (choice == 7)
{
return;
}
LINE_TYPE lt;
switch(choice){
case 1: {
drawPolygon();
lt = ORIGIN;
drawReflection(lt);
}
break;
case 2:{
drawPolygon();
lt = X_AXIS;
drawLine(lt);
drawReflection(lt);
}
break;
case 3: {
drawPolygon();
lt = Y_AXIS;
drawLine(lt);
drawReflection(lt);
}
break;
case 4: {
drawPolygon();
lt = XY_LINE;
drawLine(lt);
drawReflection(lt);
}
break;
case 5: {
cout << "Enter the shearing factor along x axis: " << endl;
cin >> shx;
drawPolygon();
lt = X_AXIS;
drawShear(lt);
}
break;
case 6: {
cout << "Enter the shearing factor along y axis: " << endl;
cin >> shy;
drawPolygon();
lt = Y_AXIS;
drawShear(lt);
}
}
glFlush();
}
}
int main(int argc, char **argv)
{
cout << "Enter vertices\n";
for (int i = 0; i < 4; i++)
{
cout << "Enter co-ordinates for vertex " << i + 1 << "(X, Y): ";
cin >> pntX1 >> pntY1;
pntX.push_back(pntX1);
pntY.push_back(pntY1);
}
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(100, 150);
glutCreateWindow("Basic 2D Transformations");
glutDisplayFunc(myDisplay);
myInit();
glutMainLoop();
}