09.04.2016 Views

www.ebook777.com

Make_Getting_Started_with_Processing_Second_Edition

Make_Getting_Started_with_Processing_Second_Edition

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.

170 Getting Started with Processing<br />

<strong>www</strong>.<strong>ebook777.com</strong><br />

}<br />

noFill();<br />

stroke(204, 51, 0);<br />

beginShape();<br />

for (int i = 0; i < homeRuns.length; i++) {<br />

float x = map(i, 0, homeRuns.length-1, 20, 460);<br />

float y = map(homeRuns[i], 0, 60, 100, 20);<br />

vertex(x, y);<br />

}<br />

endShape();<br />

This example is so minimal that it’s not necessary to store this<br />

data in arrays, but the idea can be applied to more complex<br />

examples you might want to make in the future. In addition, you<br />

can see how this example could be enhanced with more information—for<br />

instance, information on the vertical axis to state<br />

the number of home runs and on the horizontal to define the<br />

year.<br />

Example 12-3: 29,740 Cities<br />

To get a better idea about the potential of working with data<br />

tables, the next example uses a larger data set and introduces a<br />

convenient feature. This table data is different because the first<br />

row, the first line in the file, is a header. The header defines a<br />

label for each column to clarify the context. This is the first five<br />

lines of our new data file called cities.csv:<br />

zip,state,city,lat,lng<br />

35004,AL,Acmar,33.584132,-86.51557<br />

35005,AL,Adamsville,33.588437,-86.959727<br />

35006,AL,Adger,33.434277,-87.167455<br />

35007,AL,Keystone,33.236868,-86.812861<br />

The header makes it easier to read the code—for example, the<br />

second line of the file states the zip code of Acmar, Alabama, is<br />

35004 and defines the latitude of the city as 33.584132 and the<br />

longitude as -86.51557. In total, the file is 29,741 lines long and it<br />

defines the location and zip codes of 29,740 cities in the United<br />

States.<br />

The next example loads this data within setup() and then draws<br />

it to the screen in a for loop within draw(). The setXY() function<br />

converts the latitude and longitude data from the file into a<br />

point on the screen:

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

Saved successfully!

Ooh no, something went wrong!