-
Notifications
You must be signed in to change notification settings - Fork 247
/
Copy pathstonehenge.py
285 lines (251 loc) · 9.01 KB
/
stonehenge.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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
from visual import *
import time
print("""
Press to enter roaming mode, release to exit roaming mode.
In roaming mode, with the mouse button down, move the mouse
above or below the center of the scene to move forward or
backward; right or left rotates your direction of motion.
""")
# Bruce Sherwood
# A surreal scene that illustrates many of the features of VPython
def hourminute():
now = time.localtime(time.time())
hour = now[3] % 12
minute = now[4]
return (hour, minute)
class analog_clock:
def __init__(self, pos=(0,0,0), radius=1., axis=(0,0,1)):
self.pos = vector(pos)
self.axis = vector(axis)
self.radius = radius
self.spheres = []
self.hour = 0
self.minute = -1
for n in range(12):
self.spheres.append(sphere(pos=self.pos+rotate(radius*scene.up,
axis=self.axis, angle=-2.*pi*n/12.), radius=radius/20.,
color=color.hsv_to_rgb((n/12.,1,1)) ))
self.hand = arrow(pos=pos, axis=0.95*radius*scene.up,
shaftwidth=radius/10., color=color.cyan)
self.update()
def update(self):
hour, minute = hourminute()
if self.hour == hour and self.minute == minute: return
self.hand.axis = rotate(0.95*self.radius*scene.up,
axis=self.axis, angle=-2.*pi*minute/60.)
self.spheres[self.hour].radius = self.radius/20.
self.spheres[hour].radius = self.radius/10.
self.hour = hour
self.minute = minute
scene.title = "Surreal Stonehenge"
##scene.stereo = 'redcyan'
scene.height = 600
scene.width = 600
scene.range = (1,1,1)
scene.center = (0,2,20)
scene.userspin = False
scene.userzoom = False
grey = (0.8, 0.8, 0.8)
Nslabs = 8
R = 10
w = 5
d = 0.5
h = 5
photocenter = 0.15*w
# The floor, central post, and ball atop the post
floor = box(pos=(0,-0.1,0),size=(.2,24,24), axis=(0,1,0), color=color.orange, material=materials.wood)
pole= cylinder(pos=(0,0,0),axis=(0,h,0), radius=0.2, color=(1,0,0))
sphere(pos=(0,h,0), radius=0.5, color=(1,0,0))
# Set up the gray slabs, including a portal
### Here is the code used to create the cactus flower display
##import Image # Must install PIL, the Python Imaging Library
##name = "flower"
##width = 128 # must be power of 2
##height = 128 # must be power of 2
##im = Image.open(name+".jpg")
###print im.size # optionally see size of image
### Optional cropping:
###im = im.crop((x1,y1,x2,y2)) # (0,0) is upper left
##im = im.resize((width,height), Image.ANTIALIAS)
##materials.saveTGA(name,im)
for i in range(Nslabs):
theta = i*2*pi/Nslabs
c = cos(theta)
s = sin(theta)
xc = R*c
zc = R*s
if i == 2: # Make a portal
box(pos=(-3.*w/8.,0.75*h/2.,R),
length=0.5*w/2,height=0.75*h,width=d, color=grey)
box(pos=(3.*w/8.0,0.75*h/2.,R),
length=0.5*w/2,height=0.75*h,width=d, color=grey)
box(pos=(0,0.85*h,R),
length=w,height=0.3*h,width=d, color=grey)
else:
slab = box(pos=(R*c, h/2., R*s), axis=(c,0,s),
size=(d,h,w), color=grey)
if i == 0:
photo = materials.texture(data=materials.loadTGA("flower128"), mapping="rectangular")
if i != 6:
for x in range(2):
for y in range(2):
box(pos=slab.pos+vector(-s*photocenter*(2*x-1),photocenter*(2*y-1),c*photocenter*(2*x-1)),
size=(1.1*d,0.9*2*photocenter,0.9*2*photocenter), axis=(c,0,s),
material=photo)
# Decorate back slab with a gold box and a clock
box(pos=(0,h/2.,-R+d/2+0.1), size=(w/2.,w/2.,0.2),
color=(1,0.8,0), material=materials.wood)
clock = analog_clock(pos=(0,h/2.,-R+d/2+0.2+0.2*h/10),
radius=0.2*w, axis=(0,0,1))
# Draw guy wires from the top of the central post
Nwires = 32
for i in range(Nwires):
theta = i*2*pi/Nwires
cylinder(pos=(0,h,0), axis=(R*cos(theta),-h-0.1,R*sin(theta)),
radius=0.02, color=(1,0.7,0))
# Display a pyramid
pyramid(pos=(-4,0,-5), size=(2,2,2), axis=(0,3,0), color=(0,0.6,0), material=materials.marble)
# Display smoke rings rising out of a black tube
smoke = []
Nrings = 20
x0, y0, z0 = -5, 1.5, -2
r0 = 0.1
spacing = 0.2
thick = r0/4
dr = 0.015
dthick = thick/Nrings
gray = 1
cylinder(pos=(x0,0,z0), axis=(0,y0+r0,0), radius=1.5*r0, color=color.black)
# Create the smoke rings
for i in range(Nrings):
smoke.append(ring(pos=(x0,y0+spacing*i,z0), axis=(0,1,0),
radius=r0+dr*i, thickness=thick-dthick*i, color=(gray,gray,gray)))
y = 0
dy = spacing/20
top = Nrings-1
# Roll a log back and forth
rlog = 1
wide = 4
zpos = 2
zface = 5
tlogend = 0.2
v0 = 1
v = v0
omega = -v0/rlog
theta = 0
dt = 0.1
tstop = 0.3
log = frame(pos=(-wide,rlog,zpos), axis=(0,0,1))
cylinder(frame=log,
pos=(0,0,0), axis=(zface-zpos,0,0),
color=(0.8,0.5,0), opacity=0.5)
box(frame=log,
pos=(zface-zpos+tlogend/2.+.01,0,0), axis=(0,0,1),
length=rlog, height=rlog, width=tlogend,
color=color.yellow, opacity=0.5)
leftstop = box(pos=(-wide-rlog-tstop/2,0.6*rlog,(zpos+zface)/2),
length=tstop, height=1.2*rlog, width=(zface-zpos), color=color.red)
rightstop = box(pos=(wide+rlog+tstop/2,0.6*rlog,(zpos+zface)/2),
length=tstop, height=1.2*rlog, width=(zface-zpos), color=color.red)
# Run a ball up and down the pole
y1 = 0.2*h
y2 = 0.7*h
rball = 0.4
cylinder(pos=(0,y1-0.9*rball,0), axis=(0,0.1,0), radius=1.3*pole.radius, color=color.green)
cylinder(pos=(0,y2+0.9*rball,0), axis=(0,0.1,0), radius=1.3*pole.radius, color=color.green)
vball0 = 0.3*v0
vball = vball0
ballangle = 0.05*pi
ball = frame(pos=(0,y1,0))
sphere(frame=ball, radius=rball, color=color.blue)
for nn in range(4):
cc = cone(frame=ball, pos=(0.8*rball,0,0), axis=(3*rball,0,0), radius=0.5*rball, color=color.yellow)
cc.rotate(angle=0.5*nn*pi, axis=(0,1,0), origin=(0,0,0))
rbaseball = 0.3
vbaseball0 = 3*v0
# Display an ellipsoid
Rcloud = 0.8*R
omegacloud = v0/Rcloud
cloud = ellipsoid(pos=(0,0.7*h,-Rcloud), size=(5,2,2),
color=(1,0.5,0.5), opacity=0.3)
rhairs = 0.025 # half-length of crosshairs
dhairs = 2 # how far away the crosshairs are
maxcosine = dhairs/sqrt(rhairs**2+dhairs**2) # if ray inside crosshairs, don't move
haircolor = color.black
roam = 0
# Decorate the front entrance slab
text(pos=(0, 0.77*h, R+d/2), text="No Exit", color=color.yellow,
depth=0.3, height=0.7, align="center")
# Display extruded text
text( pos=(2.0,-0.3*sin(pi/10),-2.0), text='A', height=2.0, depth=0.5,
color=(0,0,1.0), up=(0,cos(pi/3.4),-sin(pi/3.4)), axis=(0.5,0,1))
text( pos=(3.2,0,-2), text='B', height=2.0, depth=0.5,
color=(1.0,1.0,0), axis=(1,0,0.3))
text( pos=(5.0,-0.6*sin(pi/18),-1.4), text='C', height=2.0, depth=0.5,
color=(1.0,0,1.0), axis=(1,0,0.6), up=(0,cos(pi/8),sin(pi/8)) )
while True:
# Toggle roam option
if scene.mouse.events:
m = scene.mouse.getevent()
if m.press or m.drag:
roam = True
elif m.release or m.drop:
roam = False
# If in roaming mode, change center and forward according to mouse position
if roam:
ray = scene.mouse.ray
if abs(dot(ray,scene.forward)) < maxcosine: # do something only if outside crosshairs
newray = norm(vector(ray.x, 0, ray.z))
angle = arcsin(dot(cross(scene.forward,newray),scene.up))
newforward = rotate(scene.forward, axis=scene.up, angle=angle/30)
scene.center = scene.mouse.camera+newforward*mag(scene.center-scene.mouse.camera)
scene.forward = newforward
scene.center = scene.center+scene.forward*ray.y/2.
# Roll the log
theta = theta + omega*dt
log.x = log.x+v*dt
log.rotate(angle=omega*dt)
if log.x >= wide:
v = -v0
omega = -v/rlog
if rightstop.color == color.red:
rightstop.color = color.blue
else:
rightstop.color = color.red
if log.x <= -wide:
v = +v0
omega = -v/rlog
if leftstop.color == color.red:
leftstop.color = color.blue
else:
leftstop.color = color.red
# Move the cloud
cloud.rotate(angle=omegacloud*dt, origin=(0,0,0), axis=(0,1,0))
# Run the ball up and down
ball.pos.y = ball.pos.y+vball*dt
ball.rotate(angle=ballangle, axis=(0,1,0), origin=(0,0,0))
if ball.pos.y >= y2:
vball = -vball0
ballangle = -ballangle
if ball.pos.y <= y1:
vball = +vball0
ballangle = -ballangle
# Move the smoke rings
for i in range(Nrings):
# Raise the smoke rings
smoke[i].pos = smoke[i].pos+vector(0,dy,0)
smoke[i].radius = smoke[i].radius+(dr/spacing)*dy
smoke[i].thickness = smoke[i].thickness-(dthick/spacing)*dy
y = y+dy
if y >= spacing:
# Move top ring to the bottom
y = 0
smoke[top].pos = (x0, y0, z0)
smoke[top].radius = r0
smoke[top].thickness = thick
top = top-1
if top < 0:
top = Nrings-1
# Update the analog clock on the back slab
clock.update()
rate(30)