0% found this document useful (0 votes)
15 views61 pages

Ch5 Grap Lecture

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views61 pages

Ch5 Grap Lecture

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 61

ADMAS UNIVERSITY

DEPARTMENT OF COMPUTER SCIENCE


CHAPTER 5:
COMPUTER GRAPHICS
2D/3D TRANSFORMATIONS, CLIPPING,
WINDOWS AND VIEWPORT

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

plane, it is called 2D transformation.


 Transformations play an important role in

computer graphics to reposition the graphics on


the screen and change their size or orientation.
 “Transformations are the operations applied to

geometrical description of an object to change


its position, orientation, or size are called
geometric transformations”.

2
WHY TRANSFORMATIONS
NEEDED?

 Transformations are needed to


manipulate
the initially created object and to display
the
modified object without having to redraw
it.
 Animation of graphical objects like:
 Games
 Movies
 Simulations 3

 Or to create carton of any object.


CONT’D


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

angle θ. After rotating it to a new location, you


will get a new point P’ (X’, Y’).

9
Rotation
• Review Trigonometry
=> cos  = x/r , sin = y/r
• x = r. cos , y = r.sin 
P’(x’, y’)

=> cos (+ ) = x’/r


•x’ = r. cos (+ ) 
•x’ = r.coscos -r.sinsin r
•x’ = x.cos  – y.sin  y’ P(x,y)

=>sin (+ ) = y’/r


 r y
y’ = r. sin (+ )

•y’ = r.cossin + r.sincos x’ x
•y’ = x.sin  + y.cos 
Identity of Trigonometry 10
Rotation
• We can write the components:
p'x = px cos  – py sin 
p'y = px sin  + py cos 

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 cos 90  1 sin 90


 
 5 sin 90  1 cos 90

 5 0  1 1
 
 5 1  1 0
  1
 
 5 12
EXAMPLE

 Consider the triangle defined by its three


vertices A(20,0), B(60,0), C(40,100).
 Rotate the above given triangle 450 clockwise

about the origin and draw the rotated triangle.

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.

• We can write the components:

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)

•Ifthe scale factors are larger than 1


 the points will be moved away P’

from the origin  the object will be


larger.
• Example :
•P(2, 5), Sx = 2, Sy = 2
15
Scaling
P’

• If the scale factors are the same,


Sx = Sy  uniform scaling
• Only change in size (as previous
example)
•IfSx  Sy  differential scaling. P(1, 2)
•Change in size and shape
•Example : square  rectangle
•P(1, 3), Sx = 2, Sy = 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

Homogeneous coordinates: 4 components a b c tx 


d e f t y 
Transformation matrices: 4×4 elements 
g h i tz 
 
0 0 0 1
3D TRANSLATION

Moving of object is called translation.

In 3 dimensional homogeneous coordinate representation , a
point is transformed from position P = ( x, y , z) to P’=( x’, y’, z’)

This can be written as:-
Using Linear formula P’ = T + P
Or Matrix formula P’ = T . P

 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'  cos  0 sin  0  x 


 y '  0 1 0 0  y 
     
 z '    sin  0 cos  0  z 
     
 1  0 0 0 1  1 
Z-AXIS ROTATION
THE EQUATION FOR Z-AXIS ROTAION
X’ = X COSΘ – Y SINΘ
Y’ = X SINΘ + Y COSΘ
Z’ = Z

 x'  cos   sin  0 0  x 


 y '  sin  cos  0 0  y 
   
 z'  0 0 1 0  z 
     
 1  0 0 0 1  1 
EXAMPLE:
FIND THE NEW COORDINATES OF AN
OBJECT 90º-ROTATED ABOUT AN AXIS
DEFINED BY ITS POINT A(2,1,0).
EXERCISE

TRANSFORM THE GIVEN POSITION [ 3 2


0 ]BY THE FOLLOWING SEQUENCE OF
OPERATIONS
(I) TRANSLATE BY –1, -1, -1 IN X, Y, AND Z
RESPECTIVELY
(II) ROTATE BY +30°ABOUT X-AXIS AND
+45°ABOUT Y AXIS
3D SCALING

Changes the size of the object and repositions the object relative
to the coordinate origin.

 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

the screen coordinate system for “window” displays


(coordinate system moves with interface window).
Viewport - The rectangle on the graphics screen

(or interface window for “window” displays)


defining where the image will appear.
Viewing Transformation - The process of going

from a window in world coordinates to a viewport in


screen coordinates.
33
WINDOW AND VIEWPORT

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

 Viewing transformation is the mapping of a part of


a world-coordinate scene to device coordinates.
 In 2D (two dimensional) viewing transformation is
simply referred as the window-to-viewport
transformation or the windowing transformation.
 Mapping a window onto a viewport involves
converting from one coordinate system to
another.

36
WINDOW-TO-VIEWPORT COORDINATE TRANSFORMATION

Window-to-Viewport transformation
37
WINDOW-TO-VIEWPORT COORDINATE TRANSFORMATION

YWmax
YVmax
.
xw,yw
xv,yv
YWmin YVmin

XWmin XWmax XVmin XVmax

38
WINDOW-TO-VIEWPORT COORDINATE TRANSFORMATION

xv - xvmin =
xw - xwmin

xvmax - xvmin xwmax - xwmin

yv – yvmin =
yw - ywmin

yvmax – yvmin ywmax - ywmin

From these two equations we derived


xv = xvmin + (xw – xwmin)sx

yv = yvmin + (yw – ywmin)sy

where the scaling factors are 39

sx = xvmax – xvmin sy = yvmax - yvmin


EXAMPLE
 Consider the window and viewport of Figure The
window has (Wxmin, Wxmax, Wymin, Wymax) = (0,
2.0, 0, 1.0) and the viewport has (Vxmin, Vxmax,
Vymin, Vymax) = (40, 400, 60, 300) where the
original coordinate of the point is (1,0.5).

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

center of the viewport (220, 180 )

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

the position of points relative to the viewing


volume especially those points behind the
viewer − and it is necessary to remove these
points before generating the view.

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

Situation Solution Example

Both end-points inside


Don’t clip
the window

One end-point inside


the window, one Must clip
outside

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

1001 1000 1010


3 2 1 0
0000
above below right left 0001 0010
Window
Region Code Legend
51
0101 0100 0110
COHEN-SUTHERLAND: LABELLING
Every end-point is labelled with the appropriate
region code

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

 Calculate the wxmin wxmax


intersection of the line with the bottom boundary to
generate point P10’
 The line P9 to P10’ is completely inside the window so is
retained
57
COHEN-SUTHERLAND EXAMPLES
(CONT…)
Consider the line P7 to P8 below
 Start at P7
 From Window
the two region wymax
codes of the two
end-points we know P7’ [0000]
the line crosses the P7 [0001] P8 [0010]
left boundary so P8’ [0000]
wymin
calculate the
intersection point to
generate P7’
wxmin wxmax

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

You might also like