0% found this document useful (0 votes)
3K views35 pages

09 Functions 20130531

The document discusses functions in processing and how they can be used to modularize and reuse code. It shows examples of increasingly complex functions that generate randomized shapes on a canvas. Functions are defined to draw rectangles, circles, and combinations of shapes in loops. Parameters are added to the functions to specify properties like location, number of iterations, and size ranges randomly. This demonstrates how functions allow breaking a program into logical units and repeating code blocks.

Uploaded by

Gene Kao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3K views35 pages

09 Functions 20130531

The document discusses functions in processing and how they can be used to modularize and reuse code. It shows examples of increasingly complex functions that generate randomized shapes on a canvas. Functions are defined to draw rectangles, circles, and combinations of shapes in loops. Parameters are added to the functions to specify properties like location, number of iterations, and size ranges randomly. This demonstrates how functions allow breaking a program into logical units and repeating code blocks.

Uploaded by

Gene Kao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

Processing

Functions
()

09

Structure 3: Functions

Function
Function
Function

()
()
()

void
defun
procedure
subroutine
sub
function

Processing
LISP
PASCAL
FORTRAN
BASIC

1.
2.
3.
4.

()

()

void setup() {
size(400,400);
background(255);
rectMode(CENTER);
ellipseMode(CENTER);
smooth();
frameRate(10);
}

void draw() {
rect_circle(random(width),random(height));
}
void rect_circle (float x, float y) {
fill(0);
rect(x,y,100,100);
fill(255);
ellipse(x,y,100,100);
}

void rect_circle (float x, float y) {


fill(0);
rect(x,y,100,100);
fill(255);
ellipse(x,y,100,100);
}

void setup() {
size(400,400);
background(255);
rectMode(CENTER);
ellipseMode(CENTER);
smooth();
frameRate(5);
}
void draw() {
rect_circle(random(width),random(height));
}
void rect_circle (float x, float y) {
fill(0);
rect(x,y,100,100);
fill(255);
ellipse(x,y,100,100);
}

void rect_circle (float x, float y) {


float w = random(60);
fill(0);
rect(x,y,w,w);
fill(255);
ellipse(x,y,w,w);
}

void setup() {
size(400,400);
background(255);
rectMode(CENTER);
ellipseMode(CENTER);
smooth();
frameRate(5);
}
void draw() {
rect_circle(random(width),random(height));
}
void rect_circle (float x, float y) {
float w = random(60);
fill(0);
rect(x,y,w,w);
fill(255);
ellipse(x,y,w,w);
}

void rect_circle (float x, float y) {


float w = random(60);
fill(random(255),random(255),random(255));
rect(x,y,w,w);
fill(255);
ellipse(x,y,w,w);
}

void setup() {
size(400,400);
background(255);
rectMode(CENTER);
ellipseMode(CENTER);
smooth();
frameRate(5);
}
void draw() {
rect_circle(random(width),random(height));
}
void rect_circle (float x, float y) {
float w = random(60);
fill(random(255),random(255),random(255));
rect(x,y,w,w);
fill(255);
ellipse(x,y,w,w);
}

void setup() {
size(400,400);
background(255);
rectMode(CENTER);
ellipseMode(CENTER);
smooth();
frameRate(20);
}
void draw() {
rect_circle(random(width),random(height));
}
void rect_circle (float x, float y) {
float w = random(20,60);
fill(random(255),random(255),random(255));
rect(x,y,w,w);
fill(255);
ellipse(x,y,w,w);
}

void rect_circle (float z) {


void setup() {
if (z > 1) {rect_circle_1(random(width), random(height));}
size(400,400);
else {rect_circle_2(random(width), random(height));}
background(255);
}
rectMode(CENTER);
ellipseMode(CENTER);
void rect_circle_1 (float x, float y) {
smooth();
float w = random(60);
frameRate(5);
fill(random(255),random(255),random(255));
}
rect(x,y,w,w);
fill(255);
void draw() {
ellipse(x,y,w,w);
rect_circle(random(2));
}
}
void rect_circle_2 (float x, float y) {
float w = random(60);
fill(255);
rect(x,y,w,w);
fill(random(255),random(255),random(255));
ellipse(x,y,w,w);
}

void setup() {
size(400,400);
background(255);
}

void draw() {
fill(0);
rect(50,50,100,100);
fill(255);
rect(50,50,80,80);
fill(0);
rect(50,50,60,60);
fill(255);
rect(50,50,40,40);
fill(0);
rect(50,50,20,20);
}

void setup() {
size(400,400);
background(255);
rectMode(CENTER);
}
void draw() {
fill(0);
rect(50,50,100,100);
fill(255);
rect(50,50,80,80);
fill(0);
rect(50,50,60,60);
fill(255);
rect(50,50,40,40);
fill(0);
rect(50,50,20,20);
}

void setup() {
size(400,400);
background(255);
rectMode(CENTER);
}

void setup() {
size(400,400);
background(255);
rectMode(CENTER);
}

void draw() {
five_rects();
}

void draw() {
five_rects();
}

void five_rects() {
fill(0);
rect(50,50,100,100);
fill(255);
rect(50,50,80,80);
fill(0);
rect(50,50,60,60);
fill(255);
rect(50,50,40,40);
fill(0);
rect(50,50,20,20);
}

void five_rects() {
for (int i = 0; i < 5; i += 1) {
if ((i % 2) == 1) {fill(255);} else {fill(0);}
rect(50,50,100-20*i,100-20*i);
}
}

void draw() {
five_rects(random(width),random(height));
}

