CS401-M3-Computer Graphics-Ktustudents - in
CS401-M3-Computer Graphics-Ktustudents - in
Module-3
Two dimensional transformations. Homogeneous coordinate systems – matrix formulation and
concatenation of transformations. Windowing concepts –Window to Viewport Transformation-
Two dimensional clipping-Line clipping – Cohen Sutherland, Midpoint Subdivision algorithm
Two Dimensional Geometric Transformations:
Changes in orientations, size and shape are accomplished with geometric transformations that alter the
coordinate description of objects.
Basic transformation
Translation
T(tx, ty)
Translation
distances
Scale
S(sx,sy)
Scale factors
Rotation
R( )
KTUStudents.in
Rotation angle
Translation
A translation is applied to an object by representing it along a straight line path from one
coordinate location to another adding translation distances, tx, ty to original coordinate position (x,y) to
move the point to a new position (x’,y’) to
x’ = x + tx, y’ = y + ty
Dept. of CSE 1
Translation equation can be expressed as single matrix equation by using column vectors to represent the
coordinate position and the translation vector as
P (x, y)
T (tx , t y )
x' x t x
y' y ty
The translation distance point (tx,ty) is called translation vector or shift vector.
Translation equation can be expressed as single matrix equation by using column vectors
to represent the coordinate position and the translation vector as
P (x, y)
T (tx ,ty )
x' x tx
y' y ty
KTUStudents.in x1
y1
x tx
= y + ty
P' P T
Rotations:
Positive values for the rotation angle define counter clock wise rotation about
pivot point. Negative value of angle rotates objects in clock wise direction. The
KTUStudents.in
transformation can also be described as a rotation about a rotation axis perpendicular to
xy plane and passes through pivot point
Rotation of a point from position (x,y) to position (x’,y’) through angle θ relative to
coordinateorigin
The transformation equations for rotation of a point position P when the pivot point is at
coordinate origin. In figure r is constant distance of the point positions Ф is the original
angular of the point from horizontal and θ is the rotation angle.
x = rcosФ, y = rsinФ
the transformation equation for rotating a point at position (x,y) through an angle θ about
origin
x’ = xcosθ – ysinθ
KTUStudents.in
y’ = xsinθ + ycosθ
Rotation equation
P’= R . P
Rotation Matrix
R= Cosθ sinθ
Sinθ cosθ
Note : Positive values for the rotation angle define counterclockwise rotations about the
rotation point and negative values rotate objects in the clockwise.
Scaling
A scaling transformation alters the size of an object. This operation can be carried out for
polygons by multiplying the coordinate values (x,y) to each vertex by scaling factor Sx &
Sy to produce the transformed coordinates (x’,y’)
KTUStudents.in
x1 sx 0
x y
y1 sy
or
P’ = S. P
Turning a square (a) Into a rectangle (b) with scaling factors sx = 2 and sy= 1.
Any positive numeric values are valid for scaling factors sx and sy. Values less than 1
reduce the size of the objects and values greater than 1 produce an enlarged object.
There are two types of Scaling. They are
Uniform scaling
Non Uniform Scaling
1. Reflection
2. Shear
Reflection
KTUStudents.in
Reflection the x axis is accomplished with the transformation matrix
1 0 0
0 -1 0
0 0 1
-1 0 0
0 1 0
0 0 1
KTUStudents.in
Reflection about origin is accomplished with the transformation matrix
-1 0 0
0 -1 0
0 0 1
Shear
A Transformation that slants the shape of an object is called the shear transformation.
Two common shearing transformations are used. One shifts x coordinate values and other
shift y coordinate values. However in both the cases only one coordinate (x or y) changes
its coordinates and other preserves its values.
1 shx 0
0 1 0
0 0 1
KTUStudents.in
which transforms the coordinates as
x’ =x+ shx .y
y’ = y
Y Shear
The y shear preserves the x coordinates, but changes the y values which cause horizontal
lines which slope up or down
as x’ =x
y’ = y+ shy .x
1
X
1 shx 0 X
1 = shy 1 0 1 Y
Y 1
0 0 1
which transforms the coordinates as x’
=x+ shx .y
y’ = y+ shy .x
Homogenous Coordinates:
To perform a sequence of transformation such as translation followed by rotation and scaling, we need to
follow a sequential process −
KTUStudents.in
Translate the coordinates,
Rotate the translated coordinates, and then
Scale the rotated coordinates to complete the composite transformation.
To shorten this process, we have to use 3×3 transformation matrix instead of 2×2 transformation matrix. To
convert a 2×2 matrix to 3×3 matrix, we have to add an extra dummy coordinate W.
In this way, we can represent the point by 3 numbers instead of 2 numbers, which is called Homogenous
Coordinate system. In this system, we can represent all the transformation equations in matrix multiplication.
Any Cartesian point P(X, Y) can be converted to homogenous coordinates by P’ (X w, Yw, w).
Translation
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’).
KTUStudents.in
X’ = X + tx
Y’ = Y + ty
The pair (tx, ty) is called the translation vector or shift vector. The above equations can also be represented
using the column vectors.
P’ = P + T
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’).
Using standard trigonometric the original coordinate of point P(X, Y) can be represented as −
KTUStudents.in
X= rcosϕ......(1)
Y=rsinϕ......(2)
Same way we can represent the point P’ (X’, Y’) as −
x′=rcos(ϕ+θ)=rcosϕcosθ−rsinϕsinθ.......(3)
y′=rsin(ϕ+θ)=rcosϕsinθ+rsinϕcosθ.......(4)
Substituting equation (1) & (2) in (3) & (4) respectively, we will get
x′=xcosθ−ysinθ
y′=xsinθ+ycosθ
Representing the above equation in matrix form,
[X′Y′]=[XY][cosθsinθ−sinθcosθ]OR
P’ = P . R
For positive rotation angle, we can use the above rotation matrix. However, for negative angle rotation, the
matrix will change as shown below −
R=[cos(−θ)sin(−θ)−sin(−θ)cos(−θ)]
=[cosθsinθ−sinθcosθ](∵cos(−θ)=cosθandsin(−θ)=−sinθ)
Scaling
To change the size of an object, scaling transformation is used. In the scaling process, you either expand or
compress the dimensions of the object. Scaling can be achieved by multiplying the original coordinates of the
object with the scaling factor to get the desired result.
Let us assume that the original coordinates are (X, Y), the scaling factors are (SX, SY), and the produced
coordinates are (X’, Y’). This can be mathematically represented as shown below −
The scaling factor SX, SY scales the object in X and Y direction respectively. The above equations can also be
represented in matrix form as below −
[X′Y′]=[X Y] Sx 0
0 Sy
OR
P’ = P . S
Where S is the scaling matrix. The scaling process is shown in the following figure.
KTUStudents.in
If we provide values less than 1 to the scaling factor S, then we can reduce the size of
the object. If we provide values greater than 1, then we can increase the size of the
object.
KTUStudents.in
KTUStudents.in
KTUStudents.in
KTUStudents.in
KTUStudents.in
KTUStudents.in
KTUStudents.in
KTUStudents.in
KTUStudents.in
KTUStudents.in
Windowing concepts:
Window: Area selected in world-coordinate for display is called window. It defines what is to be viewed.
Viewport: Area on a display device in which window image is display (mapped) is called viewport. It defines
where to display.
In many case window and viewport are rectangle, also other shape may be used as window and viewport.
In general finding device coordinates of viewport from word coordinates of window is called as viewing
transformation.
Sometimes we consider this viewing transformation as window-to-viewport transformation but in general it
involves more steps.
KTUStudents.in
Fig: - A viewing transformation using standard rectangles for the window and viewport.
Now we see steps involved in viewing pipeline.
By changing the size of the window and viewport we can obtain zoom in and zoom out effect as per
requirement.
Fixed size viewport and small size window gives zoom in effect, and fixed size viewport and larger window gives
zoom out effect.
View ports are generally defines with the unit square so that graphics package are more device independent
which we call as normalized viewing coordinate.
Window-To-Viewport Coordinate Transformation:
Mapping of window coordinate to viewport is called window to viewport transformation.
We do this using transformation that maintains relative position of window coordinate into viewport.
That means center coordinates in window must be remains at center position in viewport.
We find relative position by equation as follow:
xv − xvmin xw − xwmin
=
xvmax − xvmin xwmax − xwmin
yv − yvmin yw − ywmin
=
yvmax − yvmin ywmax − ywmin
Solving by making viewport position as subject we obtain:
xv = xvmin + (xw − xwmin)sx
KTUStudents.in
KTUStudents.in
KTUStudents.in
P9
Window Window
P4 P10
P2 P2
P8
P1 P1
P5 P6 P5 P6 P8
P3
P7
P7
In this we divide whole space into nine region and assign 4 bit code to each endpoint of line depending on the
position where the line endpoint is located.
KTUStudents.in
Set bit 3: For below clipping window.
Set bit 4: For above clipping window.
All bits as mention above are set means 1 and other are 0.
After assigning code we check both end points code if both endpoint have code 0000 then line is completely
inside.
If both are not 0000 then we perform logical ending between this two code and check the result if result is non
zero line is completely outside the clipping window.
Otherwise we calculate the intersection point with the boundary one by one and divide the line into two parts
from intersection point and perform same procedure for both line segments until we get all segments
completely inside or completely outside.
Draw line segment which are completely inside and eliminate other line segment which found completely
outside.
Now we discuss how to calculate intersection points with boundary.
For intersection calculation we use line equation “y = mx + b”.
For left and right boundary x coordinate is constant for left “x = xwnin” and for right “x = xwnas” .
We calculate y coordinate of intersection for this boundary by putting values of x as follow:
y = y1 + m(x − x1)
Similarly fro top and bottom boundary y coordinate is constant for top “y = ywnas” and for bottom “y =
ywnin”.
We calculate x coordinate of intersection for this boundary by putting values of y as follow:
y − y1
x = x1 +
m
Midpoint Subdivision Algorithm By now you know that the Cohen Sutherland algorithm involves calculating
the intersection of the line with the window’s boundaries. However, these calculations can be avoided by
subdividing the line at its midpoint. This subdividing process is repeated until you get the line segment
completely visible or completely invisible. In the Midpoint Subdivision algorithm, the line is first tested for
visibility. If the line is completely outside the window, then it is invisible and is rejected. If the line is visible,
then the line is within the window and is drawn. On the other hand, if the line is partially visible, then it is
subdivided into two parts. Each line segment is again tested for visibility. This subdivision process is repeated
until you get a line segment that is completely visible (inside the window) or completely invisible (outside the
window).
KTUStudents.in
xm = x1+ x2 / 2
ym = y1 + y2/2
The Midpoint subdivision algorithm is written as below:
1. Read two endpoints of the line: A(x1, y1) and A(x2, y2).
2. Read two regions of the window (left top and right bottom): (Wx1, Wy1 and Wx2 and Wy2).
KTUStudents.in
3. Assign 4-bit region codes for the two endpoints.
Initialize code with bits 0000.
Set Bit 1 if (x < Wx1 )
Set Bit 2 if (x >Wx2 )
Set Bit 3 if (y < Wy1) Set Bit 4 if (y > Wy2)
4. Check for line visibility
(a) If the region codes for both the endpoints of the line are 0000 then
the line is within the window and completely visible.
Go to step 6
(b) If the region codes for endpoints of the line are not 0000 and the
bitwise logical AND is also not 0000, then the line is outside the
window and completely invisible.
Go to step 6
(c) If the region codes for endpoints do not satisfy any of the above
two conditions then that line intersects any one of the window
boundaries, and only a segment of the line inside the window frame
is visible.
5. The partially visible line is divided into two equal parts. Repeat step 3
through 5 until the line is completely visible or completely invisible.
6. Stop.
As Midpoint Subdivision algorithm involves repeated subdivision of the line
segments, it is slower compared to Cohen Sutherland algorithm, which
……………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………
KTUStudents.in