In this workshop, we are often going to use Glowscript to create computational models, which will serve as a powerful tool to help us create visualizations and apply the ideas in this course to more real-world contexts. Below are some of the common Python commands that we will use and some coding tips compiled by previous students. (Note: we do not expect you to have any coding experience prior to this course, and we will not expect you to write a program from scratch. We will primarily be asking you to interpret chunks of code with your group members and adjust/modify pieces of code that you will be given).
sphere
, pos=vec(0,3500,0)
says where the center of the sphere should be positioned, color=color.white
says to make the sphere white, and radius=100
sets the radius of the sphere. The word cloud
before the equals sign is simply a name that you will use to refer to this object. Look here for a list of useful 3D objects in Glowscript .cloud = sphere(pos=vec(0,3500,0), color=color.white, radius=100)
box
with side lengths of 2 on each side. opacity=0.1
will make the box mostly transparent.MyBox = box(pos=vec(0,0,0), length=2, width=2, height=2, color=color.white, opacity=0.1)
pos
, length
, width
, radius
, color
, etc). You can also change the attributes of an object later in the code. This can be particularly useful for the updating the position of an object, which can let you simulating a moving object. In the example below, the first line of code creates a green, 2×2 box that is centered at the origin (0,0,0). In the second line of code, the box position is changed to (3,4,5) without changing the other characteristics of the box.object1=box(pos=vec(0,0,0), length=2, width=2,color=color.green) object1.pos=vec(3,4,5)
velocity
attribute of (5,0,0) to that object. This is useful because it allows us to easily recall values from an object by referring to a memorable object name like basketball
.basketball=sphere(pos=vec(0,0,0), color=color.white, radius=10) basketball.velocity=vec(5,0,0)
print()
command will output any value called from within the parentheses. You can print variables and attributes from your code as a way of checking what numbers the simulation has calculated. The following code creates a conical object named IceCreamCone
, and the print(IceCreamCone.size.x)
command prints the x-component of the object's size attribute. If you want to print text, it must be transformed into a “string” of letters by using quotation marks within the print()
command.IceCreamCone=cone(pos=vec(0,0,0), axis=vec(0,-5,0), size=vec(3,1,1)) print("Ice cream cone length:", IceCreamCone.size.x) print("Two scoops of butter pecan please!")
gcurve
command which plots x-y data points as a scatterplot and connects the points with a line. In general, Three lines of code are needed to graph in Glowscript…MyGraph1
is needed to set up the graphing window with its various axes titles, minimum and maximum boundaries, and other features. The line with HeightGraph
names a particular data-set that will be represented on our scatterplot. Lastly, within the while loop, the HeightGraph.plot(t,Cart.pos.y)
command is used to plot
data points for our specified data-set (t
will be on the x-axis and Cart.pos.y
will be on the y-axis).MyGraph1 = graph(title='Height vs Time', xtitle='Time (s)', ytitle='Height (m)', fast=False) #Name our graph and set some features HeightGraph = gcurve(color=color.red, label='Height') # Specifies a name for the data-set that we will be plotting Cart=box(pos=vec(0,0,0), size=vec(1,1,1),color=color.magenta) t=0 dt=1 while True: rate(100) Cart.pos=Cart.pos+vec(0,1,0)*dt # This line plots data points as the program iterates HeightGraph.plot(t,Cart.pos.y) # t will be on the x-axis and Cart.pos.y will be on the y-axis t=t+dt
* If-then statements - these act as triggers to start some new event once a condition in met. For example, it we have an object moving to the right with particle.velocity=vec(1,0,0)
and we want it to move in the opposite direction after 10 seconds, then you might use the following code.
Particle = sphere(pos=vec(0,0,0)) Particle.velocity=vec(1,0,0) t=0 dt=1 tf=20 while t >= tf: rate(100) Particle.pos=Particle.pos+Particle.velocity*dt if t >= 10: Particle.velocity=vec(-1,0,0) t=t+dt
MyAnswer = 123 * 44 + 566 / 33 - 935 print(MyAnswer)
abs()
command.Number=5 AbsoluteNumber = abs(Number)
A=5.42*10**4
A=5.24E4
sqrt()
command.qwer=sqrt(625)
vec()
command. In this command, you simply have to define the three components of the vector (x, y, and z): vec($v_x$,$v_y$,$v_z$). Python will then automatically use vector subtraction/addition if you try to add two vectors. However, it will create an error if you try to add/subtract a scalar and a vector.position1=vec(3,4,5) position2=vec(2,1,0) separation=position2-position1 #This will give separation=vec(-1,-3,-5) testing=separation-4 #This will give an error
mag()
command will calculate the magnitude of any vector. vector1=vec(1,2,3) mag1=mag(vector1) #This will calculate sqrt(1^2+2^2+3^2)
A=4+4/2 B=(4+4)/2
Num_1
and num_1
, Python would treat these as two distinct objects. If you initially set your variable name to be Num_1
but wrote num_1
throughout the rest of your code, Python would not know what num_1
was. Just be conscious that when you are naming or using different elements in your code, that you are writing it the same way every time.#
or ##
in that line (see the examples below). You can use comments in several different ways:#
). A suggestion is to never delete lines of code unless you are 100% positive that either 1: you don’t need it or 2: it’s wrong. Basically, comments let you erase lines of code without actually erasing them.## Set the radius (this line would be ignored by the program) R=20 #cm (Everything in this line before the # would run in the program, everything after the # is ignored)
Check out this link for a useful math review.