Sunteți pe pagina 1din 14

Computer Graphics Using OpenGL - Hill/Kelley 3rd Edition

Chapter 5 – Solutions to Selected Practice Exercises

Solutions to Selected exercises for Chapter 5 (preliminary version)

5.2.2. Solution: Sketch the effect.

(Qx, Qy) = (SxPx, SyPy) Sx = 3, Sy = -2


a). The circle scales into an ellipse 3 times as wide and twice as high;
b). The square scales to a rectangle 3 times as wide and twice as high, as well as being
upside down; The points of the square scale as shown:
(1,1), (1, 2), (2, 2), (2,1) → (3, -2), (3, -4), (6, -4), (6, -2)

5.2.4. Solution: Shearing Lines.

Use Qx= Px, Qy = gPx + Py

Each line is transformed into another straight line (this will be proved in general
subsequently), with transformed endpoints:

a) (-3, 2.8), (2, 4.8)


b) (-3, -5.2), (2, -3.2)
c) (-2, 4.2), (-2, -1.8)
d) (-1, -2.4), (3, 3.2)

The given figures in F5.12 shear into the figures with new points:
a) (-1,0), (0,1), (1,0), (0, -1) → (-1, 0), (0, 1), (1, 0.4), (0, -1)
b) (0,0), (0,1), (1,1), (1, 0) → (0, 0), (0, 1), (1, 1.4), (1, 0.4)
c) (1,1), (1, 2), (2, 2), (2,1) → (1, 1.4), (1, 2.4), (2, 2.8), (2, 1.8)

Page 1 of 14 file: 47134582.doc


Computer Graphics Using OpenGL - Hill/Kelley 3rd Edition

5.2.5.
Solution: To invert a Shear.

By the equation 5.11 and 5.12:


 1 - g 0

M 1 = 


- h
0
1
0
0

1
/(1-gh). This is a shear only if gh = 0
 

5.2.7. Solution: An Inverse Matrix.


By direct calculation ( see Appendix 2 for methods and details:M =
3 2 1  0.2 - 0.4 - 0.2 
- 1

 0
1
0
0


1
M 1 =  0.2

 0.
0.6
0
- 0.2 
1.


  

5.2.8. Solution:
Fixed Points of an Affine Transformation.
 M 11 M 12 0
 
a) when the third column of M is (0,0,1), M=  M 21 M 22 0
M M 31 1 
 31
 M 11 M 12 0
 
M (0, 0, 1) =  M 21
T M 22 0  (0, 0, 1) T = (0, 0, 1) T
M M 31 1 
 31
Origin is a fixed point
b) T(P) = T(P-F+F) = T(P-F)+T(F) = M(P - F)+ F.
c) Point V is the fixed point for a rotation about point V.
 cos( )  sin( ) d x 
 
MV=  sin( ) cos( ) d y  (Vx, Vy, 1)T
 0 0 1 

=( V x cos( )  VY sin( )  d x , V x sin( )  VY cos( )  d y , 1 )

Since
d x   cos( )V x  sin( )V y  V x
d y   sin( )V x  cos( )V y  V y
MV= ( V x cos( )  VY sin( )  d x , V x sin( )  VY cos( )  d y , 1 ) = (V x , V y , 1)
Point V is the fixed point for a rotation about point V.
e) Point V is the fixed point for a scaling with scale factors Sx and Sy, about V.
f) T(T(P)) = T(M(P - F)+ F) = M2(P - F)+ F
T(T(T(P))) = T(M2(P - F)+ F) = T(M2(P - F))+ T(F)= M3(P - F)+ F

Page 2 of 14 file: 47134582.doc


Computer Graphics Using OpenGL - Hill/Kelley 3rd Edition

T(T(T(T(P)))) = T(M3(P - F)+ F) = T(M3(P - F))+ T(F)= M4(P - F)+ F


T(T(T(T(T(P))))) = T(M4(P - F)+ F) = T(M4(P - F))+ T(F)= M5(P - F)+ F

5.2.9. Solution: To Find Matrices.

1 0 2  cos( )  sin( ) 0  1 0  2  2 0 0
    
a)  0 1 1  sin( ) cos( ) 0  0 1  1  0 1 0
0 0 1  0 0 1  0 0 1  0 0 1 

