Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
summer_2019:glowscript_tips [2019/08/07 04:37] wellerd |
summer_2019:glowscript_tips [2019/08/13 00:27] (current) wellerd |
||
---|---|---|---|
Line 23: | Line 23: | ||
* **Printing values and text** - The ''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. | * **Printing values and text** - The ''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. | ||
<code> | <code> | ||
- | IceCreamCone=cone(pos=vec(10,20,0), axis=vec(5,0,0), size=vec(12,3,3) | + | IceCreamCone=cone(pos=vec(0,0,0), axis=vec(0,-5,0), size=vec(3,1,1)) |
- | print(IceCreamCone.size.x) | + | print("Ice cream cone length:", IceCreamCone.size.x) |
print("Two scoops of butter pecan please!") | print("Two scoops of butter pecan please!") | ||
+ | </code> | ||
+ | |||
+ | ==== Graphing ==== | ||
+ | * **Graphs** - the most convenient graphs in Glowscript utilize the ''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... | ||
+ | - A line above the while loop which creates the axes and titles of the graph (line 3 in the picture below). | ||
+ | - A line above the while loop which tells the program what you are about to graph (line 4 in the picture below). | ||
+ | - A line inside of the while loop which plots the respective x-y data point for that iteration of the while loop (line 19 in the picture below). | ||
+ | {{:summer_2019:python-trinket-physics.png?400|}} | ||
+ | |||
+ | * **Another graphing example** - In the following code, ''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). | ||
+ | <code> | ||
+ | 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 | ||
+ | </code> | ||
+ | |||
+ | ==== Programmer Logic ==== | ||
+ | * **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. | ||
+ | <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 | ||
</code> | </code> | ||
Line 31: | Line 76: | ||
* **Arithmetic** - arithmetic functions may be executed by a computer program, similar to how one would use a calculator. The following code will calculate this difficult math for us. | * **Arithmetic** - arithmetic functions may be executed by a computer program, similar to how one would use a calculator. The following code will calculate this difficult math for us. | ||
<code> | <code> | ||
- | MyAnswer= 123 * 44 + 566 / 33 - 935 | + | MyAnswer = 123 * 44 + 566 / 33 - 935 |
print(MyAnswer) | print(MyAnswer) | ||
</code> | </code> | ||
- | * **Absolute Value** - to take the absolute value, use the |abs()| command. | + | * **Absolute Value** - to take the absolute value, use the ''abs()'' command. |
- | * **Exponents** - to write an exponent in Python you have to use a "$**$" rather than "^". For example, to write $A=5.43*10^4$ in code you would write: | + | <code> |
+ | Number=5 | ||
+ | AbsoluteNumber = abs(Number) | ||
+ | </code> | ||
+ | * **Exponents** - to write an exponent in Python you have to use a $**$ rather than ^. For example, to write $A=5.43*10^4$ in code you would write: | ||
<code> | <code> | ||
A=5.42*10**4 | A=5.42*10**4 | ||
</code> | </code> | ||
+ | * **Scientific Notation** - the code below shows how to use scientific notation in Python. | ||
<code> | <code> | ||
A=5.24E4 | A=5.24E4 | ||
</code> | </code> | ||
- | * **Square root** - to write a square root in Python, use the |sqrt()| command. | + | * **Square root** - to write a square root in Python, use the ''sqrt()'' command. |
<code> | <code> | ||
- | qwer=5.42*10**4 | + | qwer=sqrt(625) |
</code> | </code> | ||
- | * **Vectors** - In Python, you can define a variable as a vector using the "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. | + | * **Vectors** - In Python, you can define a variable as a vector using the ''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.** |
<code> | <code> | ||
position1=vec(3,4,5) | position1=vec(3,4,5) | ||
Line 53: | Line 103: | ||
testing=separation-4 #This will give an error | testing=separation-4 #This will give an error | ||
</code> | </code> | ||
- | * **Vector magnitude** - the "mag()" command will calculate the [[http://p3server.pa.msu.edu/coursewiki/doku.php?id=184_notes:math_review#Vector_Notation|magnitude of any vector]]. | + | * **Vector magnitude** - the ''mag()'' command will calculate the [[http://p3server.pa.msu.edu/coursewiki/doku.php?id=184_notes:math_review#Vector_Notation|magnitude of any vector]]. |
<code> | <code> | ||
vector1=vec(1,2,3) | vector1=vec(1,2,3) | ||
mag1=mag(vector1) #This will calculate sqrt(1^2+2^2+3^2) | mag1=mag(vector1) #This will calculate sqrt(1^2+2^2+3^2) | ||
</code> | </code> | ||
- | |||
- | ==== Graphing ==== | ||
- | |||
==== Tips for Coding ==== | ==== Tips for Coding ==== | ||
Line 68: | Line 115: | ||
B=(4+4)/2 | B=(4+4)/2 | ||
</code> | </code> | ||
- | * ** Watch upper/lower cases** - coding is case sensitive - meaning that lowercase and uppercase letters mean something different in coding. If I had two different variables named 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. | + | * ** Watch upper/lower cases** - coding is case sensitive - meaning that lowercase and uppercase letters mean something different in coding. If I had two different variables named ''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. |
- | * **Comments** - comments are informational statements that are not read or used by the program, but help you when analyzing the code. In Python, you can turn any line or part of a line in the code into a comment by putting a "#" or "##" in that line (see the examples below). You can use comments in several different ways: | + | * **Comments** - comments are informational statements that are not read or used by the program, but help you when analyzing the code. In Python, you can turn any line or part of a line in the code into a comment by putting a ''#'' or ''##'' in that line (see the examples below). You can use comments in several different ways: |
* **Notating units** - especially in long equations or calculations, keeping track of the units can help you evaluate whether your solution or equation is right. After any variable, you can add the units of that variable with a comment (see the example below). | * **Notating units** - especially in long equations or calculations, keeping track of the units can help you evaluate whether your solution or equation is right. After any variable, you can add the units of that variable with a comment (see the example below). | ||
* **Denoting what specific lines of code do** - before a chunk of code it's often helpful to write out what the purpose of those lines of code are in plain English. This is especially helpful when trying to use code that is quite long and complicated or if you’re trying to share code with a team member. This can also be helpful when working with others communicate any misunderstanding of the code or suggest potential changes. | * **Denoting what specific lines of code do** - before a chunk of code it's often helpful to write out what the purpose of those lines of code are in plain English. This is especially helpful when trying to use code that is quite long and complicated or if you’re trying to share code with a team member. This can also be helpful when working with others communicate any misunderstanding of the code or suggest potential changes. | ||
- | * **Fixing errors in your code** - if you are not sure which line is giving you trouble, instead of deleting the entire line, just comment it out. This way if you find that that line was not the problem later, you don’t have to rewrite it, you just have to uncomment it (uncommenting is done by deleting the #). 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. | + | * **Fixing errors in your code** - if you are not sure which line is giving you trouble, instead of deleting the entire line, just comment it out. This way if you find that that line was not the problem later, you don’t have to rewrite it, you just have to uncomment it (uncommenting is done by deleting the ''#''). 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. |
<code> | <code> | ||
## Set the radius (this line would be ignored by the program) | ## 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) | R=20 #cm (Everything in this line before the # would run in the program, everything after the # is ignored) | ||
</code> | </code> | ||
+ | * **Indentation errors** - these can be tricky sometimes. The best way to fix these is to [BACKSPACE] the faulty code up to the line above it and then pressing [ENTER] again to separate the lines and reset the indentation. This is not the only way to fix indentation errors. | ||
==== Math Review ==== | ==== Math Review ==== | ||
Check out this link for a **[[http://p3server.pa.msu.edu/coursewiki/doku.php?id=184_notes:math_review#Vector_Notation|useful math review]]**. | Check out this link for a **[[http://p3server.pa.msu.edu/coursewiki/doku.php?id=184_notes:math_review#Vector_Notation|useful math review]]**. |