You are on page 1of 88

# CSC251

Module: Geometry
P. J. Narayanan
Spring 2016

CSC251
Graphics Process

## CSC251 Basics of Computer Graphics

Graphics Process
World Convert to Framebuffer
Representation Graphics Graphics API
Primitives Image

• Represent it using natural structures in the program.
Convert to standard primitives supported by the API
• Processing is done by the API. Converts the primitives
in stages and forms an image in the framebuffer
• The image is displayed automatically on the device
CSC251 Basics of Computer Graphics
How to Draw A House?

## CSC251 Basics of Computer Graphics

Drawing A House
• Compose using basic shapes

// Main part
drawRectangle(v1, v2, v3, v4);
drawTriangle(v2, v3, v5); // Roof
drawRectangle( ... ); // Door
drawRectangle( ... ); // Window
drawRectangle( ... ); // Chimney
drawCircle( ... ); // Sun

## CSC251 Basics of Computer Graphics

Graphics Primitives

## • Graphics is concerned with the appearance of the 3D

world to a camera
• Only outer surface of objects important, not interiors!!
• Hence, uses only 1D and 2D primitives

## • Points: 2D or 3D. (x, y) or (x, y, z).

• Lines: specified using end-points
• Triangles/Polygons: specified using vertices
• Why not circles, ellipses, hyperbolas?
CSC251 Basics of Computer Graphics
Graphics Attributes

## • Point is the most basic primitive

CSC251 Basics of Computer Graphics
Point Representation

## • A point is represented using 2 or 3 numbers (x, y, [z]) that

are the projections on to the respective coordinate axes.
– Could also be reprsented as a 2 or 3 vector P.
• Fundamental shape-defining primitive in most Graphics
APIs. Everything else is built from it!
• Represented using byte, short, int, float, double, etc.
• The scale and unit are application dependent.
Could be angstroms or lightyears!
• Points undergo transformations:
Translations, Rotations, Scaling, Shearing.
CSC251 Basics of Computer Graphics
3D Coordinates
– Vector P
– Cartesian: (x, y, z)
Z
– Polar: (ρ, θ, φ)
z
– z=
y= P
x= ρ
θ y
– ρ= Y
φ
φ= x
θ=
X
– Elevation: θ, Azimuthal: φ
CSC251 Basics of Computer Graphics
3D Coordinates
– Vector P
– Cartesian: (x, y, z)
Z
– Polar: (ρ, θ, φ)
z
– z = ρ cos θ,
y = ρ sin θ sin φ P
x = ρ sin θ cos φ ρ
θ y
2 2 2 2 Y
– ρ =x +y +z , φ
φ = tan−1(y/x),
p x
θ = tan ( x2 + y 2/z)
−1
X
– Elevation: θ, Azimuthal: φ
CSC251 Basics of Computer Graphics
Translation

Translation

## CSC251 Basics of Computer Graphics

2D Rotation
by θ.
– x′ =?, y ′ =?
– Matrix notation: P ′ = R P
 ′   
x ? ? x
=
y ? ? y

## CSC251 Basics of Computer Graphics

2D Rotation
by θ.
– x′ =?, y ′ =?
– Matrix notation: P ′ = R P
 ′   
x ? ? x
=
y ? ? y

## CSC251 Basics of Computer Graphics

2D Rotation
by θ.
– x′ = x cos θ − y sin θ,
y ′ = x sin θ + y cos θ.

– Matrix notation: P ′ = R P
 ′   
x cos θ − sin θ x
=
y sin θ cos θ y

## CSC251 Basics of Computer Graphics

2D Rotation: Observations
 
cos θ − sin θ
R=
sin θ cos θ
– Orthonormal: R−1 = RT
– Rows: vectors that
rotate to coordinate axes
– Cols: vectors coordinate
axes rotate to
– Invariants: distances,
angles, parallelism.

3D Rotations

## • Rotation could be about any axis in 3D! What does it

mean?
– Distance of each point to the axis of rotation remains
same.
– Each points moves in a circle on a plan perpendicular to
the axis of rotation, with the centre on the axis
• About Z-axis: Just like 2D rotation case. Z-coordinates
don’t change anyway.
• X-Y coordinates change exactly the same way as in 2D.
• CCW +ve, looking into the arrowhead: Rz (θ) =??
CSC251 Basics of Computer Graphics
3D Rotations
• Rotation could be about any axis in 3D!

