You are on page 1of 19

2D TRANSFORMATION

Transformation means changing some graphics into something else by applying rules. We can have
various types of transformations such as translation, scaling up or down, rotation, shearing, etc. 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.

Homogenous Coordinates

To perform a sequence of transformation such as translation followed by rotation and scaling, we need to follow
a sequential process −

 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 PX,Y can be converted to homogenous coordinates by P’ (Xh, Yh, h)

Translation

A translation moves an object to a different position on the screen. You can translate a point in 2D by adding
translation coordinate (t x, ty) to the original coordinate X,Y to get the new coordinate X′,Y′.

From the above figure, you can write that −

1
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=[X][Y] p' = [X′][Y′]T = [tx][ty]
We can write it as −
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 PX,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 PX,Y can be represented as −
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
Where R is the rotation matrix
R=[cosθsinθ−sinθcosθ]
The rotation angle can be positive and negative.
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θ)

2
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 −
X' = X . SX and Y' = Y . SY
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′)=(XY)[Sx00Sy]
OR
P’ = P . S
Where S is the scaling matrix. The scaling process is shown in the following figure.

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.

Reflection

Reflection is the mirror image of original object. In other words, we can say that it is a rotation operation with
180°. In reflection transformation, the size of the object does not change.
The following figures show reflections with respect to X and Y axes, and about the origin respectively.

Shear

A transformation that slants the shape of an object is called the shear transformation. There are two shear
transformations X-Shear and Y-Shear. One shifts X coordinates values and other shifts Y coordinate values.
However; in both the cases only one coordinate changes its coordinates and other preserves its values. Shearing
is also termed as Skewing.

3
X-Shear
The X-Shear preserves the Y coordinate and changes are made to X coordinates, which causes the vertical lines
to tilt right or left as shown in below figure.

The transformation matrix for X-Shear can be represented as −


Xsh=[1shx0010001]
Y' = Y + Shy . X
X’ = X
Y-Shear
The Y-Shear preserves the X coordinates and changes the Y coordinates which causes the horizontal lines to
transform into lines which slopes up or down as shown in the following figure.

The Y-Shear can be represented in matrix from as −


Ysh[100shy10001]
X’ = X + Shx . Y
Y’ = Y

Composite Transformation

If a transformation of the plane T1 is followed by a second plane transformation T2, then the result itself may be
represented by a single transformation T which is the composition of T1 and T2 taken in that order. This is
written as T = T1∙T2.
Composite transformation can be achieved by concatenation of transformation matrices to obtain a combined
transformation matrix.
A combined matrix −
[T][X] = [X] [T1] [T2] [T3] [T4] …. [Tn]
Where [Ti] is any combination of

 Translation
 Scaling

4
 Shearing
 Rotation
 Reflection
The change in the order of transformation would lead to different results, as in general matrix multiplication is
not cumulative, that is [A] . [B] ≠ [B] . [A] and the order of multiplication. The basic purpose of composing
transformations is to gain efficiency by applying a single composed transformation to a point, rather than
applying a series of transformation, one after another.

2- Dimensional Transformations
The Basic Transformations:

1. Translation
2. Scaling
3. Rotation

Other Transformations:

1. Reflection
2. Shearing

Translations
Displacement of an object in a given distance and direction from its original position.

 Rigid body transformation that moves object without deformation


 Initial Position point P (x,y)
 The new point P’
(x’, y’) where
x’ = x + tx , y’ = y + ty , tx and ty is the displacement in x and y respectively.

5
The translation pair (tx, ty) is called a translation vector or shift vector

Matrix/Vector Representation of Translations

A translation can also be represented by a pair of numbers, t=(t x,ty) where tx is the change in
the x-coordinate and tyis the change in y coordinate. To translate the point p by t, we simply
add to obtain the new (translated) point.

Rotation

Rotation is applied to an object by repositioning it along a circular path in the xy plane.


To generate a rotation, we specify
 Rotation angleθ
 Pivot point ( xr ,yr)

Example of Rotation