.
 1 0 1 S x 0 - 2S x  2 
  
b)  0 1 1 0 Sy - 3Sy  3 
 0 0 1 0 0 1 
  
 3 
  0 .5 0
1 0 1 2  2 0 0  1 0.3 0  1 0 - 1 
  3  
c) 0 1 1 0.5 0  0 1 0 0 1 0  
 0 1 - 1  =
0 2
 0 1
 0 0

1  0 0 
1 0 0 10 0 1  
 
 
 2.31 0.19 - 2.01 
 1 1.165 - 0. 3
 

 0 0 1 

5.2.10. Solution: To Normalize a Box.

The given box is a rectangle, with its lower edge tiled at angle  = 30 
1. rotate through -30 
2. scale by (1/w, 1/2w), where w = 5
 1  3 1 
 0 0   0
 5  2 2 
 1  1 3 
M=  0 0 
2 2
0 =
2 5
  0 0

1
 0 0 1 

  

 0.3867 - 0.2235 0
 0.1118 0.1933 0
 

 0 0 
1 

5.2.11. Solutions: Some Transformations Commute.


Do this by direct multiplying out SR and RS

 Sx 0 0   cos( )  sin( ) 0   S x cos - S x sin  0


   
SR =  0 Sy 0    sin( ) cos( ) 0  =  S y sin  S y cos 0
0 0 1   0 0 1   0 0 1 
 

Page 3 of 14 file: 47134582.doc


Computer Graphics Using OpenGL - Hill/Kelley 3rd Edition

 cos( )  sin( ) 0  Sx 0   S x cos - S y sin  0 


0
     
RS =  sin( ) cos( ) 0  0
 0  =  S x sin  S y cos 0 
Sy
 0
 0 1   0 1   0
0 0 1 
For uniform scaling, S x = S y , SR = RS. So the uniform scaling commutes with
rotation.
The differential scaling does not commute with rotation.

5.2.12. Solutions: Reflection plus a rotation.


Reflection:
1. reflection in x
2. reflection in y
  1 0 0  1 0 0   1 0 0
M1 =  0 1 0    0  1 0  =  0  1 0 
 0 0 1 0 0 1  0 0 1 
    

Rotate 180 
 cos180   sin 180 0    1 0 0 
M2 =  sin 180  cos180  0  =  0  1 0 
 0 0 1   0 0 1 

M1 = M2
So a reflection in x followed by a reflection in y is the same as a rotation by 1800.

5.2.13. Solution: Two Successive Rotations.


Rotation
1. rotation through angle 1, R(1)
2. rotation through angle 2, R(2)
 cos 2  sin  2 0   cos 1  sin  1 0 
 sin  cos 2 0   sin  1 cos 1 0 
 2

 0 0 1  0 0 1
 cos 2 cos 1  sin  2 sin  1  cos 2 sin  1  sin  2 cos 1 0 
 
=  sin  2 cos 1  cos 2 sin  1  sin  2 sin  1  cos 2 cos 1 0 
 0 0 1
 cos( 2   1 )  sin( 2   1 ) 0 
 
=  sin( 2   1 ) cos( 2   1 ) 0 
 0 0 1

Page 4 of 14 file: 47134582.doc


Computer Graphics Using OpenGL - Hill/Kelley 3rd Edition

= R (1 + 2)
R(1) followed by R() is equivalent to applying the single rotation R(1 + ).
Thus successive rotations are additive.

5.2.14. Solutions: A Succession of Shears

 1 0 0  1 h 0 1 h 0
 g 1 0  0 1 0 =  g 1  gh 0 
     
 0 0 1  0 0 1 0 0 1
Which is a shear if gh = 0.

5.2.24 Solution: The argument generalized.


W = a1 P1 + a2 P2 + … + aN PN where a1 + a2 + … + aN = 1
T(W) = T(a1 P1 + a2 P2 + … + aN PN) = a1 T(P1) + a2 T(P2) + … + aN T(PN)

5.2.25. Solutions: Show that relative ratios are preserved.