## • About Z-axis: Z-coordinates don’t change anyway

 
cos θ −sin θ 0
Rz (θ) = sin θ cos θ 0
 
0 0 1

## • Rows: vectors that rotate onto axes; columns: vectors

that axes rotate into....
CSC251 Basics of Computer Graphics
3D Rotations
   
cos θ 0 sin θ 1 0 0
Ry =  0 1 0  Rx = 0 cos θ −sin θ
   
−sin θ 0 cos θ 0 sin θ cos θ

## • Rows: vectors that rotate onto axes; columns: vectors

that axes rotate into....

Coming soon ....

## CSC251 Basics of Computer Graphics

Non-uniform Scaling

## • Scale along X, Y, Z directions by s, u, and t.

• x′ = s x, y ′ = u y, z ′ = t z.

## • We are more comfortable with P ′ = S P or

 ′   
x s 0 0 x
 y  =  0 u 0  y 
    
z 0 0 t z

## • Invariants: parallelism, ratios of lengths in any direction

(Angles also for uniform scaling.)
CSC251 Basics of Computer Graphics
Shearing

## • Add a little bit of x to y or other combinations

 ′   
x 1 xy xz x
y = y 1 y z  y 
    
   x
z zx zy 1 z

Reflection

## • Negative entries in a matrix indicate reflection.

 ′   
x −1 0 0 x
 y  =  0 −1 0   y 
    
z 0 0 1 z

## CSC251 Basics of Computer Graphics

Summary of Transformations

## • Translation: New coordinates P′ = P + t

• Rotation: P′ = R P

• Scaling: P′ = S P

• Shearing: P′ = Sh P

• Reflection: P′ = Rf P

## CSC251 Basics of Computer Graphics

General Transformation

## • Rotation, scaling, shearing, and reflection: Matrix-vector

product. Vectors get tranformed into other vectors
• Translation alone is a vector-vector addition
• Sequence of: Translation, rotation, scaling, translation
and rotation: P′ = R2 [S R1 (P + t1) + t2]
• If translation is also a matrix-vector product, we can
combine above transformations into a single matrix:
P′ = R2 T2 S R1 T1 P = M P

## CSC251 Basics of Computer Graphics

Homogeneous Coordinates

## • Add a non-zero scale factor w to each coordinate.

A 2D point is represented by a vector [x y w]T
• [x y w]T ≡ (x/w, y/w).
• Simplest value of w is obviously 1
• Translate [x y]T by [a b]T to get [x + a y + b]T
    
x+a ? ? ? x
 y + b  =  ? ? ?  y 
    
1 ? ? ? 1

## CSC251 Basics of Computer Graphics

Homogeneous Coordinates

## • Add a non-zero scale factor w to each coordinate.

A 2D point is represented by a vector [x y w]T
• Translate [x y]T by [a b]T to get [x + a y + b]T
    
x+a 1 0 a x
 y + b  =  0 1 b  y 
    
1 0 0 1 1

## • Now, translation is also: P′ = T P, a matrix-vector

product and a linear operation.

## CSC251 Basics of Computer Graphics

Homogeneous Coordinates

## • Add a non-zero scale factor w to each coordinate.

A 2D point is represented by a vector [x y w]T
• [x y w]T ≡ (x/w, y/w).
• Now, translation is also: P′ = T P
• For a point: Rotation followed by translation followed by
scaling, followed by another rotation: P′ = R2STR1 P.
• Similarly for 3D. Points represented by: [x y z w]T .
• All matrices are 3 × 3 in 2D. Last row is [0 0 1].
• All matrices are 4 × 4 in 3D. Last row is [0 0 0 1].
CSC251 Basics of Computer Graphics
Homogeneous Representation

## • Convert to a 4-vector with a scale factor as fourth.

(x, y, z) ≡ [kx ky kz k]T for any k 6= 0.

## • Translation, rotation, scaling, shearing, etc. become

linear operations represented by 4×4 matrices.

## • [a b c d]T could be a point or a plane. Lines are specified

using two such vectors, either as join of two points or
intersection of two planes!

## CSC251 Basics of Computer Graphics

Transformation Matrices: Rotations
 
