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

752<br />

Pythagorean theorem<br />

I suspect most of you remember something about the Pythagorean theorem. It is a simple<br />

equation for figuring the lengths of sides of a right triangle. The side opposite the right<br />

angle is the hypotenuse, which we commonly refer to as c, <strong>and</strong> the other sides are labeled<br />

a <strong>and</strong> b. The way the sides relate is as follows:<br />

Although the Pythagorean theorem is relatively simple, it is crucially important to our<br />

work in computer graphics, so it’s worth memorizing.<br />

Distance formula<br />

The distance formula relates directly to the Pythagorean theorem, <strong>and</strong> we use it to find<br />

the length of any line on the Cartesian coordinate system. Finding the length of vertical<br />

<strong>and</strong> horizontal lines is easy, as their distance is just the difference between either the<br />

x (horizontal line) or y (vertical line) components of the two points at either end of<br />

the line (x 2 – x1 or y 2 – y1). Diagonal lines are harder to measure <strong>and</strong> require the distance<br />

formula. The distance formula is the following:<br />

x 2 <strong>and</strong> x 1 are the two x components of the two points at either end of the line you’re trying<br />

to measure, <strong>and</strong> y 2 <strong>and</strong> y 1 are obviously the y components. The trick is to put the line in a<br />

form you can easily solve for—<strong>and</strong> that is as part of a right triangle (as shown in Figure B-1).<br />

Make the line you’re attempting to measure the hypotenuse of a right triangle, with one of<br />

its legs parallel to the x-axis <strong>and</strong> the other leg parallel to the y-axis. Now, if you know the<br />

distance of the two new sides you added to the triangle, you can easily solve for c, the<br />

hypotenuse. The distances of a <strong>and</strong> b respectively are simply the values x 2 – x 1 <strong>and</strong> y 2 – y 1 .<br />

Thus, just stick these expressions in the hypotenuse theorem, solving for c, <strong>and</strong> you get the<br />

distance formula.<br />

Area of a triangle<br />

The area of a triangle equals one half times the length of a base times the corresponding<br />

height:<br />

Area of a rectangle<br />

The area of a rectangle equals its length times its width:

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

Saved successfully!

Ooh no, something went wrong!