P = A + bt where b = B - A.
So P = A + bt = A + (B – A)t = (1 - t) A + t B
|P - A| = | t B – t A| = t |B – A|
|P - B| = | (1– t) A +(t -1) B | = |(1 - t) (B – A)| = (1 - t) |A – B| = (1 - t) |B – A|
This is true if t lies outside of the range 0 to 1 because t + (1 – t) = 1.
T(P) = (1 - t) T(A) + t T(B)
|T(P) - T(A)| = | - t T(A) + t T(B)| = t | T(B) – T(A)|
|T(P) - T(B)|.= (1 - t) | T(B) – T(A)|

Exercise 5.3.1.
Solution.
Yes, by direct visualization, 90o rotations about each axis rotate each axis into
another To visualize the 90° .
Rotating a point on the x – axis about the x – axis has no effect for the point.

Rotations

For example as x – roll, there is c = cos90  = 0, s = sin90  =1, we have


1 0 0 0  x  1 0 0 0  x   x 
       z 
Q= 0 c - s 0  y 0 0 -1 0  y
0       
s c 0  z  0 1 0 0  z   y 
         
  1 0  1
0 0 0 1    0 0 1     1 

Page 5 of 14 file: 47134582.doc


Computer Graphics Using OpenGL - Hill/Kelley 3rd Edition

Also we can get the familiar results for y – roll and z – roll. So a 90  rotation about each
axis rotates the other axes into one another.

Exercise 5.3.2. To Rotate the Basic Barn.


Sketching not done here.

5.3.3. Solution: Do a Rotation, a –roll of 45 degrees.


 c 0 s 0  1  0 
  2 
  2
Q= 
0
 - s
1
0
0
c
0
0    1
 1 
0 

, where c = s = 0.707, based on
 0 0 0 1   1 

a rotation by 45 degrees.
This is reasonable since the y-coord Is not affected, due to the placement of zeroes in the
matrix.

5.3.4. Solution: Test 90° Rotations of the Axes.

cos90  = 0, sin90  =1
For x – roll,
1 0 0 0  1 
 0   0
Q xi =  0
 0
0
1
- 1
0
0
0 

 0
1


=
 0 0 1   
1   0   0 
 0   0    1
 0  Q xj = 1  Q xz =  1 
1  1   1 
     

i  i ; j  k ; k  -j

for y-roll,
 0   0  1 
   
 0 
Q yi = 

0 
 1 Q yj = 1
 0

 Q yz =  0 







 1 
 1  1   

i  -k ; j  j ; k  i

for z-roll,
 0    1  0 
   
 0 
Qzi = 1
 0




Q zj = 


0
0



Qzk = 1
1


   
1   1   

i  k ; j  -i ; k  k

5.3.5. Solution: Is a y-roll indeed different?


1 0 0 0  x   x 
    
Qx =  0
= (x, cy-sz, sy + cz)
c - s 0  y  cy - sz 
 0 
s c 0  z  sy  cz 
    
 
 0 0 0 1 1   1 

Q current = x, Q next = cy – sz, Q prev = sy + cz , P current = x, P next = y, P prev =z


Also

Page 6 of 14 file: 47134582.doc


Computer Graphics Using OpenGL - Hill/Kelley 3rd Edition

Q y = (cx + sz, y, cz – sx)


Q current = y, Q next = c z – sx, Q prev = sz + cx , P current = y, P next = z, P prev = x
Q z = (cx - sy, sx + cy, z)
Q current = z, Q next = c x – sy, Q prev = sx + cy , P current = z, P next = x, P prev = y

5.3.6. Solution: Which ones commute?

a) pure translations : T 1 T 2 = T 2 T 1 : yes, commute.


b) both are scaling :
S x1 0 0 0  S x2 0 0 0 S x1S x2 0 0 0
 0 0  0  0
T1T2 = = commute. T
 S y1 0  0 S y2 0    0 S y1S y2 0 
 0 0 S z1 0  0 0 S z2 0  0 0 S z1 S z2 0
    
 0
 0 0 1 0 0 0 
1 
 0 0 0 
1

2 T1
c) shear:
1 0 0 0 
 

T1T2 =
 f 1 0 0 
 0 0 1 0 
 
 0 0 0 1
 
1 k 0 0 
 
 0
=
1 0 0 
 0 0 1 0 
 
 0 0 0 1 
 
1 k 0 0 
 
 f 1  fk 0 0 
 0 0 1 0 

 
 0 0 0 1

1 k 0 0 
 