1 0 0 0
0 cos θ −sin θ 0
 
Rx(θ) = 
 
0 sin θ cos θ 0

0 0 0 1
   
cos θ 0 sin θ 0 cos θ −sin θ 0 0
0 1 0 0 sin θ cos θ 0 0
   
Ry =   Rz = 
 
−sin θ 0 cos θ 0  0 0 1 0

0 0 0 1 0 0 0 1

## • CCW +ve; orthonormal; length preserving; rows give

direction vectors that rotate onto each axis; columns ....

## CSC251 Basics of Computer Graphics

Translation, Scaling, Composite
   
1 0 0 a a 0 0 0
0 1 0 b 0 b 0 0
   
T (a, b, c) =  , S(a, b, c) = 
0 0 1 c 0 0 c 0

0 0 0 1 0 0 0 1

## • A sequence of transforms can be represented using a

composite matrix: M = RxTRy ST · · ·

• RT and TR??

## CSC251 Basics of Computer Graphics

Rotation and Translation
   
I t R 0
• T4×4 = and R4×4 =
0 1 0 1
  
I t R 0
• T R= = ?
0 1 0 1
  
R 0 I t
• RT = = ?
0 1 0 1

## CSC251 Basics of Computer Graphics

Rotation and Translation
   
I t R 0
• T4×4 = and R4×4 =
0 1 0 1
    
I t R 0 R t
• T R= =
0 1 0 1 0 1
    
R 0 I t R Rt
• RT = =
0 1 0 1 0 1

## CSC251 Basics of Computer Graphics

Rotation and Translation
   
I t R 0
• T4×4 = and R4×4 =
0 1 0 1
    
I t R 0 R t
• T R= =
0 1 0 1 0 1
    
R 0 I t R Rt
• RT = =
0 1 0 1 0 1
• T R = R T if: (a) R = I or (b) t = 0 or (c) Rt = t
• When is Rt = t? t is an eigenvector of R
• Question: Are transformations commutative?

Commutativity

## • Translations are commutative: T1T2 = T2T1

• Scaling is commutative: S1S2 = S2S1
?
• Are rotations commutative? R1R2 = R2R1
?
• Rotation and Scaling commute? SR = RS
• What would be an example?
Consider the effect on Z-axis of:

Commutativity

## • Translations are commutative: T1T2 = T2T1

• Scaling is commutative: S1S2 = S2S1
• Are rotations commutative? R1R2 6= R2R1
• Rotation and Scaling commute. SR = RS
• Consider the effect on Z-axis of
Rx(90)Ry (90) and Ry (90)Rx(90)
• RT 6= TR. (If translation is not parallel to rotation axis)
• Consider: R(π/4) and T (5, 0).
Where does the origin (0, 0) go in TR and RT?
CSC251 Basics of Computer Graphics
TR and RT

TR

RT

## TR keeps it on X axis to (5, 0). TR takes it to ( √52 , √52 ).

CSC251 Basics of Computer Graphics
Objects Away from Origin
• Object “translates” when
rotated or scaled!!
Rotated

## • Default: Perform these

• How do we transform
points “in place”?
• Rotate or scale about the Shape

• How?

## CSC251 Basics of Computer Graphics

– Align P with origin
– Translate back
• Rotation:
RC(θ) = T(C) R T(−C)
• Scaling:
SC() = T(C) S() T(−C)
• A transformation M:
MC = T(C) M T(−C)

Object

## CSC251 Basics of Computer Graphics

Object and Translation

′ ′
P =P P =T P
CSC251 Basics of Computer Graphics
Object, Translation, Rotation

′ ′
P =P P =R P
CSC251 Basics of Computer Graphics
Understanding Transformations

TR

RT

## CSC251 Basics of Computer Graphics

R, T Operations on Points

## • T(5,0) R(π/4): Impact on a point:

– R(π/4): (Point stays at (0, 0))
– T(5, 0) : (Point goes to (5, 0))

## • R(π/4) T(5,0): Impact on the point:

– T(5, 0): (Point moves to (5, 0))
– R(π/4). (Point rotates about origin)

## • All points on the shape undergo the same motions and

get new coordinates

## CSC251 Basics of Computer Graphics

Relating Coordinate Frames
• T (5, 0) and R(π/4)
• Start: Black axes TR
Next: Blue axes
Last: Red axes
RT

