Ch5 Grap Lecture
Ch5 Grap Lecture
By: Getaneh T.
E-Mail: [email protected]
1
INTRODUCTION
Transformation means changing some graphics
into something else by applying rules.
When a transformation takes place on a 2D
2
WHY TRANSFORMATIONS
NEEDED?
Transformation are used to position objects , to shape object , to
change viewing positions , and even how something is viewed.
In simple words transformation is used for
1) Modeling
2) viewing
TYPES OF TRANSFORMATIONS
There are two types of transformation in
computer graphics.
1) 2D transformation
2) 3D transformation
Types of 2D and 3D transformation
1) Translation
2) Rotation
3) Scaling
5
TRANSLATIONS
A translation moves an object to a different
position on the screen. You can translate a
point in 2D by adding translation coordinate
(tx, ty) to the original coordinate (X, Y) to get
the new coordinate (X’, Y’).
6
TRANSLATION
A translation moves all points in
an object along the same
straight-line path to new
positions.
The path is represented by a P'(8,6)
vector, called the translation or
shift vector.
We can write the components: ty=4
p'x = px + tx
p'y = py + ty P(2, 2) tx = 6
or in matrix form:
P' = P + T
x' x tx
y' = y + ty 7
EXAMPLE
1. P=(2,4), T=(-1,14), P’=(?,?)
2. P=(8.6,-1), T=(0.4,-0.2), P’=(?,?)
3. P=(3,1), T=(?,?), P’=(5,7)
4. Consider the triangle defined by its three vertices A(20,0),
B(60,0), C(40,100) by translating vector (100, 10) and
draw the translated triangle
8
ROTATION
In rotation, we rotate the object at particular
angle θ (theta) from its origin. From the
following figure, we can see that the point P(X,
Y) is located at angle φ from the horizontal X
coordinate with distance r from the origin.
Let us suppose you want to rotate it at the
9
Rotation
• Review Trigonometry
=> cos = x/r , sin = y/r
• x = r. cos , y = r.sin
P’(x’, y’)
P’(x’, y’)
• or in matrix form:
P' = R • P
can be clockwise (-ve) or
counterclockwise (+ve as our
example). y’
P(x,y)
• Rotation matrix
r
cos sin y
R
sin cos x’ x
11
ROTATION
Example
Find the transformed point, P’, caused by
rotating
P= (5, 1) about the origin through an angle of
90.
cos sin x x cos y sin
sin
cos y x sin y cos
5 0 1 1
5 1 1 0
1
5 12
EXAMPLE
13
Scaling
• Scaling changes the size of an
object and involves two scale
factors, Sx and Sy for the x-
P’
and y- coordinates
respectively.
• Scales are about the origin.
p'x = sx • px P
p'y = sy • py
or in matrix form:
P' = S • P
Scale matrix as:
sx 0
S
s y
14
0
•
Scaling
If the scale factors are in between
0 and 1 the points will be moved
P’
closer to the origin the object
will be smaller.
• Example :
•P(2, 5), Sx = 0.5, Sy = 0.5
P(2, 5)
16
EXAMPLE
Scale the points p1(1,1), p2(1,4) and p3(4,3)
using scaling factors sx=0.5, sy=0.5 and draw
the scaled triangle.
17
3D TRANSFORMATION
Transformations are a fundamental part of the computer graphics.
Transformations are the movement of the object in Cartesian
plane(2D or 3D plane) .
3D TRANSFORMATION
When the transformation takes place on a 3D plane .it is called
3D transformation.
Generalize from 2D by including z coordinate
Straight forward for translation and scale, rotation more difficult
x 1 0 0 tx x
y 0 1 0 t y y
z 0 0 1 tz z
1 0 0 0 1 1
3D TRANSLATION
The matrix representation is equivalent to the three equation.
x’=x+ tx , y’=y+ ty , z’=z+ tz
Where parameter tx , ty , tz are specifying translation distance for the
coordinate direction x , y , z are assigned any real value.
Example:Given a 3D object with coordinate
points A(0, 3, 1), B(3, 3, 2), C(3, 0, 0), D(0, 0, 0).
Apply the translation with the distance 1 towards
X axis, 1 towards Y axis and 2 towards Z axis and
obtain the new coordinates of the object.
Solution-
Given-
•Old coordinates of the object = A (0, 3, 1), B(3, 3,
2), C(3, 0, 0), D(0, 0, 0)
•Translation vector = (Tx, Ty, Tz) = (1, 1, 2)
•Thus, New coordinates of the object = A (1, 4, 3),
B(4, 4, 4), C(4, 1, 2), D(1, 1, 2). 22
3D ROTATION
Where an object is to be rotated about an axis that is parallel to one of
the coordinate axis, we can obtain the desired rotation with the
following transformation sequence.
Coordinate axis rotation
Z- axis Rotation
Y-axis Rotation
X-axis Rotation
COORDINATE AXIS ROTATION
Obtain rotations around other axes through cyclic permutation of
coordinate parameters:
x y z x
X-AXIS ROTATION
The equation for X-axis rotation
x’ = x
y’ = y cosθ – z sinθ
z’ = y sinθ + z cosθ
x' 1 0 0 0 x
y ' 0 cos sin 0 y
z ' 0 sin cos 0 z
1 0 0 0 1 1
Y-AXIS ROTATION
The equation for Y-axis rotaion
x’ = x cosθ + z sinθ
y’ = y
z’ = z cosθ - x sinθ
x s x 0 0 0 x
y 0 s 0 0 y
y
z 0 0 s z 0 z
1 0 0 0 1 1
3D SCALING
The equations for scaling
x’ = x . sx
Ssx,sy,sz => y’ = y . sy
z’ = z . sz
Example: Given a 3D object with coordinate
points A(0, 3, 3), B(3, 3, 6), C(3, 0, 1), D(0, 0, 0).
Apply the scaling parameter 2 towards X axis, 3
towards Y axis and 3 towards Z axis and obtain
the new coordinates of the object.
Given-
•Old coordinates of the object = A (0, 3, 3), B(3, 3,
6), C(3, 0, 1), D(0, 0, 0)
•Scaling factor along X axis = 2
•Scaling factor along Y axis = 3
•Scaling factor along Z axis = 3
New coordinates of corner A after scaling = (0, 9,
9).
New coordinates of corner B after scaling = (6, 9, 32
18).
WINDOWS, VIEWPORTS AND
CLIPPING
Terminology
Modelling Coordinate System – Representation
of an object measured in some physical units
Interface Window - The visual representation of
Viewport:
(800,600)
Area on screen to be used for drawing.
(600,400)
Unit: pixels (screen coordinates)
Note: y-axis often points down
(200,200)
(0,0)
(2,1)
Window:
Virtual area to be used by application
Unit: km, mm,… (world coordinates) 34
(-2,-1)
VIEWPORT
In addition, we define a rectangular viewport in the
screen window on the display.
The objects inside the world window appear
automatically at proper sizes and locations inside
the viewport (in screen coordinates, which are
pixel coordinates on the display).
35
VIEWING TRANSFORMATION
36
WINDOW-TO-VIEWPORT COORDINATE TRANSFORMATION
Window-to-Viewport transformation
37
WINDOW-TO-VIEWPORT COORDINATE TRANSFORMATION
YWmax
YVmax
.
xw,yw
xv,yv
YWmin YVmin
38
WINDOW-TO-VIEWPORT COORDINATE TRANSFORMATION
xv - xvmin =
xw - xwmin
yv – yvmin =
yw - ywmin
40
CONTINUE….
After calculation you got the following new
coordinates of viewing port.
The center of the window (1.0, 0.5) maps to the
41
PRACTICE EXERCISE
Find values of sx and sy for the case of a world
window (10.0, 10.0, -6.0, 6.0) and a viewport (0,
600, 0, 400).
42
CLIPPING
The primary use of clipping in computer
graphics is to remove objects, lines, or line
segments that are outside the viewing pane.
The viewing transformation is insensitive to
43
WINDOWING I
A scene is made up of a collection of objects
specified in world coordinates
44
World Coordinates
WINDOWING II
When we display a scene only those objects within
a particular window are displayed
Window
wymax
wymin
45
wxmin wxmax
World Coordinates
WINDOWING III
Because drawing things to a display takes time we
clip everything outside the window
Window
wymax
wymin
46
wxmin wxmax
World Coordinates
CLIPPING
For the image below consider which lines and points
should be kept and which ones should be clipped
P4
Window P2
wymax
P6
P3
P1
P7 P5
P9
P8
wymin
P10
47
wxmin wxmax
POINT CLIPPING
Easy - a point (x,y) is not clipped if:
wxmin ≤ x ≤ wxmax AND wymin ≤ y ≤ wymax
otherwise it is clipped
P4 Clipped
Clipped
Window P2
wymax
Clipped
P5
P1
P7 Points Within the Window
are Not Clipped
P9 P8
wymin
Clipped P10 48
wxmin wxmax
LINE CLIPPING
Harder - examine the end-points of each line to see
if they are in the window or not
Both end-points
Don’t know!
outside the window
COHEN-SUTHERLAND CLIPPING
ALGORITHM
An efficient line clipping algorithm
The key advantage of the algorithm is that it vastly
reduces the number of line intersections that must
be calculated
50
COHEN-SUTHERLAND: WORLD
DIVISION
World space is divided into regions based on
the window boundaries
Each region has a unique four bit region code
Region codes indicate the position of the regions
with respect to the window
P11 [1010]
P4 [1000]
Window
wymax
P6 [0000]
P3 [0001]
P5 [0000] P12 [0010]
P7 [0001]
P9 [0000] P8 [0010]
wymin
P10 [0100]
P13 [0101] P14 [0110]
52
wxmin wxmax
COHEN-SUTHERLAND: LINES IN
THE WINDOW
Lines completely contained within the
window boundaries have region code [0000]
for both end-points so are not clipped
P11 [1010]
P4 [1000]
Window
wymax
P6 [0000]
P3 [0001]
P5 [0000] P12 [0010]
P7 [0001]
P9 [0000] P8 [0010]
wymin
P10 [0100]
P13 [0101] P14 [0110] 53
wxmin wxmax
COHEN-SUTHERLAND: LINES
OUTSIDE THE WINDOW
Any lines with a common set bit in the region
codes of both end-points can be clipped
– The AND operation can efficiently check this
P11 [1010]
P4 [1000]
Window
wymax
P6 [0000]
P3 [0001]
P5 [0000] P12 [0010]
P7 [0001]
P9 [0000] P8 [0010]
wymin
P10 [0100]
P13 [0101] P14 [0110] 54
wxmin wxmax
COHEN-SUTHERLAND: OTHER LINES
Lines that cannot be identified as completely
inside or outside the window may or may not
cross the window interior
These lines are processed as follows:
◦ Compare an end-point outside the
window to a boundary (choose any
order in which to consider boundaries
e.g. left, right, bottom, top) and
determine how much can be
discarded
◦ If the remainder of the line is entirely
inside or outside the window, retain it 55
or clip it respectively
COHEN-SUTHERLAND: OTHER
LINES (CONT…)
Otherwise, compare the remainder of the line
against the other window boundaries
Continue until the line is either discarded or a
segment inside the window is found
We can use the region codes to determine
which window boundaries should be
considered for intersection
To check if a line crosses a particular boundary
we compare the appropriate bits in the region
codes of its end-points
If one of these is a 1 and the other is a 0 then
the line crosses the boundary
56
COHEN-SUTHERLAND EXAMPLES
Consider the line P9 to P10 below Window
wymax
Start at P10
From the region codes
of the two end-points we P [0000]
9
know the line doesn’t wymin
P ’ [0000]
cross the left or right
10
boundary P [0100]
10
58
COHEN-SUTHERLAND EXAMPLES
(CONT…)
Consider the line P7’ to P8
Start at P8
Calculate Window
the wymax
intersection with the
right boundary to P7’ [0000]
generate P8’ P7 [0001] P8 [0010]
P8’ [0000]
P7’to P8’ is inside wymin
the window so is
retained
wxmin wxmax
59
COHEN-SUTHERLAND WORKED
EXAMPLE
Window
wymax
wymin
60
wxmin wxmax
SUMMARY
Objects within a scene must be clipped to
display the scene in a window
Because there can be so many objects clipping
must be extremely efficient
The Cohen-Sutherland algorithm can be used
for line clipping
The Sutherland-Hodgman algorithm can be
used for area clipping
61