Spirograph Laboratory project
This lab will allow you to get some practice
writing simple statements in Java. The Spirograph is a little dot that
leaves a red line behind it as it moves. You will be writing simple instructions
that will control the Spirograph's motion.
How to Use the Spirograph
Instructions on how to start and use the Spirograph problem set are available
here.
Things to Try
Begin by putting the Spirograph in position-control mode. In this
mode, the return value of your code will specify the ball's position.
Static Positioning
-
Write rules that will place the ball in position (10, 20). Try other
coordinates as well, including negative ones.
-
Use the Horizontal rule for both rules. What do you expect would
happen?
-
Use the Horizontal and Vertical rules separately again. Use the mouse to
manually move the ball to another position (see Advanced Environment Options).
Do this several times. What happens? Explain.
Answer: The ball goes back to (10,20). Sometimes
the ball moves to the (10,y) or (x,10) first, before going to (10,20).
This is because the horizontal and vertical positions are not necessarily
updated simultaneously.
-
Rewrite your code so that the ball stays where you place it.
Answer: return pos;
Implementing Velocity
-
Write rules to make the ball move horizontally from left to right.
Can you control how fast the ball moves (i.e., velocity) by changing your
code? (Don't worry that the "Vel" display on the Spirograph says
"(0.0,0.0)". "Vel" displays the balls internal/automatic(?) velocity,
and is not applicable in position-control mode. (Note: Maybe we
should just disable this display when in position control mode.)
-
Use the horizontal rule for both rules. What do you expect would
happen? Explain.
Answer: You should get a (roughly, depending on timing differences
between the two threads) 45 degree straight line.
-
What happens if the horizontal and vertical rules have different effective
velocities?
Answer: The angle changes.
-
Use the mouse to move the dot to a different position. What happens?
Explain.
Implementing Acceleration
-
Now that you can implement velocity using position controls, implement
acceleration. Note that you cannot use myVel and otherVel, since
these are always 0 in position-control mode. (Hint: use fields.)
Write your code so you can change the initial velocity and acceleration
by changing the code.
Answer: fields: double effVel = 0;
double effAcc = 1;
method:
newpos = pos + effVel;
effVel += effAcc;
return newpos;
-
Make the dot go in a parabolic path.
Wraparound
Try running the code you have so far in wrap-around mode and no-wrap-around
mode, and observe its behavior. (Note: we assume bouncing is disabled.
Maybe we can even force it to be disabled in position control mode.)
-
Modify your code to make it emulate the behavior of wrap-around mode while
using no-wrap-around mode.
Answer: newPos = whatever;
if (newPos > maxPos)
newPos = -maxPos;
return newPos
Other cool stuff
-
Implement a function plotter. Write code to plot the following:
-
y = x^2;
-
y = sin( x );
-
y = 1/x; (they have to check for zero!)
Using Velocity and Acceleration Controls
Although you can implement velocity and acceleration using position
controls alone, Spirograph is capable of doing this for you, and makes
it easier for you to play around with the effects of different code.
In the case of acceleration controls, you can think of the ball as a robot
with independent horizontal and vertical motors, and your rules as the
controls for its motors.
-
Play around with the velocity and acceleration-control mode. Play
around with bounce and no-bounce modes too.
-
Try the different position rules you wrote above (in particular, the static
positioning, velocity, and acceleration)
-
Write code that will draw a parabola.
-
Challenge: Write code that will draw a circle (given an appropriate
initial position and velocity). (Hint: remember a = v^2/r
from Physics.)
(Old Stuff)
Before you leave lab today, make sure that you've tried writing code
that uses the following statements.
-
return 0;
-
a simple return number; statement
-
an if statement
-
some statements using simple arithmatic
-
a call to the Math library (you can check the on-line API here.)
Things to Think About
-
Start the spirograph with no acceleration and some velocity and watch the
pattern it makes. Now restart the spirograph with a acceleration of -1
in the Y direction and 0 in the X direction, and give it some initial velocity.
Finally, restart the spirograph and give both the X and Y directions and
acceleration of 1, as well as giving the ball some initial velocity. Can
you explain why the three different sets of accelerations produce their
respective patterns?
-
Switch the spirograph to circular mode (click on the top button in the
Advanced Environement Options menu). Start the ball from the center of
the circle with no initial velocity but with any acceleration you want.
Can you explain why the ball behaves the way it does?
-
Write some code which returns larger and larger numbers (ie: the first
time it returns 1, then 2, then 3 etc,) and start the ball from rest using
this acceleration. Can you explain why the ball behaves the way it does?
Post Lab
Pick a few of your code samples and turn
them in, along with a brief description of how the Spirograph behaved when
you ran them and why you think it acted the way it did. In addition, quickly
discuss your thoughts on some of the following:
-
How long did this lab take you?
-
Did you work with other students? If so, how helpful was it?
-
Did you have problems writing code for this lab?
-
Did you have problems running/using this lab?