Graphics Reference
In-Depth Information
is outside the frame. We'll ignore this problem, and assume that if we ask our
graphics library to draw a line segment in our picture, and the coordinates of the
line segment go outside the bounds of the picture, nothing gets drawn outside the
bounds. (This is true, for example, for WPF.) A preliminary sketch of the program
is given in Listing 3.4.
Listing 3.4: Edge-drawing version of the Dürer rendering algorithm.
Input: a scene containing one object ob
Output: a drawing of the objects
1
2
3
4
5
6
7
8
9
10
11
12
13
14
initialize drawing to be blank ;
for (int i = 0; i < number of vertices in ob ; i++){
Point3D P = vertices[i];
pictureVertices[i] = Point(-P.x/P.z, P.y/P.z);
}
for {int i = 0; i < number of edges in ob ; i++){
int i0 = edges[i][0];
int i1 = edges[i][1];
Draw a line segment from pictureVertices[i0]
to pictureVertices[i1];
}
Finally, we have to observe that this algorithm depends on the “picture rectan-
gle” having coordinates that run from ( x min , y min ) to ( x max , y max ) . We can, however,
remove this dependency by using coordinates that range from 0 to 1 in both direc-
tions, as is very common in graphics libraries. We can convert the x -coordinates
as follows. First, subtract x min from the x -coordinates; the new coordinates will
range from 0 to x max
x min . Then divide by x max
x min , and the new coordinates
will range from 0 to 1. So we have
x min
x max
x
x new =
x min ;
(3.3)
an analogous expression gives us y -values that range from 0 to 1. Because we've
required that x max
y min , we get no distortion by transforming x and
y this way: Both are divided by the same factor. The program, modified to include
this transformation, is shown in Listing 3.5.
Recall that we negated x so that the picture would be correctly oriented. When
we negate x new , however, the resulting values will range from
x min = y max
1to0.Wethere-
foreadd1toreturntheresulttotherange0to1,inline11ofListing3.5.
Listing 3.5: Edge-based implementation with the limits of the view square
included as parameters.
1
Input: a scene containing one object o , and a square
x min x x max and y min y y max in the z = 1 plane.
Output: a drawing of the object in the unit square
2
3
4
5
6
7
8
9
10
11
12
initialize drawing to be blank ;
for(int i= 0; i < number of vertices in o ; i++){
Point3D P = vertices[i];
double x = P.x/P.z;
double y = P.y/P.z;
pictureVertices[i] =
Point(1 - (x - xmin)/(xmax - xmin),
(y - ymin)/(ymax - ymin));
}
 
Search WWH ::




Custom Search