-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRotateLineAboutPoint.cpp
187 lines (130 loc) · 3.58 KB
/
RotateLineAboutPoint.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//
// 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>
#include <math.h>
#define PI 3.14159265359
int width = 500;
int height = width;
double graph_length, xc, yc, xend, yend ;
double xnew, ynew, degree, angle;
void initDefaultColor(void);
void draw(void);
void inputs(){
do{
printf("Enter the Graph Quadrant Length (Minimum 10) : ");
scanf("%lf", &graph_length);
}while (graph_length < 10);
printf("Enter the Starting Point (X,Y) : ");
scanf("%lf %lf", &xc, &yc);
printf("Enter the Ending Point (X,Y) : ");
scanf("%lf %lf", &xend, ¥d);
printf("Enter the Angle Degree of Rotation : ");
scanf("%lf", °ree);
}
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);
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 original(){
glColor3ub(46, 134, 193);
//glPointSize(width/graph_length/2);
glLineWidth(3.0);
glBegin(GL_LINES);
glVertex2i(xc, yc);
glVertex2i(xend, yend);
glEnd();
}
void rotated(){
angle = degree * (PI/180);
//xnew = xc + (xend -xc)*cos(angle) - (yend-yc)*sin(angle);
//ynew = yc + (xend -xc)*sin(angle) - (yend-yc)*cos(angle);
//xnew = xend + (xend -xc)*cos(angle) - (yend-yc)*sin(angle);
//ynew = yend + (xend -xc)*sin(angle) - (yend-yc)*cos(angle);
xnew = xend + (xc - xend)*cos(angle) - (yc - yend)*sin(angle);
ynew = yend + (xc - xend)*sin(angle) - (yc - yend)*cos(angle);
glColor3ub(125, 60, 152);
glLineWidth(3.0);
glBegin(GL_LINES);
glVertex2f(xc, yc);
glVertex2f(xnew, ynew);
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();
original();
rotated();
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;
}