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!
mousePressed() function
Your mousePressed() 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.)
draw() function
Your draw() function will:
r, given
a, b, and theta, using the equation:r to (x, y) coordinates.theta is not zero, draw a line from
(150 + previous x, 150 + previous y)
to
(150 + x, 150 + y).thetasetup() function
Your setup() function will set the window size to 300 by 300 and
call mousePressed() to start the first drawing. Your finished
sketch should work like the following sketch. Click the mouse to start a new
drawing.
Zip up the sketch into zip file named
polar.zip and upload it.
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 |
|---|---|---|
| 0 | 0 / 4.0 | 0 |
| 1 | 1 / 4.0 | 0.25 |
| 2 | 2 / 4.0 | 0.50 |
| 3 | 3 / 4.0 | 0.75 |
| 4 | 4 / 4.0 | 1.00 |
| 5 | 5 / 4.0 | 1.25 |
| ... | ||
| 19 | 19 / 4.0 | 4.75 |
| 20 | 20 / 4.0 | 5.00 |
And, of course, you can use similar logic to get random numbers in steps of one-third.