MIT 6.096: 
Introduction to Interactive Programming

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

  1. Write rules that will place the ball in position (10, 20).  Try other coordinates as well, including negative ones.
  2. Use the Horizontal rule for both rules.  What do you expect would happen?
  3. 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.
  4. 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.

  5. Rewrite your code so that the ball stays where you place it.
  6. Answer: return pos;

Implementing Velocity
  1. 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.)
  2. Use the horizontal rule for both rules.  What do you expect would happen?  Explain.
  3. Answer: You should get a (roughly, depending on timing differences between the two threads) 45 degree straight line.

  4. What happens if the horizontal and vertical rules have different effective velocities?

    Answer: The angle changes.
  5. Use the mouse to move the dot to a different position.  What happens?  Explain.
Implementing Acceleration
  1. 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;


  2. 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.)

  1. 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

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.

  1. Play around with the velocity and acceleration-control mode.  Play around with bounce and no-bounce modes too.
  2. Try the different position rules you wrote above (in particular, the static positioning, velocity, and acceleration)
  3. Write code that will draw a parabola.
  4. 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.

 

Things to Think About

 

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: