Graphics Programs Reference
In-Depth Information
When we scale the object, we have to change its coordinates. Note that the scaling is
done about the center point of the object. If the expression looks strange, it's a good
idea to try a simple example like a square using pencil and paper. If a scale factor is
1, then the coordinates stay as they are. If a scale factor is 2, then the coordinates
expand equally on either side of the center point.
The equations for the changes in the coordinates due to rotation are considerably more
complicated than the others, and as a result, a separate function, rotateObject() has
been defined to handle this situation (line 92).
Finally, once the 3D coordinates of the object have been updated in just about every
conceivable way, we convert them to screen coordinates using the standard equations
(lines 97-98).
Step 8: Rotate the object about its center point
The function to rotate the object about its center point is the most complicated in the
program as it involves trigonometry and, frankly, isn't a whole lot of fun. To spare you
as much pain, agony, and angst as possible, we'll focus on presenting the main ideas.
The rotation angles rx , ry , and rz are passed to the function through the button
actions. Since they are in degrees, they need to be converted to radians in the usual
way (lines 108-110). Once converted to radians, the sine and cosine of each of the
angles is calculated (lines 113-115). These can be done outside of any loop since the
rotations will be applied to all of the points of the object.
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// -------------------------------------------------------
// function to rotate the object about its center point
rotateObject = function(rx,ry,rz)
{
// convert degrees to radians for rotation angles
var a = Math.PI * rx/ 180;
var b = Math.PI * ry/ 180;
var c = Math.PI * rz/ 180;
// calculate sines and cosines of rotation angles
var sina = Math.sin(a); var cosa = Math.cos(a);
var sinb = Math.sin(b); var cosb = Math.cos(b);
var sinc = Math.sin(c); var cosc = Math.cos(c);
Search WWH ::




Custom Search