CIT020 Index > Mathematics

Mathematics

In section 13.8 of the book, the author discusses polar coordinates, where you specify a point by its radius and angle, rather than its x and y coordinates.

In this assignment, you will draw a polar equation, where the radius is a function of the angle. (You do this all the time in Cartesian coordinates, where y is a function of x.) The polar equation you will be drawing will have this form:

r = sin(a·θ) + cos(b·θ)

Your sketch will be named polar, and it will set up a sketch window that is 300 by 300. Make global float variables a, b, and theta. You will also need a couple of other global variables to store the “previously drawn” x and y coordinates; you may name them as you wish. Except: these variables are not the same as pmouseX and pmouseY—they have nothing to do with the mouse position—so do not use those names!

Here are the functions you will need to write for your sketch.

The initialize() function

Your initialize() function will set the background color to white, set theta to zero, and calculate new random values for a and b. Variable a will range from 0 to 5 in steps of one-fourth (0.25); variable b will range from 0 to 5 in steps of one-third (0.3333...). (See hint)

(Optional: you may set a random stroke color for the drawing.)

The draw() function

Your draw() function will:

  1. Calculate r, given a, b, and theta, using the equation:
    r = sin(a·θ) + cos(b·θ).
  2. Transform r to (x, y) coordinates.
  3. Multiply both x and y by 50 to make the lines longer.
  4. If theta is not zero, draw a line from (150 + previous x, 150 + previous y) to (150 + x, 150 + y).
  5. The “previous x” and “previous y” variables will now become x and y.
  6. Add 3° (π/60 radians) to theta

The mousePressed() function

Your mousePressed() function will simply call initialize() to start a new drawing.

The setup() function

Your setup() function will set the window size to 300 by 300 and call initialize() to start the first drawing. Your finished sketch should work like the following sketch. Click the mouse to start a new drawing.

This browser does not have a Java Plug-in.
Get the latest Java Plug-in here.

 

When You Finish

Zip up the sketch into zip file named polar.zip and upload it.

Hint

When I want a variable ranging from 0 to 5 in steps of one-fourth, I mean it can have values of 0, 0.25, 0.5, 0.75, 1, 1.25, ... 4.75, 5.00, but not any values in between, like 1.6 or 3.38. That means you can not use random(0, 5). This table may help you figure out how to do it, though:

Random
integer
Divided
by 4
Produces
float value
00 / 4.00
11 / 4.00.25
22 / 4.00.50
33 / 4.00.75
44 / 4.01.00
55 / 4.01.25
...
1919 / 4.04.75
2020 / 4.05.00

And, of course, you can use similar logic to get random numbers in steps of one-third.