T T  0
=
1 0 0 
 0 0 
2 1 

0 1


 0 0 0 1 
 1 0 0 0 
 
 f
=
1 0 0 
 0 0 1 0 

 
 0 0 0 1
1  fk k 0 0 
 
 f 1 0 0 
 0 0 1 0 
 
 0 0 0 1 
 

They commute if fk = 0.

d) A rotation and a translation


for example x – roll
1 0 0 m14 
 
1

0 0 0 
 0 1 0 m24 
T1T2 =  0
=
c - s 0 
 0


 0
s
0
c
0
0 
1

 0 0 1 m34 
 
0 0 0 1 

1 0 0 m14 
 
0 c -s m 24 c - m34 s 
0 s c m 24 s - m34 c 
 
0 0 0 1 
 

Page 7 of 14 file: 47134582.doc


Computer Graphics Using OpenGL - Hill/Kelley 3rd Edition

1 0 0 m14 
 
0 1 0 m24  1

0 0 0 

T2 T1 =  0
=
c - s 0 

m34 
 0 s c 0 
0 0 1 

 0 0 0 1

 
0 0 0 1 

1 0 0 m14 
 
0 c - s m24 
0 s c m24 
 
0 0 0 1 

T 1 T 2 is not the same as T 2 T 1 . Familiar with x-roll, we can get the same result by y –
roll and z – roll.

e) A rotation and a scaling


for example x – roll
Sx 0 0 0
 
1

0 0 0 
 0 Sy 0 0
T1T2 =  0
=
c - s 0 
 0


 0
s
0
c
0
0 
1
 0 0 Sz 0
 

0 0 0 1 

 Sx 0 0 0
 
0 cS y  sS z 0
0 sS y cS z 0
 
0 0 0 1 

Sx 0 0 0
 
0 cS y  sS y
0
T2 T1 =
0 sS z cS z 0
 
0 0 0 1 

T1T2 is not the same as T 2 T 1 .

f) a scaling and a shear, for example in x-axes shear


Sx 0 0 0
 
 0 S y 0 0 1

k 0 0 

T1T2 =   0
=
1 0 0 

0 S z 0
 0 0 1 0 

0 

 0 0 0 1

 

0 0 0 1 
 
 x
S kS 0 0 
 x

 0 Sy 0 0
0 0 S z 0
 
0 0 0 1 

Page 8 of 14 file: 47134582.doc


Computer Graphics Using OpenGL - Hill/Kelley 3rd Edition

Sx kS y 0 0
 
 0 1 0 0
T2 T1 =
0 0 Sz 0
 
 0 0 0 1 

T 1 T 2 is not the same as T 2 T 1 .

5.3.7. Solution: Special cases of rotation about a general axis u.

Using Equation 5.33


a) X- axis, i
1 0 0 0 
 

R u () =  0 c - s 0 
 0 s c 0 
 
 0 0 0 1
 

It’s the same as x – roll.


We also can get the familiar results of y – axis, j ; z – axis , k.

5.3.8. Solution: Classic Approach to Rotation about an Axis.


It’s Equation 5.32.
Ru() = Rz( -) Ry( -) Rz() Ry() Rz()
=
cos( )  sin( ) 0 0  cos( ) 0 sin( ) 0  cos   sin  0 0  cos  0 sin  0  cos  - sin  0 0
sin( ) cos( ) 0  0   0   0   0
 0  0 1 0  sin  cos  0  0 1 0   sin  cos  0 
 0 0 1 0  - sin( ) 0 cos(  ) 0  0 0 1 0  - sin  0 cos  0  0 0 1 0 
     
 0 0 0 1  0 0 0 1  0 0 0 1  0 0 0 1  0 0 0 1 

Where  = 30   cos = 0.866, sin = 0.5;


 = 45   cos = 0.707, sin = 0.707;
 = 35   cos = 0.819, sin = 0.5735
So we can get the final result is:
 .877  .366 .281 0
 
~  .445 .842  .306 0
M 
 .124 .396 .910 0
 
 0 0 0 1 

5.3.9. Solution:
Orthogonal Matrices.

By direct calculation each of the three rotation matrices has orthogonal columns (and
rows), each of length 1. The determinant of an orthogonal matrix is 1. Partition
orthogonal matrix M into its columns:
 c1T 
 