void five_rects (float x, float y) {


for (int i = 0; i < 5; i += 1) {
if ((i % 2) == 1) {fill(255);} else {fill(0);}
rect(x,y,100-20*i,100-20*i);
}
}

void setup() {
size(400,400);
background(255);
rectMode(CENTER);
}
void draw() {
five_rects(random(width),random(height));
}
void five_rects (float x, float y) {
for (int i = 0; i < 5; i += 1) {
if ((i % 2) == 1) {fill(255);} else {fill(0);}
rect(x,y,100-20*i,100-20*i);
}
}

void setup() {
size(400,400);
background(255);
//rectMode(CENTER);
}
void draw() {
five_rects(random(width),random(height));
}
void five_rects (float x, float y) {
for (int i = 0; i < 5; i += 1) {
if ((i % 2) == 1) {fill(255);} else {fill(0);}
rect(x,y,100-20*i,100-20*i);
}
}

void setup() {
size(400,400);
background(255);
//rectMode(CENTER);
frameRate(6);
}
void draw() {
five_rects(random(width),random(height));
}
void five_rects (float x, float y) {
if (random(1 ,3) >= 2) {rectMode(CENTER);}
else {rectMode(CORNER);}
for (int i = 0; i < 5; i += 1) {
if ((i % 2) == 1) {fill(255);} else {fill(0);}
rect(x,y,100-20*i,100-20*i);
}
}

void setup() {
size(400,400);
background(255);
smooth();
frameRate(5);
}
void draw() {
circles(random(width),random(height));
}
void circles (float x, float y) {
for (int i = 0; i < 5; i += 1) {
if ((i % 2) == 1) {fill(255);} else {fill(0);}
ellipse(x,y,100-20*i,100-20*i);
}
}

void setup() {
size(400,400);
background(255);
smooth();
frameRate(5);
}
void draw() {
circles(random(width),random(height));
}
void circles (float x, float y) {
for (int i = 0; i < 5; i += 1) {
fill(random(255),random(255),random(255));
ellipse(x,y,100-20*i,100-20*i);
}
}

void draw() {
int i;
circles(random(width),random(height),int(random(1,5)));
}
void circles (float x, float y, int step) {
for (int i = 0; i < step; i += 1) {
fill(random(255),random(255),random(255));
ellipse(x,y,100-20*i,100-20*i);
}
}

void setup() {
size(400,400);
background(255);
smooth();
frameRate(5);
}
void draw() {
int i;
circles(random(width),random(height),
int(random(1,5)));
}
void circles (float x, float y, int step) {
for (int i = 0; i < step; i += 1) {
fill(random(255),random(255),random(255));
ellipse(x,y,100-20*i,100-20*i);
}
}

void draw() {
int i;
circles(random(width),random(height),
int(random(1,10)),int(random(100,200)));
}
void circles (float x, float y, int step, int d) {
for (int i = 0; i < step; i += 1) {
fill(random(255),random(255),random(255));
ellipse(x,y,d-20*i,d-20*i);
}
}

void setup() {
size(400,400);
background(255);
smooth();
frameRate(5);
}
void draw() {
int i;
circles(random(width),random(height),
int(random(1,10)),int(random(100,200)));
}
void circles (float x, float y, int step, int d) {
for (int i = 0; i < step; i += 1) {
fill(random(255),random(255),random(255));
ellipse(x,y,d-20*i,d-20*i);
}
}

void setup() {
size(400,400);
background(255);
smooth();
frameRate(5);
}
void draw() {
int i;
circles(random(width),random(height),
int(random(1,10)),int(random(100,200)));
}
void circles (float x, float y, int step, int d) {
for (int i = 0; i < step; i += 1) {
fill(random(255),random(255),random(255),125);
ellipse(x,y,d-20*i,d-20*i);
}
}

void setup() {
size(400,400);
background(255);
smooth();
noStroke();
}
void draw() {
int i;
frameRate(5);
circles(random(width),random(height),
int(random(1,10)),int(random(100,200)));
}
void circles (float x, float y, int step, int d) {
for (int i = 0; i < step; i += 1) {
fill(random(255),random(255),random(255),150);
ellipse(x,y,d-20*i,d-20*i);
}}

void setup() {
size(400,400);
background(255);
smooth();
noStroke();
frameRate(5);
}
void draw() {
int i;
many_rects(random(width),random(height),
int(random(1,10)),int(random(100,200)));
}
void many_rects (float x, float y, int step, int d) {
for (int i = 0; i < step; i += 1) {
fill(random(255),random(255),random(255),255);
rect(x,y,d-20*i,d-20*i);
}
}

void setup() {
size(400,400);
background(255);
smooth();
noStroke();
rectMode(CENTER);
frameRate(5);
}
void draw() {
int i;
many_rects(random(width),random(height),
int(random(1,10)),int(random(100,200)));
}

void many_rects (float x, float y, int step, int d) {


for (int i = 0; i < step; i += 1) {
fill(random(255),random(255),random(255),255);
rect(x,y,d-20*i,d-20*i);
}
}

void setup() {
size(400,400);
background(255);
smooth();
noStroke();
rectMode(CENTER);
frameRate(5);
}
void draw() {
int i;
many_rects(random(width),random(height),
int(random(1,10)),int(random(100,200)));
}
void many_rects (float x, float y, int step, int d) {
for (int i = 0; i < step; i += 1) {
fill(random(255),random(255),random(255),
random(255));
rect(x,y,d-20*i,d-20*i);
}
}

Ch. 21
05/31/2013

You might also like