(L * sin(theta) * cos(phi), L * sin(theta) * sin(phi), L * cos(theta))) xyz = list(map(tolabframe, rpoints[:, 0:3])) #objects top = v.sphere(radius=R, pos=xyz[0], material=v.materials.marble) top.rotate(angle=pi / 2) stem = v.cylinder(radius=R / 7, pos=xyz[0], axis=(0, 0, L)) #view v.scene.center = (0, 0, 4 * R) v.scene.forward = (0, 1) v.scene.range = 4 * R v.scene.ambient = v.color.gray(0.2) v.distant_light(direction=(0, -1), color=v.color.gray(0.5)) afr = 200 #number of frames per lab second #slowdown = initial param set at top dr = slowdown * afr for i in np.arange(0, len(tpoints), jump): v.rate(dr) if (rpoints[i, 0] > pi / 2 - 0.1): break ##NOTE: when stem reaches parallel to the 'ground' then the animation is forced to cutoff #because the model is not accurate beyond that point if (plot_translation): top.pos = (rpoints[i, 4], rpoints[i, 5]) #vx,vy
vp.arrow(pos=(0, 4, 0), axis=(0, 1, 0), color=vp.color.red) BOXY = vp.box(size=(3, 3, 3), color=(0.5, 0.5, 0.5), material=vp.materials.rough) B1 = vp.sphere(radius=0.3, pos=(R, 0, 0), color=vp.color.magenta, material=vp.materials.emissive) B2 = vp.sphere(radius=0.3, pos=(0, 0, R), color=vp.color.yellow, material=vp.materials.emissive) B3 = vp.arrow(radius=0.3, pos=(0, 0, R), color=vp.color.green, material=vp.materials.emissive) L1 = vp.local_light(pos=B1.pos, color=B1.color) L2 = vp.local_light(pos=B2.pos, color=B2.color) L3 = vp.distant_light(direction=B3.pos, color=B3.color) while True: vp.rate(100) L1.pos = B1.pos = R * vp.vector(vp.cos(A1), vp.sin(A1), B1.z) A1 += 0.02 L2.pos = B2.pos = (R + 0.4) * vp.vector(B2.x, vp.sin(A2), vp.cos(A2)) A2 += 0.055 L3.direction = B3.pos = (R + 3) * vp.vector(vp.sin(A3), B3.y, vp.cos(A3)) B3.axis = B3.pos * -0.3 A3 += 0.033
from __future__ import print_function, division import visual as vp print(__doc__) R = 3 A1 = A2 = A3 = 0.0 vp.arrow(pos=(0, 4, 0), axis=(0, 1, 0), color=vp.color.red) BOXY = vp.box(size=(3, 3, 3), color=(0.5, 0.5, 0.5), material=vp.materials.rough) B1 = vp.sphere(radius=0.3, pos=(R, 0, 0), color=vp.color.magenta, material=vp.materials.emissive) B2 = vp.sphere(radius=0.3, pos=(0, 0, R), color=vp.color.yellow, material=vp.materials.emissive) B3 = vp.arrow(radius=0.3, pos=(0, 0, R), color=vp.color.green, material=vp.materials.emissive) L1 = vp.local_light(pos=B1.pos, color=B1.color) L2 = vp.local_light(pos=B2.pos, color=B2.color) L3 = vp.distant_light(direction=B3.pos, color=B3.color) while True: vp.rate(100) L1.pos = B1.pos = R*vp.vector(vp.cos(A1), vp.sin(A1), B1.z) A1 += 0.02 L2.pos = B2.pos = (R+0.4)*vp.vector(B2.x, vp.sin(A2), vp.cos(A2)) A2 += 0.055 L3.direction = B3.pos = (R+3)*vp.vector(vp.sin(A3), B3.y, vp.cos(A3)) B3.axis = B3.pos * -0.3 A3 += 0.033
class hanoi_state(object): def __init__(self,num_of_disks): self.num_of_disks_per_rod = [num_of_disks,0,0] self.place_of_disk = [0]*num_of_disks def move_disk_to_rod(self,disk,to_rod): self.num_of_disks_per_rod[self.place_of_disk[disk]] -= 1 self.place_of_disk[disk] = to_rod self.num_of_disks_per_rod[self.place_of_disk[disk]] += 1 def visualize_hanoi_solution(num_of_disks): g = hanoi_graphics(num_of_disks,3) state = hanoi_state(num_of_disks) def solve_one(from_rod,to_rod): moves = calc_hanoi_sequence(num_of_disks,from_rod,to_rod) visual.rate(1.0) for move in moves: winsound.Beep(880,50) g.animate_disk_move(disk=move.disk_to_move,to_rod=move.to_rod,to_z_order=state.num_of_disks_per_rod[move.to_rod]) state.move_disk_to_rod(disk=move.disk_to_move,to_rod=move.to_rod) while True: solve_one(0,2) solve_one(2,0) if __name__=='__main__': lights = [visual.distant_light(direction=(0,ydir,0), color=(1.0,1.0,1.0)) for ydir in [-1.0,1.0]] visualize_hanoi_solution(num_of_disks=6)