-
Notifications
You must be signed in to change notification settings - Fork 0
/
2d-transformations.cpp
112 lines (96 loc) · 3.78 KB
/
2d-transformations.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
#include<windows.h>
#include <GL/glut.h>
#include<bits/stdc++.h>
void PointTranslate(float xAxisValueOfPoint, float yAxisValueOfPoint,
float xAxisTranslate, float yAxisTranslate, int translateTimes){
glColor3d(1,1,1);
glPointSize(3);
glBegin(GL_POINTS);
glColor3d(0,1,0);
for(int i=0; i<=translateTimes;i++){
glVertex2d(xAxisValueOfPoint,yAxisValueOfPoint);
glColor3d(1,1,1);
xAxisValueOfPoint += xAxisTranslate;
yAxisValueOfPoint += yAxisTranslate;
}
glEnd();
}
void LineTranslate(float x1AxisValueOfLine, float y1AxisValueOfLine,
float x2AxisValueOfLine, float y2AxisValueOfLine,
float xAxisTranslate, float yAxisTranslate, int translateTimes){
glBegin(GL_LINES);
glColor3d(0,1,0);
for(int i=0; i<=translateTimes;i++){
glVertex2d(x1AxisValueOfLine,y1AxisValueOfLine);
glVertex2d(x2AxisValueOfLine,y2AxisValueOfLine);
glColor3d(1,1,1);
x1AxisValueOfLine += xAxisTranslate;
y1AxisValueOfLine += yAxisTranslate;
x2AxisValueOfLine += xAxisTranslate;
y2AxisValueOfLine += yAxisTranslate;
}
glEnd();
}
void ScaleLine(float x1AxisValueOfLine, float y1AxisValueOfLine,
float x2AxisValueOfLine, float y2AxisValueOfLine,
float xAxisScalingFactor, float yAxisScalingFactor, int scalingTimes){
glBegin(GL_LINES);
glColor3d(0,1,0);
for(int i=0; i<=scalingTimes;i++){
glVertex2d(x1AxisValueOfLine,y1AxisValueOfLine);
glVertex2d(x2AxisValueOfLine,y2AxisValueOfLine);
glColor3d(1,1,1);
x1AxisValueOfLine *= xAxisScalingFactor;
y1AxisValueOfLine *= yAxisScalingFactor;
x2AxisValueOfLine *= xAxisScalingFactor;
y2AxisValueOfLine *= yAxisScalingFactor;
}
glEnd();
}
void RotateLine(float x1AxisValueOfLine, float y1AxisValueOfLine,
float x2AxisValueOfLine, float y2AxisValueOfLine,
float RotateAngle, int RotatingTimes){
float theta = (RotateAngle * M_PI/180);
float x1, y1, x2, y2;
float xp1 = x1 = x1AxisValueOfLine;
float yp1 = y1 = y1AxisValueOfLine;
float xp2 = x2 = x2AxisValueOfLine;
float yp2 = y2 = y1AxisValueOfLine;
glBegin(GL_LINES);
glColor3d(0,1,0);
for(int i=0; i<=RotatingTimes;i++){
glVertex2d(xp1,yp1);
glVertex2d(xp2,yp2);
glColor3d(1,1,1);
xp1 = x1 * cos(theta) - y1 * sin(theta);
yp1 = y1 * cos(theta) - x1 * sin(theta);
xp2 = x2 * cos(theta) + y2 * sin(theta);
yp2 = y2 * cos(theta) + x2 * sin(theta);
theta += (RotateAngle * M_PI/180);
}
glEnd();
}
void display(){
// Single Point Translation
PointTranslate(0.3,0.2,0.1,0.2,1);
// Multiple Points (5 Times) Translation
PointTranslate(0.2,0.2,0.1,0.1,5);
// Multiple Lines (5 Times) Translation
LineTranslate(-0.2,-0.1,-0.2,-0.3,0.1,0.2,5);
// Multiple Lines (5 Times) Scaling [Sr = 1.25]
ScaleLine(0.1,0.1,0.2,0.1,1.25,1.25,5);
// Multiple Lines (5 Times) Rotating [30 degree]
RotateLine(0.0,0.0,0.4,0.4,30,5);
glFlush();
}
int main(int argc, char ** argv)
{
glutInit(&argc, argv);
glutInitWindowSize(640,480);
glutInitWindowPosition(10,10);
glutCreateWindow("Transformations");
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE | GLUT_DEPTH);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}