Black Blue
• P′ = | T | R P

Black Blue
• P′ = | R | T P

## CSC251 Basics of Computer Graphics

R, T Operaions on Frames

## • T(5,0) R(π/4): Impact on coordinate frame:

– T(5, 0): (Origin shifted to (5, 0))
– R(π/4). (Axes rotated at new origin)

## • R(π/4) T(5,0): Impact on coordinate frame:

– R(π/4): (Axes rotate by 45 degrees))
– T(5, 0). (Point moves to (5, 0) in new axes)

on objects!!

## CSC251 Basics of Computer Graphics

I Am Where I Think I Am (IAWITIA)

## • What am I? Different entities at different times.

student, friend, brother, enemy, daughter, neighbour, ...
• Let us stick to easier 3D geometry!
What are my coordinates (if I am a point)?
• Coordinates of a point depend on the viewpoint used
(similar to life; evaluation depends on the viewer)
• Nothing really “happens”. Nothing “moves”.
There are only changes in viewpoints (in geometry)!!
• IAWITIA: Pronounced ayA-wI-shia (rhymes with dementia)

## CSC251 Basics of Computer Graphics

IAWITIA in Action

• Consider P4 = M4M3M2M1 P0

## • Imagine the point having coordinates P1, P2, P3 after

operations M1, M2, M3

## • We can also visualize coordinate frames Π4, Π3, Π2, Π1, Π0

in which a point has coordinates P4 to P0 respectively

## • Operation Mi represents a change in coordinates from

Πi to Πi−1, resulting in new labels for the point.
CSC251 Basics of Computer Graphics
IAWITIA in TR

IAWITIA in RT

## CSC251 Basics of Computer Graphics

IAWITIA in Action in IIIT Campus

## • Model IIIT Campus as a whole. Campus is our “world”

• Global coordinate frame ΠG for the campus: at the Gate
• Buildings: Himalaya, Vindhya, Bakul, Parul, ..., Palash.
Each has a natural coordinate frame. ΠH is Himalaya’s
• Himalaya has several rooms: H105, H204, H304, etc.,
with own coordinate frames. ΠC is of H205 (our class)
• H205 has 75 desks, with coord frames ΠDi for desk i
• Desks are identical in geometry; the coord frame ΠDi
places each in its location.

Consider a Desk

## • Consider a corner point P of desk 37, with coordinates

(200, 30, 100) in ΠD37. That is: PD37 = (200, 30, 100)
• Since our world is the campus, we have to ultimately
describe everything in the coordinate frame ΠG
PG = MGH MHC MCD37 PD37
• MGH aligns ΠG to ΠH. MHC aligns ΠH to ΠC.
MCD37 aligns ΠC to ΠD37
PH PC PD37
• PG = MGH | MHC | MCD37 | P (for any point P on Desk37)

## • We can place a given desk in any building, room, place!

CSC251 Basics of Computer Graphics
Walking on Stage

## • Person walking horizontally on stage, with swinging arms

• How does the hand-tip move w.r.t each student? How?
• Student knows own position in room’s reference frame
• Start at a student’s eye. (That provides the viewpoint!)
• Align to room’s reference frame using M1. Different
matrix for each student, but everyone same now....
• Walk: pure translation. M2 aligns to person coord frame
• Arm swing: Simple harmonic motion with angle θ(t)
Simpler viewpoints in newer coord frames. IAWITIA !!
CSC251 Basics of Computer Graphics
Rolling Wheel
Coordinates P(t)?

P(t) = ??

## CSC251 Basics of Computer Graphics

P(t) = T1(r, θ(t)) P’(t)

Y’

## CSC251 Basics of Computer Graphics

P(t) = T1(r, θ)R(θ(t)) P”(t)

Y’
Y’

## CSC251 Basics of Computer Graphics

P(t) = T1RT2(p(t)) P”’

Y’
Y’
’ Y’’’

## CSC251 Basics of Computer Graphics

Final Transformation

## • P(t) = T1(t) R(θ(t)) T2(p(t)) P”’

• T1(t) = T(r θ(t), 0) = T(r ω t, 0) (A translation matrix)

Given an object

## • An object traingleObj is given. Can be drawn using

drawObject(triangleObj)

## • drawObject(triangleObj) draws the object at

