-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorbit.cpp
149 lines (133 loc) · 3.9 KB
/
orbit.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
#include <iostream>
#include <cmath>
#include <GL/glut.h>
int refreshmill = 1;
int counterForRevolution = 0;
void init(void)
{
glClearColor(9.0, 9.0, 9.0, 0.0);
glShadeModel(GL_FLAT);
}
void timer(int value)
{
glutPostRedisplay();
glutTimerFunc(refreshmill, timer, 0);
}
//drawOrbitPlanet function draws an orbit along with a planet revolving around the center
void drawOrbitPlanet(float rx, float ry, float radiusPlanet)
{
float x = 0, y = ry; //starting from point (0,ry)
float pointX[10000]; //x-coordinates
float pointY[10000]; //y-coordinates
float decisionParameter;
int counter = 0;
float xStep = 0.1, yStep = 0.1;
decisionParameter = pow(ry, 2) - pow(rx, 2) * ry + 0.25 * pow(rx, 2);
while (pow(ry, 2) * x <= pow(rx, 2) * y)
{
pointX[counter] = x;
pointY[counter] = y;
counter++;
// pointX[counter] = y;
// pointY[counter] = x;
// counter++;
x = x + xStep;
if (decisionParameter < 0)
{
decisionParameter = decisionParameter + 2 * pow(ry, 2) * x + pow(ry, 2);
}
else
{
y = y - yStep;
decisionParameter = decisionParameter + 2 * pow(ry, 2) * x - 2 * pow(rx, 2) * y + pow(ry, 2);
}
}
//for region 2;
/* x = rx;
y = 0; */
decisionParameter = pow(ry, 2) * pow((x + 0.5), 2) + pow(rx, 2) * pow(y - 1, 2) - rx * rx * ry * ry;
//decisionParameter = pow(rx, 2) - rx * pow(ry, 2) + 0.25 * pow(ry, 2);
//while (pow(ry, 2) * x > pow(rx, 2) * y)
while (y > 0)
{
pointX[counter] = x;
pointY[counter] = y;
counter++;
// y = y + yStep;
y = y - yStep;
if (decisionParameter >= 0)
{
decisionParameter = decisionParameter - 2 * y * rx * rx + rx * rx;
/* x = x - xStep;
decisionParameter = decisionParameter - 2 * x * pow(ry, 2) + 2 * y * pow(rx, 2) + pow(rx, 2); */
}
else
{
x = x + xStep;
decisionParameter = decisionParameter - 2 * y * rx * rx + rx * rx + 2 * ry * ry * x;
//decisionParameter = decisionParameter + 2 * y * pow(rx, 2) + pow(rx, 2);
}
}
for (int i = 0; i < counter; i++)
{
pointX[counter + i] = pointX[counter - i - 1];
pointY[counter + i] = -pointY[counter - i - 1];
}
for (int i = 0; i < counter; i++)
{
pointX[2 * counter + i] = -pointX[i];
pointY[2 * counter + i] = -pointY[i];
}
for (int i = 0; i < counter; i++)
{
pointX[3 * counter + i] = -pointX[counter - i - 1];
pointY[3 * counter + i] = pointY[counter - i - 1];
}
glBegin(GL_LINE_STRIP);
glColor3f(0.0, 0.0, 0.0);
for (int i = 0; i < 4 * counter; i++)
{
glVertex2f(pointX[i], pointY[i]);
}
glEnd();
glPushMatrix();
glColor3f(0.0, 100.0, 100.0);
//glRotatef(angle, 0, 0, 1);
glTranslatef(pointX[counterForRevolution], pointY[counterForRevolution], 0);
glutSolidSphere(radiusPlanet, 100, 100);
glPopMatrix();
counterForRevolution += 4;
if (counterForRevolution >= 4 * counter)
{
counterForRevolution = 0;
}
}
//drawOrbitPlanet function ends here
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 0.0, 0.0);
drawOrbitPlanet(30, 15, 3);
//drawSphere(30, 0);
glutSwapBuffers();
}
void reshape(int w, int h)
{
glMatrixMode(GL_PROJECTION);
gluPerspective(50.0, w / (GLfloat)h, 3.0, 90.0);
glMatrixMode(GL_MODELVIEW);
gluLookAt(0.0, 0.0, 60.0, 0.0, 0.0, 0.0, 0.0, 0.1, 0.0);
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(1100, 700);
glutCreateWindow("Solar System");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutTimerFunc(0, timer, 0);
glutMainLoop();
return 0;
}