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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

PROCESSING: CREATIVE CODING AND COMPUTATIONAL ART<br />

32<br />

learning a programming language is much easier than learning a verbal or natural language.<br />

Now let’s tackle some of the new terms I just threw at you <strong>and</strong> look at just a little<br />

processing code.<br />

First, what does it mean to “structure” your code?<br />

Here is a simple <strong>Processing</strong> program that draws a black rectangle on a white background;<br />

it’s not too ground-breaking, but it is a nice rectangle (see Figure 2-1 for an image of the<br />

rectangle that the code produces).<br />

size(400, 400);<br />

background(255);<br />

noStroke();<br />

fill(0);<br />

rect(width/4, height/4, width/2, height/2);<br />

Figure 2-1. Rectangle created using an<br />

unstructured coding approach<br />

A program this simple wouldn’t necessitate structuring your code any further. So, at this<br />

point, if all you need to make are single squares, you’re done. Congratulations! However,<br />

imagine instead of having 5 lines of code, you had 50, or 500, or 5,000? It becomes very<br />

difficult to underst<strong>and</strong> <strong>and</strong> maintain a program when it reaches a certain scale. There are<br />

ways of structuring your code, kind of like using paragraphs to organize a chapter, that<br />

make a large program more manageable. In addition, there are structural ways of simplifying<br />

your program, reducing redundant lines of code, <strong>and</strong> ultimately increasing your coding<br />

efficiency <strong>and</strong> possibly the program’s performance (these are all good things).<br />

Function-based (procedural) vs. object-oriented structure<br />

There are a number of ways of structuring your code—I’ll discuss two ways it’s done in<br />

<strong>Processing</strong>. The first way uses functions (also referred to as procedures, or subroutines—<br />

different words essentially describing the same technique). Functions are reusable blocks

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

Saved successfully!

Ooh no, something went wrong!