09.04.2016 Views

www.ebook777.com

Make_Getting_Started_with_Processing_Second_Edition

Make_Getting_Started_with_Processing_Second_Edition

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

124 Getting Started with Processing<br />

<strong>www</strong>.<strong>ebook777.com</strong><br />

Make a Function<br />

In this section, we’ll draw an owl to explain the steps involved in<br />

making a function.<br />

Example 9-3: Draw the Owl<br />

First, we’ll draw the owl without using a function:<br />

void setup() {<br />

size(480, 120);<br />

}<br />

void draw() {<br />

background(176, 204, 226);<br />

translate(110, 110);<br />

stroke(138, 138, 125);<br />

strokeWeight(70);<br />

line(0, -35, 0, -65); // Body<br />

noStroke();<br />

fill(255);<br />

ellipse(-17.5, -65, 35, 35); // Left eye dome<br />

ellipse(17.5, -65, 35, 35); // Right eye dome<br />

arc(0, -65, 70, 70, 0, PI); // Chin<br />

fill(51, 51, 30);<br />

ellipse(-14, -65, 8, 8); // Left eye<br />

ellipse(14, -65, 8, 8); // Right eye<br />

quad(0, -58, 4, -51, 0, -44, -4, -51); // Beak<br />

}<br />

Notice that translate() is used to move the origin (0,0) to 110<br />

pixels over and 110 pixels down. Then the owl is drawn relative to<br />

(0,0), with its coordinates sometimes positive and negative as<br />

it’s centered around the new 0,0 point. See Figure 9-1.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!