Graphics Programs Reference
In-Depth Information
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// -------------------------------------------------------
// this function finds the center point of the object
// which is used for scaling and rotation of the object
findTheCenterPoint = function()
{
xmax = ymax = zmax = -10000;
xmin = ymin = zmin = 10000;
// find the max and min points of the shape
for (c = 0; c < numPts; c++)
{
xmax = Math.max(xmax,x[c]);
xmin = Math.min(xmin,x[c]);
ymax = Math.max(ymax,y[c]);
ymin = Math.min(ymin,y[c]);
zmax = Math.max(zmax,z[c]);
zmin = Math.min(zmin,z[c]);
}
// take the average of max and min to find the center
xc = (xmax + xmin)/2;
yc = (ymax + ymin)/2;
zc = (zmax + zmin)/2;
}
loop over all of the points in the object and compare each point with the current
maximum and minimum values using the Math.max() and Math.min() functions. For
example, in line 52, the x-coordinate of a point is compared with the current value of
xmax . If it is smaller than xmax , then nothing happens. If it is larger, then that x-coordi-
nate becomes the new value of xmax . The other values are determined similarly. Once
the loop is complete, the center point is just the average of the maximum and mini-
mum values in each of the three directions (lines 62-64).
Step 7: Convert 3D coordinates to screen coordinates
The function getScreenCoords() is similar in function to the displayObject()
function used in previous chapters. This key function performs several tasks and uses
information passed to it by the buttons. First, the distance from the camera to the
Search WWH ::




Custom Search