VPython Loops: Difference between revisions
No edit summary |
No edit summary |
||
Line 33: | Line 33: | ||
ball = sphere(pos=(distance,0,0) , radius=1) </nowiki> | ball = sphere(pos=(distance,0,0) , radius=1) </nowiki> | ||
This loop will create a ball of the same radius in a line along the x axis every 10 meters. It's possible to alter distances along the y and z axis the same why simply by creating a variable for the y or z part of the vector. | This loop will create a ball of the same radius in a line along the x axis every 10 meters. It's possible to alter distances along the y and z axis the same why simply by creating a variable for the y or z part of the vector. | ||
[[File:Balls_in_line.png]] | [[File:Balls_in_line.png|200px]] | ||
While loops can also be used to create objects in a circular path! | While loops can also be used to create objects in a circular path! |
Revision as of 21:14, 5 December 2015
Claimed by Alyx Falis
What loops are and how to use them in a VPython Program
The Main Idea
In programming, loops exist to execute a singular or series of statements for a specified number of times. This simplifies executing any function multiple times. Depending on the type of loop, the functions will know when to be carried out and how many time/for how long it will be carried out.
While Loop
While loops are used to repeat a function until a certain value or criteria is met.
Examples
While loops are very useful in physics for representing time intervals. For example, if you wanted to express that a object was moving over a certain time period you could represent it as such:
#initial values ball = sphere(pos=vector(0, 0, 0), radius=1) time = 0 velocity = 5 #calculations while time < 100 time = time+10 pos = velocity*time
This loop starts with the initial values of position = 0 meters, time = 0 seconds, and velocity = 5 m/s. The loop will run until the time value reaches 100 seconds. Inside the loop, time is first updated so that every iteration of the loop increases the time value (i.e. the first run of the loop time becomes 10 seconds, the second run time becomes 20 seconds, and so on). Next the position is updated using a physics formula: change in distance = velocity * time.
While loops can also be used to create objects in a pattern. For example, if you wanted to create series of spheres in a line you could use the following code:
#initial values distance = 0 #calculations while distance < 100: distance = distance + 10 ball = sphere(pos=(distance,0,0) , radius=1)
This loop will create a ball of the same radius in a line along the x axis every 10 meters. It's possible to alter distances along the y and z axis the same why simply by creating a variable for the y or z part of the vector.
While loops can also be used to create objects in a circular path!
#initial values theta = 0 #calculations while theta < 2*pi: theta= theta + pi/6 location = vector(cos(theta),sin(theta),0) ball = sphere(pos=(location), radius=0.1)
This code creates a series of 12 spheres in a circle by changing theta each time the loop is iterated. Changing the the increment by which theta is increased (in this case pi/6) you can change the number of spheres that are formed.
For Loop
For loops are used to repeat a function a specific number of times.
Be sure to show all steps in your solution and include diagrams whenever possible
Simple
Middling
Difficult
Connectedness
- How is this topic connected to something that you are interested in?
- How is it connected to your major?
- Is there an interesting industrial application?
History
Put this idea in historical context. Give the reader the Who, What, When, Where, and Why.
See also
Are there related topics or categories in this wiki resource for the curious reader to explore? How does this topic fit into that context?
Further reading
Books, Articles or other print media on this topic
External links
References
This section contains the the references you used while writing this page