-
Notifications
You must be signed in to change notification settings - Fork 199
/
Copy pathPyBounce.py
118 lines (97 loc) · 2.38 KB
/
PyBounce.py
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
# PyBounce.py
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
from Numeric import *
import sys
# Set the width and height of the window
global width, height, axrng, anim, x, y ,dx, dy
global xborder, yborder, radius
#initial values
x = -0.67
y = 0.34
dx = dy = 1
radius = 0.5
width = height = 500
axrng = 10.0
anim = 1
xborder = yborder = axrng
def init():
glClearColor(0.0, 0.0, 0.0, 1.0)
glColor3ub(255, 0, 0)
def idle():
if anim == 1:
glutPostRedisplay()
def plotfunc():
global x, y, dx, dy
glClear(GL_COLOR_BUFFER_BIT)
x = x + 0.005*dx
y = y + 0.005*dy
glPushMatrix()
glTranslate(x,y,0)
glutSolidSphere(radius, 50, 50)
#glScalef(0.5, 0.5, 0.5)
#glutSolidTeapot(radius)
glPopMatrix()
if x >= xborder-radius or x <= -xborder+radius:
dx = -1*dx
if y >= yborder-radius or y <= -yborder+radius:
dy = -1*dy
glutSwapBuffers()
def reshape( w, h):
global xborder, yborder, x, y
# 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()
# Set the aspect ratio of the plot so that it
# Always looks "OK" and never distorted.
if w <= h:
gluOrtho2D(-axrng, axrng, -axrng*h/w, axrng*h/w)
yborder = axrng*h/w
xborder = axrng
else:
gluOrtho2D(-axrng*w/h, axrng*w/h, -axrng, axrng)
xborder = axrng*w/h
yborder = axrng
if x <= -xborder:
x = -xborder + (2*radius)
if x >= xborder:
x = xborder - (2*radius)
if y <= -yborder:
y = -yborder + (2*radius)
if y >= yborder:
y = yborder - (2*radius)
# 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'
# We can animate by "a" and stop by "s"
global anim
if key == chr(27):
sys.exit()
if key == "a":
anim = 1
if key == "s":
anim = 0
if key == "q":
sys.exit()
def main():
global width, height
glutInit(sys.argv)
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE)
glutInitWindowPosition(100,100)
glutInitWindowSize(width,height)
glutCreateWindow("PyBounce")
glutReshapeFunc(reshape)
glutDisplayFunc(plotfunc)
glutKeyboardFunc(keyboard)
glutIdleFunc(idle)
init()
glutMainLoop()
main()