-
Notifications
You must be signed in to change notification settings - Fork 0
/
10. 2D transformation.cpp
161 lines (151 loc) · 3.08 KB
/
10. 2D 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
#include <iostream>
#include <math.h>
#include <time.h>
#include <GL/glut.h>
#include <vector>
using namespace std;
int edge;
vector<int> xpoint;
vector<int> ypoint;
int ch;
double round(double d){
return floor(d + 0.5);
}
void init(){
glClearColor(1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0, 640, 0, 480);
glClear(GL_COLOR_BUFFER_BIT);
}
void rotaion(){
int cx, cy;
cout << "\n Enter Ar point x , y ";
cin >> cx >> cy;
cx = cx + 320;
cy = cy + 240;
glColor3f(0.0, 1.0, 0.0);
glBegin(GL_POINTS);
glVertex2i(cx, cy);
glEnd();
glFlush();
double the;
cout << "\n Enter thetha ";
cin >> the;
the = the * 3.14 / 180;
glColor3f(0, 0, 1.0);
glBegin(GL_POLYGON);
for (int i = 0; i < edge; i++){
glVertex2i(round(((xpoint[i] - cx) * cos(the) - ((ypoint[i] - cy) * sin(the))) +
cx), round(((xpoint[i] - cx) * sin(the) + ((ypoint[i] - cy) * cos(the))) + cy));
}
glEnd();
glFlush();
}
void scale(){
glColor3f(1.0, 0, 0);
glBegin(GL_POLYGON);
for (int i = 0; i < edge; i++){
glVertex2i(xpoint[i] - 320, ypoint[i] - 240);
}
glEnd();
glFlush();
cout << "\n\tIn Scaling whole screen is 1st Qudrant \n";
int sx, sy;
cout << "\t Enter sx, sy \n";
cin >> sx >> sy;// scale the point
for (int i = 0; i < edge; i++){
xpoint[i] = (xpoint[i] - 320) * sx;
ypoint[i] = (ypoint[i] - 240) * sy;
}
glColor3f(0, 0, 1.0);
glBegin(GL_POLYGON);
for (int i = 0; i < edge; i++){
glVertex2i(xpoint[i], ypoint[i]);
}
glEnd();
glFlush();
}
void reflection(){
char reflection;
cout << "Enter Reflection Axis \n";
cin >> reflection;
if (reflection == 'x' || reflection == 'X'){
glColor3f(0.0, 0.0, 1.0);
glBegin(GL_POLYGON);
for (int i = 0; i < edge; i++){
glVertex2i(xpoint[i], (ypoint[i] * -1) + 480);
}
glEnd();
glFlush();
}
else if (reflection == 'y' || reflection == 'Y'){
glColor3f(0.0, 0.0, 1.0);
glBegin(GL_POLYGON);
for (int i = 0; i < edge; i++){
glVertex2i((xpoint[i] * -1) + 640, (ypoint[i]));
}
glEnd();
glFlush();
}
}
void Draw(){if (ch == 2 || ch == 3){
glColor3f(1.0, 0, 0);
glBegin(GL_LINES);
glVertex2i(0, 240);
glVertex2i(640, 240);
glEnd();
glColor3f(1.0, 0, 0);
glBegin(GL_LINES);
glVertex2i(320, 0);
glVertex2i(320, 480);
glEnd();
glFlush();
glColor3f(1.0, 0, 0);
glBegin(GL_POLYGON);
for (int i = 0; i < edge; i++){
glVertex2i(xpoint[i], ypoint[i]);
}
glEnd();
glFlush();
}
if (ch == 1){
scale();
}
else if (ch == 2){
rotaion();
}
else if (ch == 3){
reflection();
}
}
int main(int argc, char **argv){
cout << "\n \t Enter 1) Scaling ";
cout << "\n \t Enter 2) Rotation about arbitrary point";cout << "\n \t Enter 3) Reflection";
cin >> ch;
if (ch == 1 || ch == 2 || ch == 3){
cout << "Enter No of edges \n";
cin >> edge;
int xpointnew, ypointnew;
cout << " Enter" << edge << " point of polygon \n";
for (int i = 0; i < edge; i++){
cout << "Enter " << i << " Point ";
cin >> xpointnew >> ypointnew;
xpoint.push_back(xpointnew + 320);
ypoint.push_back(ypointnew + 240);
}
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(200, 200);
glutCreateWindow("2D");
init();
glutDisplayFunc(Draw);
glutMainLoop();
return 0;
}
else
{
cout << "\n \t Check Input run again";
return 0;
}
}