Scaling

 Scaling alters the size of anobject.


 Operation can be carried out by multiplying each of its components by ascalar
 Uniform scaling means this scalar is the same for all components
 Non-uniform scaling: different scalars per component

6
Example for Scaling

Reflection

A reflection is a transformation that produces a mirror image of an object Generated relativeto an axis of
reflection

1. Reflection along xaxis


2. Reflection along yaxis
3. Reflection relative to an axis perpendicular to the xy plane and passing through the coordinate
origin
4. Reflection of an object relative to an axis perpendicular to the xy plane and passing through
point P
5. Reflection of an object with respect to the liney=x.

Reflection about X-Axis :

Example for reflection about X-axis

Reflection about y-axis:

Example for reflection about Y-axis

Example for reflection about XY plane

7
Shearing

A transformation that distorts the shape of an object such that the transformed object appears as if
the object were composed of internal layers that had been caused to slide over each other.

Example for X- Shearing

Example for Y- Shearing

2D VIEWING

The mapping of a 2D world coordinate system to device coordinates is called a two-dimensional


viewing transformation. The clipping window is the section of the 2D scene that is selected for
viewing. The display window is where the scene will be viewed. The viewport controls the
placement of the scene within the display window

8
Fig 2.10 Steps involved with window to viewport mapping

9
window and viewport

Window to Viewport coordinate transformation

To maintain the same relative placement in view port as in window. The conversion is performed
with the following sequence of transformations:

1. Perform a scaling transformation using point position of (xw min, yw min) that scales the
window area to the size of view port.
2. Translate the scaled window area to the position of view port. Relative proportions of
objects are maintained if scaling factor are the same(Sx=Sy).

Example for window and viewport

Formula for window to viewport transformation

World coordinate – It is the Cartesian coordinate defined by Xwmin, Xwmax, Ywmin,


Ywmax Device Coordinate –It is the screen coordinate where the objects is to be displayed,
like Xvmin, Xvmax, Yvmin, Yvmax

Window –It is the area on world coordinate selected for display.


ViewPort –It is the area on device coordinate where graphics is to be displayed.

10
Advantages

1. The position of the viewport can be changed allowing objects to be viewed at different
positions on the Interface Window.
2. Multiple viewports can also be used to display different sections of a scene at different
screen positions. Also, by changing the dimensions of the viewport, the size and proportions
of the objects being displayed can be manipulated.
3. Thus, a zooming affect can be achieved by successively mapping different dimensioned
clipping windows on a fixed sized viewport.
4. If the aspect ratio of the world window and the viewport are different, then the image may
look distorted.

2D viewing functions

11
CLIPPING OPERATION

1. Point Clipping
2. Line Clipping
3. Area Clipping
4. Curve Clipping
5. Text Clipping

1. Point Clipping

In computer graphics our screen act as a 2-D coordinate system. it is not necessary that each and
every point can be viewed on our viewing pane(i.e. our computer screen). We can view points,
which lie in particular range (0,0) and (Xmax, Ymax). So, clipping is a procedure that identifies
those portions of a picture that are either inside or outside of our viewing pane.

In case of point clipping, we only show/print points on our window which are in range of our
viewing pane, others points which are outside the range are discarded.

2. LINE CLIPPING

i) COHEN –SUTHERLAND LINE CLIPPING algorithm


12
Given a set of lines and a rectangular area of interest, the task is to remove lines which are outside
the area of interest and clip the lines which are partially inside the area.

Clipping window
Cohen-Sutherland algorithm divides a two-dimensional space into 9 regions and then efficiently
determines the lines and portions of lines that are inside the given rectangular area.

Nine regions of Clipping window

There are three possible cases for any given line.


1. Completely inside the given rectangle : Bitwise OR of region of two end points of line is 0
(Both points are inside the rectangle)
2. Completely outside the given rectangle : Both endpoints share at least one outside region
which implies that the line does not cross the visible region. (bitwise AND of endpoints !=
0).
3. Partially inside the window : Both endpoints are in different regions. In this case, the
algorithm finds one of the two points that is outside the rectangular region. The intersection of
the line from outside point and rectangular window becomes new corner point and the
algorithm repeats

