-
Notifications
You must be signed in to change notification settings - Fork 0
/
2D_Translation.cpp
173 lines (121 loc) · 3.13 KB
/
2D_Translation.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
//
// main.cpp
// OpenGL
//
// Created by Ibrahim Sharif on 28/9/22.
//
#define GL_SILENCE_DEPRECATION //MacOS Warning Suppression
#include <iostream>
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h>
int width = 500;
int height = width;
int graph_length, x, y, xc, yc;
void initDefaultColor(void);
void draw(void);
void inputs(){
do{
printf("Enter the Graph Quadrant Length (Minimum 10) : ");
scanf("%d", &graph_length);
}while (graph_length < 10);
printf("Enter the Initial Point (X,Y) : ");
scanf("%d %d", &x, &y);
printf("Positive forwards & Negative backwards the translation \n");
printf("Enter Translation of X: ");
scanf("%d", &xc);
printf("Enter Translation of Y : ");
scanf("%d", &yc);
}
void initDefaultColor(){
glClearColor(1, 1, 1, 1);
}
void reshape(int x, int y){
//viewport
glViewport(0, 0, (GLsizei)x, (GLsizei)y);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-graph_length, +graph_length, -graph_length, +graph_length);
//gluPerspective(0, aspect_ratio, 0.01f, 100.0f);
glMatrixMode(GL_MODELVIEW);
//projection
}
void graph(){
glLineWidth(1.0);
glBegin(GL_LINES);
glColor3ub(253, 237, 236);
// Positive X
for (int i=1; i<=graph_length; i++) {
glVertex2i(-graph_length, i);
glVertex2i(graph_length, i);
}
// Negative X
for (int i=graph_length; i>=1; i--) {
glVertex2i(-graph_length, -i);
glVertex2i(graph_length, -i);
}
glColor3ub(234, 250, 241);
// Positive Y
for (int i=1; i<=graph_length; i++) {
glVertex2i(i, -graph_length);
glVertex2i(i, graph_length);
}
// Negative Y
for (int i=graph_length; i>=1; i--) {
glVertex2i(-i, -graph_length);
glVertex2i(-i, graph_length);
}
glEnd();
}
void axis(){
glLineWidth(2.0);
glBegin(GL_LINES);
// X Axis
glColor3ub(236, 112, 99 );
glVertex2i(-graph_length, 0);
glVertex2i(graph_length, 0);
// Y Axis
glColor3ub(82, 190, 128);
glVertex2i(0, -graph_length);
glVertex2i(0, graph_length);
glEnd();
}
void origin(){
glColor3ub(46, 134, 193);
//glPointSize(width/graph_length/2);
glPointSize(10);
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}
void translated(){
glColor3ub(231, 76, 60);
//glPointSize(width/graph_length/2);
glPointSize(10);
glBegin(GL_POINTS);
glVertex2i(x+xc, y+yc);
glEnd();
}
void draw(){
//clear
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
//****** D R A W F U N C T I O N S *******
graph();
axis();
origin();
translated();
glFlush();
}
int main(int argc, char * argv[]) {
inputs();
glutInit(&argc, argv);
glutInitWindowPosition(300, 200);
glutInitWindowSize(width, height);
glutCreateWindow("OpenGL GLUT from MacOS");
initDefaultColor();
glutDisplayFunc(draw);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}