M = (c1| c2 | c3), so M T =  c2
T

 c3 T


. Now note that M M T = I, the identity
 

matrix, due to the properties of the columns. So M T = M -1 .

Page 9 of 14 file: 47134582.doc


Computer Graphics Using OpenGL - Hill/Kelley 3rd Edition

5.3.10. Show that the Matrix of Equation 5.33 Is Orthogonal.

Based on the hint that accompanies this problem we need only show that the product of
two orthogonal matrices is orthogonal. Suppose A and B are orthogonal, and set C =
A * B. Now examine C-1.

We must show C-1 is the same as CT. But for any matrix C the inverse can be written as
the reversed product of its individual inverses, so C-1 = B-1 * A-1, and since these guys are
orthogonal, we have C-1 = BT * AT.

Now since the transpose of a product can be written as the product of transposes (but
reverse their order), C-1 = (A*B)T, so yes indeed, C-1is CT!

5.3.11. Solution: Structure of a rotation matrix.

Use Equation 5.33, where c is cos, s is sin


a) The first row  the second row is:
2
[c + (1-c)u 2x ]i[(1– c)u x u y + su z ]i + [(1– c)u x u y - su z ]k [c + (1-c)u y ]k
+ [(1– c)u x u z + su y ]j [(1– c)u z u y - su x ]j
3
= 2c(1-c)u x u y + (1- c) 2 u 3x u y + (1- c) 2 u y u x + (1-c)u 2z u x u y -s 2 u x u y
2
= (1- c) 2 u x u y ( -1 + u 2x + u y + u 2z )
=0
They are orthogonal.
Also there are the first the third = 0; the second the third = 0
b) Use Equation 4.28
First  Second = Third

5.3.12. Solution: What if the axis of rotation does not pass through the origin?
1. translate to the origin through –S
2. apply the rotation
3. translate back through S

Page 10 of 14 file: 47134582.doc


Computer Graphics Using OpenGL - Hill/Kelley 3rd Edition

1 0 0 Sx   c  (1  c)u x 2 (1  c)u y u x  su z (1  c)u z u x  su y 0


   2

0 1 0 Sy   (1  c)u xu y  su z c  (1  c)u y (1  c)u z u y  su x 0
0 0 1 Sz   2 

0
  (1  c)u xu z  su y (1  c)u y u z  su x c  (1  c)u z 0
 0 0 1   0 0 0 1 

1 0 0  Sx 
 
0 1 0  Sy 
0 0 1  Sz 
 
0 0 0 1 

=
 c  (1  c)u x 2 (1  c)u y u x  su z (1  c)u z u x  su y
2
 S x [c  (1  c)u x ]  S y [(1  c)u y u x  su z ]  S z [(1
 2 2
 (1  c)u u  su z c  (1  c)u y (1  c)u z u y  su x  S x [(1  c)u u  su z ]  S y [c  (1  c)u y ]  S z [(1
 2
 (1  c)u x u z  su y (1  c)u y u z  su x c  (1  c)u z  S x [(1  c)u x u z  su y ]  S y [(1  c)u y u z  su x ]  S
 0 0 0 1

5.4.1. Solution: How transforming a coordinate system relates to transforming a


point.

Suppose we have a 2D coordinate frame #1 as shown in Figure 5.31, with origin  and
axes i and j. Further suppose we have an affine transformation T(.) represented by
matrix M. So T(.) transforms coordinate frame #1 into coordinate frame #2, with new
origin  ’ = T( ), and new axes i’ = T(i) and j’ = T(j).
i’= M(1, 0, 0)T = Mi ; j’= M(0, 1, 0)T = Mj
ci’ + dj’ +  ’ = cM(1, 0, 0)T + dM(0, 1, 0)T+ M(0, 0, 1)T= c Mi + d Mj + M
c 
M(c, d, 1)T= M d

1



= c Mi + d Mj + M
 

M(c, 0, 0) + M(0, d, 0) + M(0, 0, 1) T = c Mi + d Mj + M


T T

ci’ + dj’ +  ’ is the same as M(c, d, 1)T and M(c, 0, 0)T+ M(0, d, 0)T + M(0, 0, 1) T.
The same result in Equation 5.35.

5.4.2. Solution: Using elementary examples.


1 0 m  e 
 
n   e  m 
 f  n
a) P = 0 1  f  =  
0 0 
1  

 1 

 1 
The point (e, f) in the new system lies at (e + m, f + n) in the original system.

Page 11 of 14 file: 47134582.doc


Computer Graphics Using OpenGL - Hill/Kelley 3rd Edition

 cos A - sinA 0  e 
 
0    e cos A  f sin A
 e sin A  A
b) P =  sin A cos A f  =  f cos 
 0 0 
1 1 

 1 

 
The point (e, f) in the new system lies at (e cos(a) - f sin(a), e sin(a) + f cos(a)), where
a =  A / 180 radians.
3 0 0  e 
 
0   3e 
2 f 
c) P = 0 2  f  =  
0 0 
1  

 1 

 1 
The point (e, f) in the new system lies at (3e, 2f) in the original system.
1 0 0  e 
 
0    e 
d) P = 0 1  f  = 
 f 

