2.3.1 Drawing the checkerboard.
Write the routine checkerboard(int size) that draws the checkerboard shown in Figure 2.33b.
Place the checkerboard with its lower left corner at (0,0). Each of the 64 squares has length size
pixels. Choose two nice colors for the squares.
Solution: The ij-th square has lower left corner at (i*size, j*size) for i = 0,..,7 and j =
0...7. The color can be made to alternate between (r1, g1, b1) and (r2, g2, b2) using one
color when i+j is even, and the other color when i + j is odd.
void drawCheckerBoard(). The rest of the method simply places these ideas within two
imbedded loops:
{
for (int i=0;i<8;i++)
for (int j=0;j<8;j++)
{
if((i + j)%2 ==0) // if i + j is even
glColor3f( 1, 1, 1);//white color
else
glColor3f(0.5, 0.5, 0.5);//Gray color
glRecti(i*size, j*size, (i+1)*size, (j+1)*size);
}
}
3.2.1 Ex. Building the mapping. Find values of A, B, C, and D for the case of a world
window (0.0, 10.0, -6.0, 6.0) and a viewport (0, 600, 0, 400).
Solution.
sx = A x + C, sy = B y + D
C V .l A W.l 0 60 * 0 0
while D V .b B W .b 0 33.33 * (6) 200
B
V .t V .b
400 0
33.33
W .t W .b 6 (6)
A
V .r V .l 600 0
60
W .r W .l 10 0
3.3.2. clipping for the window with (left, right, bottom, top) = (30,220,50,240)
Solution.
1). p1=(40,140), p2=(100,200);
2). p1=(10,270), p2=(300,0);
300
300
250
250
200
200
150
150
100
100
50
50
0
0
0
50
100
150
200
250
300
0
3). p1=(20,10), p2=(20,200);
50
100
150
200
250
300
4). p1=(0,0), p2=(250,250);
300
300
250
250
200
200
150
150
100
100
50
50
0
0
0
50
100
150
200
250
300
0
50
100
150
200
250
300
3.4.1. Draw Rosettes
void oddRosette(int n, float radius)
{
Point2 * pt = new Point2[n];
if(n < 3) return;
double angle = 2 * 3.14159265 /n;
// bad number of sides
//angle increment
//generate the vertices pt[0],. . .,pt[N-1]
for(int i = 0; i < n; i++)
pt[i]->set(radius * cos(i*angle), radius * sin(i*angle));
//Connecting edges
for(int j=0; j<n-1; j++) // begin at j-th vertex
for(int k = j + 1; k < n ; k++)
{
moveTo(pt[j]); // connect j-th to all the remaining vertices.
lineTo(pt[k]);
}
}
4.3.12. Solution: a= (2,3) and n = (-2,1) Find the Reflected Direction.
Solution: From the problem, we know a 2 3i ; n 2 i .
The direction of the reflection, r, can be represent as:
Derive the reflection r as: (see book)
2(a n)n
r a 2(a nˆ )nˆ a
2
n
(2 3i ) 2[( 2 3i ) (2 i )]
2i
(2) 2 12
2i
5
4 2i
2 3i
5
6 17
6 17
i ,
5 5
5 5
2 3i 2(1)
4.7.1. Solution: Intersections of rays with lines and planes.
Find when and where the ray A + ct hits the object n. (p-B) = 0.
a) A = (2, 3), c = (4, -4), n = (6, 8), B = (7, 7)
Derive the as thit(see book):
n ( B A) (6,8) [(7,7) (2,3)] 30 32
t hit
7.75
nc
(6,8) (4,4)
24 32
Phit = A + cthit = (2, 3) + (4, -4)*(-7.75) = (-29, 34)
b) A = (2, -4, 3), c = (4, 0, -4), n = (6, 9, 9), B = (-7, 2, 7)
thit = n.(B-A)/n.c = -3
Phit = A + cthit = (-10,-4,15)
4.7.2. Solution: Rays hitting Planes.
Find the point where the ray (1, 5, 2) + (5,-2,6)t hits the plane 2x -4y + z=8
Let A (1, 5, 2) and B (0, 0, 8) .
The normal vector of the plane is n (2, 4, 1) .
n ( B A) (2,4,1) [(0,0,8) (1,5,2)] (2,4,1) (1,5,6) 2 20 6
t hit
1
nc
(2,4,1) (5,2,6)
(2,4,1) (5,2,6)
10 8 6
Therefore, the hitting point is:
Phit (1,5,2) (5,2,6)1 (6,3,8) .
4.8.3. Find the Clipped Line.
Apply Cyrus-Beck to find the portion of the segment with endpoint (2,4) and (20,8) within the
quadrilateral window with corners at (0,7), (9,9) (14,4) and (2,2).
Solution: The parametric form of the segment with endpoints (2,4) and (20,8) is:
l : P(t ) (2,4) (18,4)t
The four parametric forms of the four sides of the quadrilateral window are:
l1 : P(s1 ) (0,7) (9,2)s1
l2 : P(s2 ) (9,9) (5,5)s2
l3 : P(s3 ) (2,2) (12,2)s3
l4 : P(s4 ) (2,2) (2,5)s4
l and l1 are parallel;
11
l and l 2 intersects at t , the corresponding intersecting point is (17.5, 23/3);
12
l and l 3 intersects at t 0 ;
l and l 4 intersects at t 0 ;
From above, we can see that point (2,4) is included in the quadrilateral window.
Therefore, the portion of the segment that lies within the quadrilateral window is a segment with
start point (2,4) and end point (17.5, 23/3).
© Copyright 2026 Paperzz