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.

This is the same drawTable() function as in the last example, only many of the values in<br />

the main algorithm have been converted into parameters. Parameters are the variables,<br />

including their type declaration, that are placed between the parentheses of a function<br />

head when a function is defined. When you call or invoke the function, the values you pass<br />

to the function parameters are referred to as arguments. This can be confusing to new<br />

coders. The parameters define the number <strong>and</strong> type of arguments the function requires to<br />

be called. Another term you may come across is the function signature, which is just the<br />

name of the function, its parameters, <strong>and</strong> the return type. The return type relates to what,<br />

if any, value the function returns. A function that doesn’t return any value has a void<br />

return type. You always need to specify either a return type or void when you define a<br />

function. That’s why both the setup() <strong>and</strong> drawTable() functions are prefaced by the<br />

void keyword. Functions that return a specific value need to be prefaced by the data type<br />

of that value. For example, to return an int, a function should be prefaced with int. The<br />

same rule holds true for any data type that a function returns. Functions may return any<br />

legal data type.<br />

The parameters in the new, improved drawTable() function open up a ton of cool possibilities,<br />

a few of which you can see in the included figures. Try changing any of the following<br />

argument values to see what happens: cols, rows, xPadding, yPadding, r<strong>and</strong>omFactor,<br />

colorMin, strokeWtMax, <strong>and</strong> overDraw. You can change these values at the top of the<br />

setup() function, before the drawTable() function call is made. Really take the time to<br />

experiment with this sketch. It covers a lot of stuff you’ll see again <strong>and</strong> again. I also think<br />

you’ll be surprised at the range of output you can get with relatively few lines of code<br />

(although I realize that this might look like a lot of code to some of you).<br />

In the next section, you’ll look at the vertex() function, along with the companion<br />

beginShape() <strong>and</strong> endShape() functions. Using the vertex() function opens up even<br />

more exciting possibilities.<br />

Vertex functions<br />

A vertex is just another word for a point. However, what separates the vertex() function<br />

from the point() function is that the vertex() function utilizes the companion<br />

beginShape() <strong>and</strong> endShape() functions, allowing the vertices to be connected into lines,<br />

curves, 2D shapes, <strong>and</strong> even 3D forms. Since coordinate locations in a Cartesian system<br />

require a coordinate component for each axis, it makes sense that any form plotted on the<br />

computer would be reducible to a series of point locations, or vertices. The beginShape()<br />

function takes a mode argument that controls how the vertices are joined. The seven<br />

mode arguments are POINTS, LINES, TRIANGLES, TRIANGLE_STRIP, TRIANGLE_FAN, QUADS,<br />

<strong>and</strong> QUAD_STRIP. (Please note that POINTS mode is no longer documented in the official<br />

language API, but continues to work as of this writing.) If no option is specified, the line<br />

strip mode is used, which can be closed to form a polygon. This chapter will only look at<br />

how to use these modes to generate points, lines, open line strips, <strong>and</strong> closed line strips<br />

(polygons).<br />

LINES<br />

209<br />

6

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

Saved successfully!

Ooh no, something went wrong!