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.

part of the article as well. My suggestion when creating strings is to use the shortcut<br />

approach. If for some reason you do need to compare the values of two strings created<br />

using the object-oriented approach (using the new keyword), you can use Java’s String<br />

method equals(). Here’s an example:<br />

Conversion<br />

String s1 = new String("hello");<br />

String s2 = "hello";<br />

println("s1 == s2 is " + (s1 == s2));<br />

println("s1.equals(s2) is " + s1.equals(s2));<br />

String s3 = new String("goodbye");<br />

String s4 = new String("goodbye");<br />

println("s3 == s4 is " + (s3 == s4));<br />

println("s3.equals(s4) is " + s3.equals(s4));<br />

The Conversion section includes ten entries, each of which is a utility function for converting<br />

between one data type <strong>and</strong> another. Since variables hold values of a specific type,<br />

there are times when, for example, you may need to use a value of type float to pass into<br />

a function that requires an integer value. <strong>Processing</strong> has a way of doing this conversion,<br />

called type casting. For example, to convert a float variable named gpa to an integer, you<br />

just need to write int(gpa). One issue to consider when doing conversion is truncation. In<br />

my example, the gpa float value will be truncated, or shortened, when it is converted. For<br />

example, if gpa equals 3.97, after converting it using int(gpa), the value returned will be<br />

3 (not 4). Instead of rounding the value, it simply snips off all the values to the right of the<br />

decimal point. Thus, a simple data conversion mistake like this could keep you out of grad<br />

school. There are of course other <strong>and</strong> better ways to solve this last example (involving logical<br />

value rounding), which I’ll cover later in my discussion of the Math section.<br />

There’s one final cautionary note about using <strong>Processing</strong>’s int() <strong>and</strong> float() conversion<br />

functions. Currently, there is a reported <strong>and</strong> unresolved bug about their use<br />

(http://dev.processing.org/bugs/show_bug.cgi?id=4). Thus, if these functions don’t<br />

seem to be working, it’s recommended that you use Java’s alternative syntax. Here’s an<br />

example:<br />

float temp = 98.64783;<br />

//<strong>Processing</strong> conversion syntax<br />

int pTemp = int(temp);<br />

println(pTemp);<br />

//Java conversion syntax<br />

int jTemp = (int)temp;<br />

println(jTemp);<br />

PROCESSING LANGUAGE API<br />

681<br />

A

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

Saved successfully!

Ooh no, something went wrong!