forked from burakbayramli/books
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleap_frog.py
More file actions
129 lines (106 loc) · 2.54 KB
/
Copy pathleap_frog.py
File metadata and controls
129 lines (106 loc) · 2.54 KB
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
# PySkel.py
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
from numpy import *
import sys
import psyco
psyco.full()
# Set the width and height of the window
global width
global height
# Initial values
width = 300
height = 300
#initial values for position, velocity components, and time increment
global vx, vy, vz, x, y, z, r2, r3, ax, ay, az, dt
x = 1.0
y = 0.0
z = 0.0
vx = 0.0
vy = 0.5
vz = 0.0
m1 = 0.7
m2 = 1 - m1
r2 = x*x + y*y + z*z
r3 = r2*sqrt(r2)
ax = -x/r3
ay = -y/r3
az = -z/r3
#This value keeps a smooth orbit on my workstation
#Smaller values slow down the orbit, higher values speed things up
dt = 0.0001
def init():
glClearColor(0.0, 0.0, 0.0, 1.0)
def plotFunc():
glClear(GL_COLOR_BUFFER_BIT)
glPushMatrix()
glTranslate(m1*x,m1*y,z)
glColor3ub(245, 150, 30)
glutSolidSphere(0.02, 10, 10)
glPopMatrix()
glPushMatrix()
glTranslate(-m2*x,-m2*y,z)
glColor3ub(245, 230, 100)
glutSolidSphere(0.035, 10, 10)
glPopMatrix()
glutSwapBuffers()
def Reshape( w, h):
# To insure we don't have a zero height
if h==0:
h = 1
# Fill the entire graphics window!
glViewport(0, 0, w, h)
# Set the projection matrix... our "view"
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(45.0, 1.0, 1.0, 10.0)
gluLookAt(-1.0, 0.0, 1.5, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0)
# Set the aspect ratio of the plot so that it
# Always looks "OK" and never distorted.
#if w <= h:
# gluOrtho2D(-nRange, nRange, -nRange*h/w, nRange*h/w)
#else:
# gluOrtho2D(-nRange*w/h, nRange*w/h, -nRange, nRange)
# Set the matrix for the object we are drawing
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
def keyboard(key, x, y):
# Allows us to quit by pressing 'Esc' or 'q'
if key == chr(27):
sys.exit()
if key == "q":
sys.exit()
def orbits():
global vx, vy, vz, x, y, z, r2, r3, ax, ay, az
vx += 0.5*ax*dt
vy += 0.5*ay*dt
vz += 0.5*az*dt
x += vx*dt
y += vy*dt
z += vz*dt
r2 = x*x + y*y + z*z
r3 = r2*sqrt(r2)
ax = -x/r3
ay = -y/r3
az = -z/r3
vx += 0.5*ax*dt
vy += 0.5*ay*dt
vz += 0.5*az*dt
#send x,y,z to the display
glutPostRedisplay()
def main():
global width
global height
glutInit(sys.argv)
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE)
glutInitWindowPosition(100,100)
glutInitWindowSize(width,height)
glutCreateWindow("PySkel")
glutReshapeFunc(Reshape)
glutDisplayFunc(plotFunc)
glutKeyboardFunc(keyboard)
glutIdleFunc(orbits)
init()
glutMainLoop()
main()