06.01.2013 Views

Learning Processing: A Beginner's Guide to Programming Images ...

Learning Processing: A Beginner's Guide to Programming Images ...

Learning Processing: A Beginner's Guide to Programming Images ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

202 <strong>Learning</strong> <strong>Processing</strong><br />

And most recently, in Chapter 10, we tested intersection using the Pythagorean Th eorem.<br />

float d = dist(x1,x2,y1,y2);<br />

Th ese are just a few examples we have seen so far and as you get more and more advanced, you may even<br />

fi nd yourself online, late at night, googling “ Sinusoidal Spiral Inverse Curve. ” For now, we will start with a<br />

selection of useful mathematical <strong>to</strong>pics.<br />

13.2<br />

Modulus<br />

We begin with a discussion of the modulo opera<strong>to</strong>r , written as a percent sign, in <strong>Processing</strong>. Modulus is a<br />

very simple concept (one that you learned without referring <strong>to</strong> it by name when you fi rst studied division)<br />

that is incredibly useful for keeping a number within a certain boundary (a shape on the screen, an index<br />

value within the range of an array, etc.) Th e modulo opera<strong>to</strong>r calculates the remainder when one number<br />

is divided by another. It works with both integers and fl oats.<br />

20 divided by 6 equals 3 remainder 2. (In other words: 6*3 � 2 � 18 � 2 � 20.)<br />

therefore:<br />

20 modulo 6 equals 2 or 20 % 6 � 2<br />

Here are a few more, with some blanks for you <strong>to</strong> fi ll in.<br />

17 divided by 4 equals 4 remainder 1 17 % 4 � 1<br />

3 divided by 5 equals 0 remainder 3 3 % 5 � 3<br />

10 divided by 3.75 equals 2 remainder 2.5<br />

10.0 % 3.75 � 2.5<br />

100 divided by 50 equals ___________ remainder ______ _________ 100 % 40 � ____________<br />

9.25 divided by 0.5 equals ___________ remainder _______________ 9.25 % 0.5 � ______ _____<br />

You will notice that if A � B % C , A can never be larger than C . Th e remainder can never be larger than<br />

the divisor.<br />

0 % 3 � 0<br />

1 % 3 � 1<br />

2 % 3 � 2<br />

3 % 3 � 0<br />

4 % 3 � 1<br />

etc .

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

Saved successfully!

Ooh no, something went wrong!