Drawing custom shapes
Squares and circles might be boring after using them for a while. Luckily for you, Processing has some functions to draw custom shapes. We'll take a look at how you can write functions for drawing stars and flowers.
How to do it...
We'll start by writing the code for the setup()
function. I've used the frameRate()
function to make the sketch run at one frame per second.
void setup() { size( 640, 480 ); smooth(); frameRate( 1 ); }
The next thing we'll do is write a function to draw a star. The function takes three parameters: an integer for the number of spikes on the star, and two float variables for the inner and outer radius we'll use to calculate the vertices.
void star( int numSpikes, float innerRadius, float outerRadius ) { int numVertices = numSpikes * 2; float angleStep = TWO_PI / numVertices; beginShape(); for ( int i = 0; i < numVertices; i++ ) { float x, y; if ( i % 2 == 0 ) { x = cos( angleStep * i ) * outerRadius; y = sin...