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.

PROCESSING: CREATIVE CODING AND COMPUTATIONAL ART<br />

102<br />

int[] getXYMotion(){<br />

xpos+=xspeed;<br />

ypos+=yspeed;<br />

int[]xypos = new int[]{xpos, ypos};<br />

return xypos;<br />

}<br />

The function return type is now an int[] array, not a primitive int. The function creates<br />

an xypos array each time it’s called, <strong>and</strong> then returns the array. The other important aspect<br />

to this approach is knowing how to receive the array that’s passed back. When I was originally<br />

just returning an int back, it was simple, because it was the only value returned. But<br />

to really benefit from this approach, the rect() call needs to be changed from:<br />

to<br />

rect(getXMotion(), ypos, 10, 10);<br />

rect(getXYMotion()[0], getXYMotion()[1], 10, 10);<br />

Does this make sense? I know it looks pretty strange. Remember, arrays are indexed,<br />

beginning at [0]. When the array is passed back, it’s being received as a multivalue structure,<br />

with xpos at the 0 index <strong>and</strong> ypos at the 1 index. These values are retrieved using<br />

getXYMotion()[0] <strong>and</strong> getXYMotion()[1], respectively. If this is still not making sense,<br />

imagine replacing getXYMotion() with the array. To get the value in the first position of<br />

the array, you’d use arrayname[0]. To get the value in the second position, you’d use<br />

arrayname[1]. I hope that helps. You’ll be doing stuff like this in future chapters, so try not<br />

to freak out if this stuff is hurting your head—it’s supposed to in the beginning.<br />

The last thing I want to say about functions relates to a nasty habit many coders get into.<br />

Now don’t get any wrong ideas here. I’m talking about the use of magic numbers—what<br />

were you thinking of? Magic numbers are values we hard-code into a program that make<br />

the program difficult to change or customize. Using magic numbers is generally not the<br />

way to develop a modular system, but it can be a fast way of hacking out a solution without<br />

thinking of the big picture. In general, you’ll want to avoid them. Following are two<br />

simple examples—the first using magic numbers:<br />

//sketch using magic numbers:<br />

size(200, 200);<br />

background(255);<br />

strokeWeight(5);<br />

stroke(20);<br />

fill(100);<br />

rect (50, 50, 100, 100);<br />

The second example improves upon this sketch, using a parameterized function instead of<br />

the hard-coded values:<br />

// sketch using a nice parameterized function<br />

void setup(){<br />

size(200, 200);<br />

background(255);

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

Saved successfully!

Ooh no, something went wrong!