-
Notifications
You must be signed in to change notification settings - Fork 0
/
Geometric Transformation(Scaling, Translation, Rotation).cpp
143 lines (130 loc) · 2.94 KB
/
Geometric Transformation(Scaling, Translation, Rotation).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
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <vector>
#include<windows.h>
#include <GL/glut.h>
using namespace std;
int x, y, option = 0, edges;
vector<int> x_cordinates;
vector<int> y_cordinates ;
int transX, transY;
double scaleX, scaleY;
double angle, angleRad;
double round(double d)
{
return floor(d + 0.5);
}
void drawPolygon()
{
glBegin(GL_POLYGON);
glColor3f(1.0, 0.0, 0.0);
for (int i = 0; i < edges; i++)
{
glVertex2i(x_cordinates[i], y_cordinates[i]);
}
glEnd();
}
void drawPolygonTrans(int x, int y)
{
glBegin(GL_POLYGON);
glColor3f(0.0, 0.0, 1.0);
for (int i = 0; i < edges; i++)
{
glVertex2i(x_cordinates[i] + x, y_cordinates[i] + y);
}
glEnd();
}
void drawPolygonScale(double x, double y)
{
glBegin(GL_POLYGON);
glColor3f(0.0, 0.0, 1.0);
for (int i = 0; i < edges; i++)
{
glVertex2f(x_cordinates[i] * x, y_cordinates[i] * y);
}
glEnd();
}
void drawPolygonRotation(double angleRad)
{
glBegin(GL_POLYGON);
glColor3f(0.0, 0.0, 1.0);
for (int i = 0; i < edges; i++)
{
glVertex2i(round((x_cordinates[i] * cos(angleRad)) - (y_cordinates[i] * sin(angleRad))), round((x_cordinates[i] * sin(angleRad)) + (y_cordinates[i] * cos(angleRad))));
}
glEnd();
}
void myInit(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glColor3f(0.0f, 0.0f, 0.0f);
glPointSize(4.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 800.0, 0.0, 800.0);
}
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 0.0, 0.0);
if (option == 1)
{
drawPolygon();
drawPolygonTrans(transX, transY);
}
else if (option == 2)
{
drawPolygon();
drawPolygonScale(scaleX, scaleY);
}
else if (option == 3)
{
drawPolygon();
drawPolygonRotation(angleRad);
}
glFlush();
}
int main(int argc, char** argv)
{
cout << "Click on what you want to do:\n\n" << endl;
cout << "1. Translation" << endl;
cout << "2. Scaling" << endl;
cout << "3. Rotation" << endl;
cout << "4. Exit" << endl;
cin >> option;
if (option == 4)
{
return 0;
}
cout << "\n\nEnter no of edges for Polygon:\n" << endl;
cin >> edges;
for (int i = 0; i < edges; i++)
{
cout << "Enter co-ordinates for vertex " << i + 1 << " : "; cin >> x >> y;
x_cordinates.push_back(x);
y_cordinates.push_back(y);
}
if (option == 1)
{
cout << "Enter the translation factor for X and Y: "; cin >> transX >> transY;
}
else if (option == 2)
{
cout << "Enter the scaling factor for X and Y: "; cin >> scaleX >> scaleY;
}
else if (option == 3)
{
cout << "Enter the angle for rotation: "; cin >> angle;
angleRad = angle * 3.1416 / 180;
}
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800, 800);
glutInitWindowPosition(200, 300);
glutCreateWindow("Geometric Transformation Transformations");
glutDisplayFunc(myDisplay);
myInit();
glutMainLoop();
return 0;
}