(current) origin
CSC251 Basics of Computer Graphics
Draw it in a different configuration

Transformations

to

## • What are the transformations? Combination of

Translation, Rotation, Scaling!!

## CSC251 Basics of Computer Graphics

Which combination?
Envision and sketch the impact of each of:

1. S( 21 , 12 ) R(90) T(3, 2)

2. S( 21 , 12 ) T(3, 2) R(90)

3. T(3, 2) R(90) S( 21 , 12 )

4. T(3, 2) S( 21 , 12 ) R(90)

5. R(90) S( 21 , 12 ) T(3, 2)

6. R(90) T(3, 2) S( 21 , 12 )
CSC251 Basics of Computer Graphics
CSC251 Basics of Computer Graphics
Several Correct Situations

## CSC251 Basics of Computer Graphics

Another Situation
• A clock is hanging from a nail fixed to a flat plate. The
plate is being translated with a velocity ~v and acceleration
~a. The pendulum of the clock swings back and forth with
a time period of 5 seconds and a max angle of ±θ. An
ant travels from the bottom tip of the pendulum up to the
centre.

## • How do we compute the ant’s position with respect to a

fixed coordinate system coplanar with the plate?

## Please sketch the situation and work it out for

yourself
CSC251 Basics of Computer Graphics
A Transformation Problem

C B
A
D
D
B

## CSC251 Basics of Computer Graphics

Transformation Computation

## • Step 1: Translate by −D. What is the orientation of BC?

• Step 2: Rotate to have unit vector ~u = [ux uy ]T from B to
C on the Y axis. That is the second row of R matrix
• The matrix for the total operation: M = R T(−D)

• Two options for first row. [uy −ux]T and [−uy ux]T
   
uy −ux −uy ux
• R matrix: (a) or (b) ?
ux uy ux uy
• Difference? The direction aligned to the X-axis!
• Option (a) is correct. Why? Draw Option (b)!
CSC251 Basics of Computer Graphics
Rotation about an axis parallel to Z

## • Translate so that the axis passes through the origin:

T(−x, −y, k) for any k!!

## • Why shouldn’t k matter? RZ doesn’t affect the z

coordinate. So, whatever k is added first will be
subtracted later

## CSC251 Basics of Computer Graphics

Easy 3D Transformations
   
1 00 a a 0 0 0
0 10 b 0 b 0 0
T (a, b, c) = 
0
 , S(a, b, c) =  
01 c 0 0 c 0
0 00 1 0 0 0 1
 
1 0 0 0
0 cos θ −sin θ 0
Rx(θ) = 
0 sin θ

cos θ 0
0 0 0 1
   
cos θ 0 sin θ 0 cos θ −sin θ 0 0
 0 1 0 0  sin θ cos θ 0 0
Ry =   Rz =  
−sin θ 0 cos θ 0  0 0 1 0
0 0 0 1 0 0 0 1

## • CCW +ve; orthonormal; length preserving; rows give

direction vectors that rotate onto each axis; columns ....

## CSC251 Basics of Computer Graphics

3D Rotation about an axis α

• What is Rα(θ)?

## CSC251 Basics of Computer Graphics

3D Rotation about an axis α

• What is Rα(θ)?

• 3-step process:
1. Apply Rαx to align α with the X axis.
2. Rotate about X by angle θ.
1
3. Undo the first rotation using R−
αx
1
• Net result: Rα(θ) = R−
αx Rx (θ) Rαx

## • (We can align α with Y or Z axis also)

CSC251 Basics of Computer Graphics
Z

β
α Y

γ
X P

Computing Rα

## • First rotate by −β about X axis. Vector α would lie in the

XY plane, with tip at point P.

• β = ?, tan β = ?

## • Next rotate by −γ about Z axis. Vector α would coincide

with the X axis.

• γ = ?, tan γ = ?

Computing Rα

αz
• tan β = αy

## • Rotate by −γ about Z axis to bring α to X axis

√ 2 2 √
αy +αz 1−α2x
• tan γ = αx = αx if |α| = 1.

1
• Rαx = Rz(−γ)Rx(−β) and R−
αx = Rx(β)Rz(γ)

## • Alternative: Don’t we know about rotation matrices and

direction cosines that go to/from coordinate axes?
CSC251 Basics of Computer Graphics
Final

