228 Learning Processing
Is this rectangle fl ying off of the computer screen about to bump into your nose? Technically, this is
of course not the case. It is simply a rectangle growing in size. But we have created the illusion of the
rectangle moving toward you.
Fortunately for us, if we choose to use 3D coordinates, Processing will create the illusion for us. While the
idea of a third dimension on a fl at computer monitor may seem imaginary, it is quite real for Processing.
Processing knows about perspective, and selects the appropriate two-dimensional pixels in order to create
the three-dimensional eff ect. We should recognize, however, that as soon as we enter the world of 3D
pixel coordinates, a certain amount of control must be relinquished to the Processing renderer. You can no
longer control exact pixel locations as you might with 2D shapes, because XY locations will be adjusted to
account for 3D perspective.
In order to specify points in three dimensions, the coordinates are specifi ed in the order you would expect:
x , y , z . Cartesian 3D systems can be described as “ left-handed ” or “ right-handed. ” If you use your right
hand to point your index fi nger in the positive y direction (up) and your thumb in the positive x direction
(to the right), the rest of your fi ngers will point toward the positive z direction. It is left-handed if you use
your left hand and do the same. In Processing, the system is left-handed, as shown in Figure 14.2 .
–Z
+X
+Y+Y
(0,0) (0,0)
+X
fi g. 14.2
Our fi rst goal is to rewrite Example 14-1 using the 3D capabilities of Processing. Assume the following
variables:
int x = width/2;
int y = height/2;
int z = 0;
int r = 10;
In order to specify the location for a rectangle, the rect( ) function takes four arguments: an x location,
a y location, a width, and a height.
rect(x,y,w,h);