Advantages of line clipping

1. It will extract part we desire.

13
2. For identifying the visible and invisible area in the 3D object.
3. For creating objects using solid modeling.
4. For drawing operations.
5. Operations related to the pointing of an object.
ii) For deleting, copying, moving part of an object.
2. LIANG-BARSKY LINE CLIPPING

Based on analysis of parametric equation of a line segment, faster line clippers have been
developed, which can be written in the form :
x = x1 + u ∆x
y = y1 + u ∆y where 0<=u<=1 and where ∆x = (x2 - x1) and ∆y = (y2 - y1)

In the Liang-Barsky approach we first the point clipping condition in parametric form
xwmin <= x1 + u ∆x <=. xwmax
ywmin <= y1 + u ∆y <= ywmax

3. POLYGON CLIPPING

An algorithm that clips a polygon is rather complex. Each edge of the polygon must be tested
against each edge of the clipping window, usually a rectangle. As a result, new edges may be
added, and existing edges may be discarded, retained, or divided. Multiple polygons may result
from clipping a single polygon. We need an organized way to deal with all of these cases.

SUTHERLAND HODGEMAN POLYGON CLIPPING

The Sutherland and Hodgman's polygon clipping algorithm is used in this tool. This algorithm is
based on a divide- and-conquer strategy that solves a series of simple and identical problems
that, when combined, solve the overall problem. The simple problem is to clip a polygon against
a single infinite clipping edge. This process outputs the series of vertices that define the clipped
polygon. Four clipping edges, each defining one boundary of the clipping window, are used to
successively to fully clip the polygon.

Area clipping

How to clip against an edge of clipping area:

1. Both vertices are inside : Only the second vertex is added to the output list

14
2. First vertex is outside while second one is inside : Both the point of intersection of the
edge with the clip boundary and the second vertex are added to the output list
3. First vertex is inside while second one is outside : Only the point of intersection of the
edge with the clip boundary is added to the output list

15
4. Both vertices are outside : No vertices are added to the output list

Cases of Polygon clipping

SamplePolygon After Clipping

Polygon Clipping

5. Curve Clipping
Curve Clipping involves complex procedures as compared to line clipping. Curve clipping requires
more processing than for object with linear boundaries. Consider window which is rectangular in
shape. The circle is to consider against rectangle window.
If circle is completely inside boundary of the window, it is considered visible. So save the circle. If
a circle is in outside window, discard it. If circle cut the boundary then consider it to be clipping
case.
The below figure illustrates circle clipping against a rectangular window. On the first pass, we can
clip the bounding rectangle of the object against the bounding rectangle of the clip region. If the
two regions overlap, we will need to solve the simultaneous line-curve equations to obtain the
clipping intersection points

16
5. TEXT CLIPPING

Various techniques are used to provide text clipping in a computer graphics. It depends on the
methods used to generate characters and the requirements of a particular application. There are three
methods for text clipping which are listed below −

 All or none string clipping


 All or none character clipping
 Text clipping
The following figure shows all or none string clipping −

Fig 2.22 Text Clipping Example -1

In all or none string clipping method, either we keep the entire string or we reject entire string based
on the clipping window. As shown in the above figure, STRING2 is entirely inside the clipping
window so we keep it and STRING1 being only partially inside the window, we reject.
The following figure shows all or none character clipping −

17
Fig 2.23 Text Clipping Example -2

This clipping method is based on characters rather than entire string. In this method if the string is
entirely inside the clipping window, then we keep it. If it is partially outside the window, then −
 You reject only the portion of the string being outside
 If the character is on the boundary of the clipping window, then we discard that entire
character and keep the rest string.
The following figure shows text clipping −

Fig 2.24 Text Clipping Example -3

18
This clipping method is based on characters rather than the entire string. In this
method if the string is entirely inside the clipping window, then we keep it. If it is
partially outside the window, then
 You reject only the portion of string being outside.
 If the character is on the boundary of the clipping window, then we
discard only that portionof character that is outside of the clipping
window.

19

You might also like