04.04.2013 Views

Processing: Creative Coding and Computational Art

Processing: Creative Coding and Computational Art

Processing: Creative Coding and Computational Art

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.

createRect(50, 50, 100, 100, 20, 5, 100);<br />

}<br />

// parameterized function<br />

void createRect(int xpos, int ypos, int wdth,<br />

int ht, int strokeCol,<br />

int strokeWt, int fillCol) {<br />

stroke(strokeCol);<br />

strokeWeight(strokeWt);<br />

fill(fillCol);<br />

rect (xpos, ypos, wdth, ht);<br />

}<br />

Both sketches should output the same centered rectangle, as shown in Figure 3-8.<br />

Figure 3-8. Rectangle generated initially using magic<br />

numbers <strong>and</strong> then with a function<br />

Do you see how the second approach will allow you to create any rectangle, while the first<br />

only creates one? The more modular <strong>and</strong> general you can make your code, the greater the<br />

chance you can reuse it or easily exp<strong>and</strong> it. However, this type of approach takes more<br />

planning <strong>and</strong> analysis, <strong>and</strong> sometimes you just want to get coding. Because this book is<br />

about creative coding, I think it is perfectly acceptable to sometimes hack out poorly<br />

structured, “ugly” code, especially if it allows you to express yourself <strong>and</strong> find new solutions<br />

more effectively. I suspect if you’re like me, you’ll eventually discover things that you<br />

want to build out into larger projects, <strong>and</strong> that’s where the rules <strong>and</strong> best practices stuff<br />

becomes important. So basically you have permission from me to write nasty code when<br />

necessary (to remain creatively engaged), but don’t rat me out to your teacher or boss<br />

when they give you a hard time about it.<br />

CODE GRAMMAR 101<br />

103<br />

3

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

Saved successfully!

Ooh no, something went wrong!