0 0 
1  
 1
 

 1 
The point (e, f) lies in the original system at (e, -f).

5.5.3. Solution: A hexagonal tiling.

a)  OAB is equilateral
So OA=AB=R
Length of a hexagon is R too

b) The distance between two rows can be expressed as:


OO’= 2OC = 2 OA .Sin60 = 3 R
The distance between two columns can be expressed as:
3
OE= OD+DE=R+ OA .Cos60= R
2
c)
# define PI 3.1415926

Page 12 of 14 file: 47134582.doc


Computer Graphics Using OpenGL - Hill/Kelley 3rd Edition

cvs.pushCT(); // so we can return here


cvs.translate2D(0, 0); // position in origin for the first motif
for(i = 0; i < numberofColumns; i++)// for each column
{
cvs.pushCT();
for(j = 0; j <numberofRows; j++) // for each row
{
drawmotif();
// move to the right
cvs.translate2D(0, 2* sin(PI/3.)*R);
}

// back to the start of this column


cvs.popCT();
// move to the next column
cvs.translate2D(1.5*R,pow(-1,i+1)* sin(PI/3.)*R);
}
cvs.popCT();

5.6.1. Solution: What does gluLookAt() do?

a).
n  eye  look
u  up  n
v  nu
So that u.v = 0; u.n = 0; n.v = 0;
u, v, and n are mutually perpendicular.

b)
ux u y uz d x 
 
 vx v y vz d y 
V  eye   (eye.x, eye. y , eye.z, 1)
nx n y nz d z 
 
0 0 0 1
 
= (eye  u  dx , eye  v  dy , eye  n  dz, 1)
= (eye  u  eye  u, eye  v  eye  v, eye  n  eye  n, 1)
= (0, 0, 0, 1)

Page 13 of 14 file: 47134582.doc


Computer Graphics Using OpenGL - Hill/Kelley 3rd Edition

ux uy uz dx 
 
v vy vz dy 
V u   x (u.x, u. y , u.z , 0) = (u  u, u  v, u  n, 0) = (1, 0, 0, 0)
n ny nz dz 
 x 
0 0 0 1 

Similarly, V v (0, 1, 0, 0)
V  n  (0, 0, 1, 0)

c)
n  eye  look = (4, 3, 4)
i j k
u  up  n = 0 1 0
= (4, 0,-4)
4 3 4

v  n  u = (-12, 32, -12)

Normalize u, v, and n,
n = (0.70711, 0, 0.70711)
u = (-0.3313, 0.8834, - 0.3313)
v = (0.6247, 0.4685, 0.6247)
dx =-eye.u =(4, 4, 4) . (-0.3313, 0.8834, - 0.3313) = 0
dy =-eye.v =(4, 4, 4) . (0.6247, 0.4685, 0.6247) = -0.88345
dz =-eye.n =(4, 4, 4) . (0.70711, 0, 0.70711) = -6.872

So that,
 .70711 0  .70711 0 
 
  . 3313 .88345  .3313  .88345
V 
.6247 .4685 .6247  6.872 
 
 0 0 0 1 

5.6.2. Inquiring of the values in a matrix in OpenGL.


This solution not included.

5.6.3. Inquiring of the values in a matrix in OpenGL.


This solution not included.

Page 14 of 14 file: 47134582.doc

S-ar putea să vă placă și