Lecture 4 Karanja On 2D Transformation 1
Lecture 4 Karanja On 2D Transformation 1
Karanja Mwangi
Lesson objective
Rotate
If you rotate a rectangle, it is still a rectangle
Basics about Vertices and matrices -1
x' a b x x' a b c x
y ' c d y y ' d e f y
z ' g h i z
x' ax by x' ax by cz
y ' dx ey fz
y ' cx dy
z ' gx hy iz
1 0 0
1 0
I I 0 1 0
0 1 0 0 1
Practice
1 7 3 3
A ,B
2 4 5 2
AB ?
Practice (You got this?)
38 17
AB ,
26 14
2D Translation
(x’,y’)
(tx,ty)
(x,y)
2D Translation
2D Translation
To move a line segment, apply the transformation
equation to each of the two line endpoints and redraw
the line between new endpoints
To move a polygon, apply
x' the transformation equation
to coordinates of each vertex and regenerate the
polygon using the new set of vertex coordinates
x ' 3 7
x ' x t x y ' 4 8
y ' y t
y x' 3
P' P T y' 4
Translation Exercises
Rotation angle
yr θ
xr
14
2D Translation-2
2D Rotation
If θ is positive counterclockwise rotation
If θ is negative clockwise rotation
Remember:
• cos(a + b) = cos a cos b - sin a sin b
• cos(a - b) = cos a sin b + sin a cos b
15
2D Translation-2
P ( x, y )
R ( )
x' r cos cos r sin sin
x r cos
y ' r cos sin r sin cos
y r sin
x' x cos y sin
x' r cos( )
y ' x sin y cos
y ' r cos( )
x' cos sin x
(x’,y’) y ' sin cos y
r θ P' R P
r (x,y)
Φ
16
2D Translation-3
Rotation of a point about any specified position (xr,yr)
x’=xr+(x - xr) cos θ – (y - yr) sin θ
y’=yr+(x - xr) sin θ + (y - yr) cos θ
17
2D Translation-4
2D Rotation
(x’,y’)
P’=R·P
θ
cos sin r
r (x,y)
R
sin cos Φ
18
2D rotation Exercises-1
cos sin
R
sin cos
2D rotation Exercises-2
Xnew Ynew
• = Xold x cosθ – Yold x sinθ • = Xold x sinθ + Yold x cosθ
• = 4 x cos30º – 4 x sin30º • = 4 x sin30º + 4 x cos30º
• = 4 x (√3 / 2) – 4 x (1 / • = 4 x (1 / 2) + 4 x (√3 /
2) 2)
• = 2√3 – 2 • = 2 + 2√3
• = 2(√3 – 1) • = 2(1 + √3)
• = 2(1.73 – 1) • = 2(1 + 1.73)
• = 1.46 • = 5.46
Thus, New ending
coordinates of the line
after rotation = (1.46,
5.46).
2D rotation Exercises-1
2D rotation Exercises-2
cos sin
R
sin cos
2D rotation Exercises-2
x' s x x x ' 3 *1
• We want: y' s y y y ' 3 * 2.
x ' 3 0 1
y ' 0 3 2
x' s x 0 x
y ' 0 s y y x' 3
• Matrix form:
P' S P y' 6
2D Scaling -3
• 2D Scaling
• We can control the location of the scaled object by
choosing a position called the fixed point (xf,yf)
• x’ – xf = (x – xf) sx y’ – yf = (y – yf) sy
• x’=x · sx + xf (1 – sx)
• y’=y · sy + yf (1 – sy)
• Polygons are scaled by applying the above formula to
each vertex, then regenerating the polygon using the
transformed vertices
26
Combining Transformations
Translation
T(tx, ty)
Translation distances
Scale
S(sx,sy)
Scale factors
Rotation
R()
Rotation angle
Combining Transformations-3
P' P T
P' R P
P' S P
Homogeneous Coordinates
A point(x, y) can be re-written in homogeneous coordinates as
(xh, yh, h)
The homogeneous parameter h is a non- zero value such that:
yh
x
xh y
h h
We can then write any point
(x, y) as (hx, hy, h)
We can conveniently choose h = 1 so that
1 0 t x
1
T 1
0 1 t y
0 0 1
T *T I
Two-Dimensional Rotation matrix
cos sin 0
1
R 1 sin cos 0
R *R I
0 0 1
Two-Dimensional Scaling matrix
1
s 0 0
x 1
S 1 0
1
0 S *S I
sx
0 0 1
Final Transformations -
Compare Equations
x' 1 0 t x x
x ' t x x
T t x , t y y ' 0 1 t y y
y ' t y y 1 0 0 1 1
P T t x , t y P P T t x , t y P
x' s x 0 0 x
x' s x 0 x
S s x , s y y ' 0 sy 0 y
y ' 0 s y y 1 0 1 1
0
P S s x , s y P P S s x , s y P
x' cos sin 0 x
x' cos sin x
R y ' sin cos 0 y
y ' sin cos y 1 0 1 1
0
P R P P R P
Matrix Operations on OpenGL
glMatrixMode(.);
Projection Mode: Determines how the scene is projected onto
the screen
Modelview Mode: Used for storing and combining geometric
transformations
Texture Mode: Used for mapping texture patterns to surfaces
Color Mode: Used to convert from one color mode to another
OpenGL Matrix Operations
37
OpenGL Matrix Operations (cont.)
Alternatively:
glLoadMatrix* (elements16);
To assign other values to the elements of the current matrix
In column-major order:
• First four elements in first column
• Second four elements in second column
• Third four elements in third column
• Fourth four elements in fourth column
38
OpenGL Matrix Operations (cont.)
39
OpenGL Transformation Functions
Summary