Computer Graphics Mustansiriyah university
Third stage 2020-2021 Education college
Computer science department
Matrix representation of transformation
Many graphic applications involves sequence of geometric
transformations. For example , animation transformation which is
require an object to be translated and rotated at each increment of
the motion.
Transformation can be represented as a product of the row
vector [x,y] and a 2x2 matrix accept for the translation.
Transformations can be combined using matrix multiplication
𝑥′ 𝑎 𝑏 𝑒 𝑓 𝑖 𝑗 𝑥
[ ]=[ ][ ] [ ] [ ]
𝑦′ 𝑐 𝑑 𝑔 ℎ 𝑘 𝑙 𝑦
Matrices are convenient to represent a sequence of
transformations
1- Translation matrix T(tx , ty)
We can represent the translation transformation as follows:
𝑥 𝑡𝑥 𝑥′ 𝑥 𝑡𝑥
′
P’ = P+T, 𝑃 = [𝑦], 𝑇 = [𝑡 ] −−→ 𝑃 = [ ′ ] = [ 𝑦] + [𝑡𝑦 ]
𝑦 𝑦
12
Computer Graphics Mustansiriyah university
Third stage 2020-2021 Education college
Computer science department
2- Scaling matrix
𝑥
If a point P [𝑦] is being a 2x1 vector. If we multiply it by 2x2
𝑆 0
matrix S=[ 0𝑥 𝑆 ]. We will obtain another 2x1 vector which we
𝑦
can interpret as another point:
P’= S . P
𝑥′ 𝑆𝑥 0 𝑥
[ ′ ]= [ 0 𝑆𝑦 𝑦 ]
] . [
𝑦
What will happen if we transfer every point by means of
multiplication by S and display the result:
1 0
1- If S is the Identity matrix: S=[ ] No change
0 1
2 0 𝑥′ 2 0 𝑥 2𝑥
2- If S=[ ] then [𝑦 ′ ]= [ ] . [𝑦 ]= [ 𝑦 ]
0 1 0 1
That mean:
- every new x coordinate would be twice as large as the old
value of vertical lines.
- x coordinate would be twice as width and the same tall.
0.5 0
3- If S= [ ] shrink all x coordinate (shrink the width with
0 1
the same tall)
Examples:
1- Stretch the image/object to twice and then compress it to one-
half of the new width?
P’= (S1S2). P
2 0 0.5 0 1 0 𝑥
S1S2=[ ][ ]= [ ].[ ] identity matrix then no
0 2 0 0.5 0 1 𝑦
change
13
Computer Graphics Mustansiriyah university
Third stage 2020-2021 Education college
Computer science department
2- Make an image twice as width and twice as tall?
P’= (S1S2). P
2 0 1 0 2 0 𝑥
(S1S2 ) . P=[ ][ ]. P −−→ [ ]. [ ]
0 1 0 2 0 2 𝑦
2- Rotation matrix
If a line segment have two endpoints (0,0) and (x,y), and length
𝐿 = √𝑥 2 + 𝑦 2 :
- The ratio of the height of the (x,y) endpoint with x-axis have
the y coordinates value and the length of the segment will be
𝑦
the Sin of the angle: Sin(𝜃 ) = 2 2
√𝑥 +𝑦
- The ratio of the distance of the (x,y) endpoint with y-axis have
the x coordinates value and the length of the segment will be
𝑥
the Cosine of the angle: Cos(𝜃 ) = 2 2
√𝑥 +𝑦
- If L=1 then Sin(𝜃)=y and Cos(𝜃)=x
- If we rotate the point (1,0) in counterclockwise by an angle 𝜃, it
becomes (Cos(𝜃), Sin(𝜃)) so:
cos(𝜃 ) 𝑎 𝑏 1 𝑎
[ ]= [ ] [ ]=[ ]
sin(𝜃) 𝑐 𝑑 0 𝑐
14
Computer Graphics Mustansiriyah university
Third stage 2020-2021 Education college
Computer science department
- If we rotate the point (0,1) in counterclockwise by an angle 𝜃, it
becomes (-Sin(𝜃 ), Cos(𝜃)) so:
−sin(𝜃 ) 𝑎 𝑏 0 𝑏
[ ]= [ ] [ ]=[ ]
cos(𝜃) 𝑐 𝑑 1 𝑑
From these equations we can see the values of a,b,c,d needed to
form the rotation matrix:
𝐶𝑜𝑠(𝜃 ) − 𝑆𝑖𝑛(𝜃)
R=[ ]
𝑆𝑖𝑛(𝜃) 𝐶𝑜𝑠(𝜃)
Example:
Rotate the point (2,3) in counterclockwise by an angle 𝜋/6 ?
The rotation matrix is:
𝜋
𝐶𝑜𝑠 ( ) − 𝑆𝑖𝑛(𝜋/6) 0.866 − 0.5
R= [ 6 ]= [ ]
𝑆𝑖𝑛(𝜋/6) 𝐶𝑜𝑠(𝜋/6) 0.5 0.866
Then the rotated point would be :
P’ = R . P
0.866 − 0.5 2 3.232
P’= [ ] [ ]= [ ]
0.5 0.866 3 3.598
Notes:
- We can rotate an entire line segment by rotating both
endpoints which specify it.
15
Computer Graphics Mustansiriyah university
Third stage 2020-2021 Education college
Computer science department
- The rotation matrix for an angle 𝜃 in clockwise would be:
𝐶𝑜𝑠(−𝜃 ) − 𝑆𝑖𝑛(−𝜃) 𝐶𝑜𝑠(𝜃 ) 𝑆𝑖𝑛(𝜃)
[ ]=[ ]
𝑆𝑖𝑛(−𝜃) 𝐶𝑜𝑠(−𝜃) −𝑆𝑖𝑛(𝜃) 𝐶𝑜𝑠(𝜃)
H.W-1\: Rotate the triangle (5,2),(1,1),(0,0) by rotation in
counterclockwise with angle 45° about origin?
H.W-2\
a- Find the matrix that represent rotation of an object by 30° about
the origin?
b- what are the new coordinates of the point p(2,-4) after the
rotation?
Homogenous coordinate in transformation matrix
Why Homogeneous Coordinates?
1. Mathematicians commonly use homogeneous coordinates as
they allow scaling factors to be removed from equations.
2. Using homogeneous coordinates allows us use matrix
multiplication to calculate transformations – extremely
efficient!
3. Homogeneous coordinates seem unintuitive, but they make
graphics operations much easier.
4. Since a 2x2 matrix representation of translation does not
exist!!. So by using a homogenous coordinate system then we
can represent 2x2 translation transformation as a matrix
multiplication.
5. It provides a consistent, uniform way of handling affine
transformations. 2D affine transformations always have a
bottom row of [0 0 1].
An “affine point” is a “linear point” with an added w-
coordinate which is always 1:
16
Computer Graphics Mustansiriyah university
Third stage 2020-2021 Education college
Computer science department
𝑥
plin
paff = [ ] = [𝑦]
1
1
Applying an affine transformation gives another affine point.
A point (x, y) can be re-written in homogeneous coordinates as
(xw, yw,w)
- The homogeneous parameter w is a non-
zero value such that x and y coordinates can easily be
recovered by dividing the first and second numbers by the
third.
𝑥𝑤 𝑦𝑤
𝑥= 𝑦=
𝑤 𝑤
- We can then write any point (x, y) as :
𝑥 𝑤. 𝑥
[𝑦] = [𝑤. 𝑦] , 𝑤 ≠ 0
𝑤
- We can conveniently choose w = 1 so that
(x, y) becomes:
𝑥 𝑥
[𝑦] = [𝑦]
1
In homogeneous coordinates the scaling matrix as follows:
𝑆𝑥 0 0
𝑆𝑥 0
[ 0 𝑆𝑦 ] [0 𝑆𝑦 0]
0 0 1
𝑆𝑥 0 0 𝑥𝑤 𝑆𝑥 𝑥𝑤
𝑆𝑥 𝑥
[0 𝑆𝑦 0] [𝑦𝑤]=[ 𝑆𝑦 𝑦𝑤 ] divide by w then [ 𝑆 𝑦] is
𝑦
0 0 1 𝑤 𝑤
the correctly scaled point.
The counterclockwise rotation matrix is
𝐶𝑜𝑠(𝜃 ) − 𝑆𝑖𝑛(𝜃)
[ ]
𝑆𝑖𝑛(𝜃) 𝐶𝑜𝑠(𝜃)
17
Computer Graphics Mustansiriyah university
Third stage 2020-2021 Education college
Computer science department
𝐶𝑜𝑠(𝜃) −𝑆𝑖𝑛(𝜃) 0
Using homogeneous coordinates we get: [ 𝑆𝑖𝑛(𝜃) 𝐶𝑜𝑠(𝜃) 0]
0 0 1
Applying it to the point (x,y) with homogeneous (xw,yw,w) gives:
𝐶𝑜𝑠(𝜃) −𝑆𝑖𝑛(𝜃) 0 𝑥𝑤
𝑅. 𝑃 = [ 𝑆𝑖𝑛(𝜃) 𝐶𝑜𝑠(𝜃) 0] [𝑦𝑤]
0 0 1 𝑤
The homogeneous coordinate translation matrix of tx , ty is
1 0 𝑡𝑥
𝑇 = [ 0 1 𝑡𝑦 ]
0 0 1
1 0 𝑡𝑥 𝑥𝑤 (𝑥𝑤 + 𝑡𝑥 𝑤)
𝑇. 𝑃 = [ 0 1 𝑡𝑦 ] [𝑦𝑤]=[ (𝑦𝑤 + 𝑡𝑦 𝑤) ]
0 0 1 𝑤 𝑤
𝑥 + 𝑡𝑥
divide by w then we get the point P=[ 𝑦 + 𝑡𝑦 ] is the correctly
translated point.
In general:
1- Translation
𝑥̅ 1 0 𝑡𝑥 𝑥
[𝑦̅] = [ 0 1 𝑡𝑦 ] [𝑦]
1 0 0 1 1
2- Rotation
𝑥̅ 𝐶𝑜𝑠(𝜃) −𝑆𝑖𝑛(𝜃) 0 𝑥
[𝑦̅] = [ 𝑆𝑖𝑛(𝜃) 𝐶𝑜𝑠(𝜃) 0] [𝑦]
1 0 0 1 1
18
Computer Graphics Mustansiriyah university
Third stage 2020-2021 Education college
Computer science department
3- Scaling
𝑥̅ 𝑆𝑥 0 0 𝑥
[𝑦̅] = [ 0 𝑆𝑦 0] [𝑦]
1 0 0 1 1
Rotation about an arbitrary point
- The homogeneous coordinate transformation matrix for
counterclockwise rotation about point (𝑥𝑐 , 𝑦𝑐 ) is done by three
steps as follows:
1- Translation to the origin
1 0 −𝑥𝑐
𝑇1 =[0 1 −𝑦𝑐 ],
0 0 1
𝑥̅ 1 0 −𝑥𝑐 𝑥
[𝑦̅] = [ 0 1 −𝑦𝑐 ] [𝑦]
1 0 0 1 1
2- Rotation about the origin
𝐶𝑜𝑠(𝜃) −𝑆𝑖𝑛(𝜃) 0
R=[ 𝑆𝑖𝑛(𝜃) 𝐶𝑜𝑠(𝜃) 0]
0 0 1
𝑥̿ 𝐶𝑜𝑠(𝜃) −𝑆𝑖𝑛(𝜃) 0 𝑥̅
[ 𝑦̿] = [ 𝑆𝑖𝑛(𝜃) 𝐶𝑜𝑠(𝜃) 0] [𝑦̅]
1 0 0 1 1
3- Translation back to its correct position
1 0 𝑥𝑐
𝑇2 =[ 0 1 𝑦𝑐 ]
0 0 1
𝑥̅̿ 1 0 𝑥𝑐 𝑥̿
[ 𝑦̿̅] = [ 0 1 𝑦𝑐 ] [ 𝑦̿]
1 0 0 1 1
19
Computer Graphics Mustansiriyah university
Third stage 2020-2021 Education college
Computer science department
Composite Transformations
- Matrices are a convenient and efficient way to represent a
sequence of transformations.
- Matrix multiplication is not commutative so that the order of
transformations is important.
- What if we want to rotate and translate?
𝑥𝑤
To rotate a point (𝑇2 (R (𝑇1 [𝑦𝑤])) )
𝑤
- Now we must form an overall transformation matrix as
follows:-
𝑥𝑤
(𝑇2 (R 𝑇1 )) [𝑦𝑤]
𝑤
1 0 𝑥𝑐 𝐶𝑜𝑠(𝜃) −𝑆𝑖𝑛(𝜃) 0 1 0 −𝑥𝑐
𝑇2 R 𝑇1 = [0 1 𝑦𝑐 ] [𝑆𝑖𝑛(𝜃) 𝐶𝑜𝑠(𝜃) 0] [0 1 −𝑦𝑐 ]
0 0 1 0 0 1 0 0 1
Order of transformations
1 0 𝑥𝑐 𝐶𝑜𝑠(𝜃) −𝑆𝑖𝑛(𝜃) −𝑥𝑐 Cos(𝜃) + 𝑦𝑐 𝑆𝑖𝑛(𝜃)
= [0 1 𝑦𝑐 ] [𝑆𝑖𝑛(𝜃) 𝐶𝑜𝑠(𝜃) −𝑥𝑐 sin(𝜃) − 𝑦𝑐 𝐶𝑜𝑠(𝜃)]
0 0 1 0 0 1
𝐶𝑜𝑠(𝜃) −𝑆𝑖𝑛(𝜃) −𝑥𝑐 𝐶𝑜𝑠(𝜃) + 𝑦𝑐 𝑆𝑖𝑛(𝜃) + 𝑥𝑐
=[𝑆𝑖𝑛(𝜃) 𝐶𝑜𝑠(𝜃) −𝑥𝑐 𝑆𝑖𝑛(𝜃) − 𝑦 𝐶𝑜𝑠(𝜃) + 𝑦 ]
𝑐 𝑐
0 0 1
𝐶𝑜𝑠(𝜃) −𝑆𝑖𝑛(𝜃) 𝑥𝑐 (1 − 𝐶𝑜𝑠(𝜃)) + 𝑦𝑐 𝑆𝑖𝑛(𝜃)
=[𝑆𝑖𝑛(𝜃) 𝐶𝑜𝑠(𝜃) 𝑦 (1 − 𝐶𝑜𝑠(𝜃)) − 𝑥𝑐 𝑆𝑖𝑛(𝜃) ]
𝑐
0 0 1
20
Computer Graphics Mustansiriyah university
Third stage 2020-2021 Education college
Computer science department
Example: Rotate line segment by 45 degrees about endpoint a and
note that tx=3?
1 0 3 cos(45) sin(45) 0 1 0 3 ax a'x
0 1 0 sin(45) cos(45) 0 0 1 0 ay a' y
0 0 1 0 0 1 0 0 1 1 1
H.W\ Rotate the triangle (5,2),(1,1),(0,0) by rotation in
counterclockwise with angle 45° about fixed point (-1,-1)?
21
Computer graphics 2018-2019 2D Viewing and Clipping
Window to Viewport Mapping
Window:
1. A world-coordinate area selected for display is called a
window.
2. In computer graphics, a window is a graphical control element.
3. It consists of a visual area containing some of the graphical
user interface of the program it belongs to and is framed by a
window decoration.
4. A window defines a rectangular area in world coordinates. You
can define the window to be larger than, the same size as, or
smaller than the actual range of data values, depending on
whether you want to show all of the data or only part of the
data.
Viewport:
1. An area on a display device to which a window is mapped is
called a viewport.
2. A viewport is a polygon viewing region in computer graphics.
The viewport is an area expressed in rendering-device-specific
coordinates, e.g. pixels for screen coordinates, in which the
objects of interest are going to be rendered.
3. A viewport defines in normalized coordinates a rectangular
area on the display device where the image of the data appears.
You can have your graph take up the entire display device or
show it in only a portion, say the upper-right part.
1
Computer graphics 2018-2019 2D Viewing and Clipping
Window to viewport transformation
1. Window-to-Viewport transformation is the process of
transforming a two-dimensional, world-coordinate scene to
device coordinates.
2. In particular, objects inside the world or clipping window are
mapped to the viewport. The viewport is displayed in the
interface window on the screen.
3. In other words, the clipping window is used to select the part
of the scene that is to be displayed. The viewport then positions
the scene on the output device.
4. Example:
2
Computer graphics 2018-2019 2D Viewing and Clipping
This transformation involves developing formulas that start with a
point in the world window, say (x, y).
3
Computer graphics 2018-2019 2D Viewing and Clipping
Example1:
A normalized window has left and right boundaries of (-0.05 to
+0.05) and lower and upper boundaries of (0.1 to 0.2). the
viewport window left and right is (250,550) and lower to upper
is (100,400),find the coordinate of any point (u,v) in the
viewport window.
Solution
Window( xmin=-0.05 , xmax=+0.05 , ymin=0.1, ymax=0.2)
Viewport ( umin=250, umax=550, vmin=100, vmax=400)
u=
c1= =300/0.1=3000
c2=umin-c1xmin
=250-3000(-0.05) =250+150 =400
u=3000x+400
v=d1y+d2
=300/0.1=3000
d2=vmin-d1ymin
=100-3000(0.1)
=-200
v=3000y-200
4
Computer graphics 2018-2019 2D Viewing and Clipping
Example2:
A normalized window has left(xmin=10) and right(xmax=50)
boundaries and lower(ymin=5) and upper(ymax=30)
boundaries .the viewport window left(umin=25) and
right(umax=75) and lower(vmin=25) to upper (vmax=75) find
the coordinate of any point (u,v) in the viewport window.
u=
c1 =75-25/50-10
c1=50/40=1.25
c2=umin-c1xmin
=25-1.25*10
= 25-12.5=12.5
u=1.25x+12.5
v=d1y+d2
=75-25/30-5
d1=50/25=2
d2= vmin-d1ymin
=25-2*5
=25-10=15
v=2y+15
5
Computer graphics 2018-2019 2D Viewing and Clipping
Window to Viewport Transformation N
We can express these two formula for computing (u,v) from (x,y) by
term:
(translate-scale-translate)
[ ] [ ]
N=T2ST1
1. T1 is the translation matrix about window origin :
[ ]
2. S is the scaling transformation matrix:
[ ]
3. T2 is the translation matrix position of the viewport :
[ ]
6
Computer graphics 2018-2019 2D Viewing and Clipping
Example1:
A normalized window has left and right boundaries of (-0.05 to
+0.05) and lower and upper boundaries of (0.1 to 0.2). the
viewport window left and right is (250,550) and lower to upper
is (100,400),find the transformation N.
Solution N=T2ST1
[ ]
[ ]
[ ]
[ ]
[ ]
[ ]
[ ][ ][ ]
7
Computer graphics 2018-2019 2D Viewing and Clipping
Example 2
Specify individually the translation and scaling matrices
required to transform a 2D window of [Xmin=-234, Ymin=156]
and [Xmax=66, Ymax=456] to a display viewport of [Umin=45,
Vmin=35] and [Umax=245, Vmax=185].
Solution N=T2ST1
[ ]
[ ]
[ ]
[ ]
[ ]
8
Computer graphics 2018-2019 2D Viewing and Clipping
[ ]
[ ]
[ ][ ][ ]