Z

β
α Y

γ
X P

## CSC251 Basics of Computer Graphics

Alternate Rαx
• After rotation, α will align with X-axis. Hence that is the
first row r1 of the rotation matrix

## • Take any vector v not parallel to α. r2 = α × v will work!!

 
α 0
 α×v 0 
 
• Lastly, r3 = r1 × r2 and Rαx = 
 r1 × r2 0 

0 1

## • Many possibilities, all with the same result (hopefully...)

CSC251 Basics of Computer Graphics
Example: Rotation about [1 1 1]T

(0, 1, 1)

(1, 0, 1) (1, 1, 1)

(1, 1, 0) (1, 2 , 0)

β = ?, γ=?

## CSC251 Basics of Computer Graphics

Example: Rotation about [1 1 1]T

(0, 1, 1)

(1, 0, 1) (1, 1, 1)

(1, 1, 0) (1, 2 , 0)

tan β = 1, tan γ = 2

## CSC251 Basics of Computer Graphics

Computing Rαx: Method 1
 
1 0 0
π  0
• Rotate by −π/4 about X. RX(− 4 ) =  √1 √1 
2 2 
0 −1
√ √1
2 2
 1 2

√ √ 0
√ 3
 −2 √1 6
• RZ(− arctan( 2)) =  √ 0 

6 3
0 0 1
 
√1 √1 √1 0
3 3 3
 
√  −2
√ √1 √1 0 
I −1 π 6 6 6
• Rαx = RZ(−tan ( 2)) RX(−4 ) = 
 
√1
−1

 0 √ 0 
 2 2 
0 0 0 1

## CSC251 Basics of Computer Graphics

Computing Rαx: Method 2

3 3

]
2 2

## • Third row: r3 = α × [0 √1 √−1 T

] = [ √26 −1 √
√ −1 T
]
2 2 6 6
 
√1 √1 √1 0
3 3 3
 
 0 √1 −1
√ 0  √
2 2
• RII =  2  = RY (tan ( 2)) RX( π4 )
−1
 
αx −1 −1
 √
 6

6

6
0 

0 0 0 1

## CSC251 Basics of Computer Graphics

Rαx Method 2: Contd

(1, 0, 2) (0, 1, 1)

(1, 0, 1) (1, 1, 1)

(1, 1, 0)

## CSC251 Basics of Computer Graphics

Rotation: Arbitrary Axis, Point

• Apply Rα.

## CSC251 Basics of Computer Graphics

3D Transformations

## • Ultimately, there is only one transform given the starting

and ending configurations.

## • Different methods let us analyze the problem from

different perspectives.

Another Example
Z Z

B
A
C
B’
Y A’ Y
C’

X X

## CSC251 Basics of Computer Graphics

Working the Example

## • Translate by -A to bring it to the origin.

• After the rotation, AC sits on the X axis.
• The first row of the rotation matrix is AC / |AC|.
• The vector normal to the plane ABC sits on the Y axis.
• The second row of the rotation matrix is the unit vector
along AB × AC = (AB × AC) / |AB × AC|.
• Third row is a cross product of the first two.

## CSC251 Basics of Computer Graphics

Transforming Lines

## • A composite transformation can be seen as changing

points in the coordinate system.
• These transformations preserve collinearity. Thus, points
on a line remain on a (transformed) line.
• Take two points on the line, transform them, and compute
the line between new points.
• Lines are defined as a join of 2 points or intersection of 2
planes in 3D. The same holds for transformed lines using
transformed points or planes!

## CSC251 Basics of Computer Graphics

Transforming Planes

## • A plane is defined by a 4-vector n (called the normal

vector) in homogeneous coordinates.
• The plane consists of points p such that nTp = 0.
• Let Q transform n when points are transformed by M.
• Coplanarity is preserved: (Qn)TMp = 0 = nTQTMp.
• True when QTM = I, or Q = M−T.
• Q is the Matrix of cofactors of M in the general case when
M−1 doesn’t exist.

## CSC251 Basics of Computer Graphics

Understanding Geometric
Transformations

## • Geometry transformation of objects is very important to

compose graphics environments

## • Understand what you want to be achieved, visualize it in

your mind and compose the series of transformations

## • Needs getting used to the ideas. Think about getting into

a simpler situtation from the current one.