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 />

662<br />

If you haven’t figured it out by now, here’s how you mutate between the four forms:<br />

1. Press the P key to convert from a cube to a pyramid.<br />

2. Press <strong>and</strong> hold the up arrow key to convert from a pyramid to a cone.<br />

3. Press the P key again to convert from a cone to a cylinder.<br />

I was surprised by how easy it was to convert between these four forms. Prior to developing<br />

this sketch, I had considered them (without really thinking much about it) as fundamental,<br />

irreducible primitives, like the primary colors. I even managed to earn two art<br />

degrees never realizing that a pyramid was just a cube with one of its faces collapsed to a<br />

point.<br />

The sketch should be fairly self-explanatory. However, there is one subtle point worth discussing.<br />

When I first generated this sketch, it had an annoying bug. As the point count<br />

changed, sometimes the geometry would get screwed up. I found a clever but overly complicated<br />

hack to solve the problem. However, when I was cleaning up the code, I realized<br />

that the problem was actually very simple to fix, <strong>and</strong> my complicated hack was a major<br />

waste of time (sort of a pattern in my life). The problem related to the line<br />

angle+=360/pts;<br />

which I resolved by changing it to<br />

angle+=360.0/pts;<br />

You have to look closely to even notice the difference between these two lines. Adding the<br />

.0 after the 360 solved the problem. Can you tell why? Originally, I thought the problem<br />

related to needing the point count to be a factor of 360, which does solve the problem.<br />

However, that was more of a symptom, not really the cause. The real problem was that I<br />

was using two int values in the 360/pts calculation, which not surprisingly produced an<br />

int; however, I needed a float value. By adding the .0 after the 360, the value was treated<br />

as type float. Operations involving an int <strong>and</strong> a float will return a float. Thus, my angle<br />

calculations, requiring float values, were getting rounded to int values, <strong>and</strong> I was slowly<br />

losing accuracy, which screwed up the geometry. This may seem like an obscure point, but<br />

it is a pretty common error in programming <strong>and</strong> can be a nasty bug to track down.<br />

Sometimes rounding errors don’t show up for many iterations, as long as the error is small<br />

enough. I recommend blaming the computer for these kinds of problems.<br />

Toroids<br />

Building upon the last few examples, it’s possible to put some of these ideas together to<br />

build slightly more complex forms, such as toroids <strong>and</strong> related forms (shown in Figures 13-22<br />

through 13-26). Toroids are ring forms (think inner tubes, bagels, or doughnuts). Toroids<br />

also have an interesting relationship to spheres <strong>and</strong> helixes, which you’ll see in the following<br />

example.<br />

The algorithm for a regular toroid, where the cross-section shape is a circle <strong>and</strong> the toroid<br />

radius is constant, might be as follows:

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

Saved successfully!

Ooh no, something went wrong!