Vpython beginner

A forum for general discussion of the Python programming language.

Vpython beginner

Postby odyssey » Sun Jan 12, 2014 7:22 am

I want to write a loop and place 12 spheres in a circle surrounding my cube that I made below but I don't know how to do it. So far what I have is

Code: Select all
from __future__ import division
from visual import *
ball1 = sphere(pos=vector(0,5,-5), radius=1, color=color.cyan)
ball2 = sphere(pos=vector(5,5,0), radius=1, color=color.blue)
ball3 = sphere(pos=vector(5,-5,0), radius=1, color=color.green)
ball4 = sphere(pos=vector(-5,5,0), radius=1, color=color.red)
ball5 = sphere(pos=vector(-5,-5,0), radius=1, color=color.orange)
ball6 = sphere(pos=vector(0,-5,5), radius=1, color=color.yellow)
ball7 = sphere(pos=vector(0,-5,-5), radius=1, color=color.blue)
ball8 = sphere(pos=vector(0,5,5), radius=1, color=color.red)
ba = arrow(pos=(ball1.pos), axis=(ball2.pos - ball1.pos), color=color.red)
bb = arrow(pos=(ball2.pos), axis=(ball3.pos - ball2.pos), color=color.red)
bc = arrow(pos=(ball3.pos), axis=(ball4.pos - ball8.pos), color=color.red)
bd = arrow(pos=(ball7.pos), axis=(ball1.pos - ball7.pos), color=color.red)

For the circle of spheres surrounding my cube I got the following but it is a total fail:

Code: Select all
theta = 0
dtheta = pi/12
while theta<2*pi:
    ball1 = sphere(pos=vector(cos(theta), sin(theta),0), radius=1, color=color.cyan)
    theta = theta + dtheta
Posts: 2
Joined: Sat Dec 21, 2013 12:43 am

Re: Vpython beginner

Postby Marbelous » Mon Jan 13, 2014 8:36 pm

You're a long way from a "total fail" here. :? You just forgot to expand the radius of your circle of balls. Add a constant to the x and y components of the position vectors and that blob will expand out into the clock-like circle of spheres you desire. Also, you got the 2pi = 360 degrees in your theta but forgot it in dtheta so your current code will produce a circle of 24 spheres.

Code: Select all
dtheta = 2*pi/12  # Added the 2* to pi to get the right (12) number of balls
r = 10  # overall radius of 10 units to surround 5 unit box
while theta<2*pi:
    ball1 = sphere(pos=vector(cos(theta)*r, sin(theta)*r,0), radius=1, color=color.cyan)  # add r to X and Y in position vector

:mrgreen: Nice Job! If you want to get the most out of Vpython, google TRIGONOMETRY TUTORIALS and study up.
Posts: 116
Joined: Fri May 31, 2013 8:12 pm

Re: Vpython beginner

Postby Marbelous » Mon Jan 13, 2014 8:51 pm

Oh BTW, if you haven't looked at the examples that ship with Vpython definitely do so. Many of them use very advanced math (beyond me for sure :oops: ) but a lot of them are pretty easy to understand and if nothing else they are sure to inspire you. Vpython and numpy (which Vpython imports) together make a very powerful physics modeling tool and the way it abstracts out the difficult 3D OpenGL graphics makes focusing on the modeling itself much easier so you don't get distracted from your overall goal. I think I'll get back to my Vpython bouncing ball and sliding puck models... Thanks for reminding me how much fun it is. :D
Posts: 116
Joined: Fri May 31, 2013 8:12 pm

Return to General Discussions

Who is online

Users browsing this forum: Google [Bot] and 2 guests