Monday, February 18, 2019


2D TRANSFORMATIONS
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.
Various types of 2D transformations are:  
1.      Translation
2.      Scaling(Up or Down)
3.      Rotation
4.      Shearing
5.      Reflection

TRANSLATION
Translation means changing the position of an object
A translation moves an object to a different position on the screen.
A point P in 2D, can be translated by adding translation coordinate T(tx, ty) to the original coordinate P(X, Y) to get the new coordinate P’(X’, Y’).
Translation
From the above figure, −
X’ = X + tx
Y’ = Y + ty
The pair T(tx, ty) is called the translation vector or shift vector. The above equations can also be represented using the column vectors.

P =               P’ =                         T =

We can write it as −
P’ = P + T

SCALING
To change the size of an object, scaling transformation is used.
In the scaling process, the size of the object is either increased or decreased.
Increase in size – Scaling up
Decrease in Size – Scaling Down
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 −

OR
P’ = P . S
Where S is the scaling matrix.
The scaling process is shown in the following figure.

Before ScalingAfter Scaling

Different Cases with Scaling:
If Sx ≠ Sy, then there is change in size as well as shape of the object.
If Sx == Sy, then, there is change only in the size of the object and there is no change in shape.
If Sx and Sy are lesser than 1, then the size of the object decreases.
If Sx and Sy are greater than 1, then the size of the object increases.



ROTATION
In rotation, we rotate the object at particular angle θ (theta) from its origin.
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)
Positive values of θ for counterclockwise rotation
Negative values of θ for clockwise rotation.

Rotation about 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’).
Rotation

Using standard trigonometric the original coordinate of point P(X, 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,
 

P =              
P’ =            

P’ = P . R
Where R is the rotation matrix
The rotation angle can be positive and negative.
For positive rotation angle (anti-clockwise), we can use the above rotation matrix.
However, for negative angle rotation (clockwise), the matrix will change as shown below −

R =  

Rotation about an arbitrary point
– Translate to the origin: T(−x1, −y1)
– Rotate: R(q)
– And translate back: T(x1, y1)

T(x1, y1) • R(q) • T(− x1, − y1)
 P’ = T(x1, y1)R(q)T(− x1, − y1)P = AP



SHEARING
ü  A transformation that distorts the shape of an object is called Shearing or Shear Transformation.
ü  The transformed object appears as if the object were composed of internal layers that had been caused to slide over each other.

ü  There are two shear transformations: X-Shear and Y-Shear
ü  In X-Shearing, the X coordinates values are shifted and Y value remains unaffected.
ü  In Y Shearing, the Y coordinate values are shifted and the X value remains unaffected.
ü  Shearing is also termed as Skewing.

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.
X-Shear
The transformation matrix for X-Shear can be represented as –
X’ = X + Shx . Y
Y’ = Y

Xsh = 

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.
Y-Shear

The Y-Shear can be represented in matrix from as −
Ysh  =    
Y' = Y + Shy . X
X’ = X

REFLECTION
ü  A reflection is a transformation that produces a mirror image of an object
ü  Reflection can also be seen as “rotation operation with 180°”.
ü  In reflection transformation, the size of the object does not change.
ü  Different types of reflections that can be done are:
1.Reflection along x axis
2.Reflection along y axis
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 line y=x

Reflection About x-Axis
        

           
Reflection about y-axis:
                                       


Reflection relative to an axis perpendicular to the xy plane and passing through the coordinate origin



Reflection of an object with respect to the line y=x


COMPOSITE TRANSFORMATIONS
ü  For a sequence of transformations, composite transformation matrix could be set up by the matrix product of the individual transformations
ü  Also referred to as Concatenation or Composition of  Matrices


HOMOGENEOUS COORDINATES
When we expand the matrix representations for coordinate positions, homogeneous coordinates allows us to express all transformation equations as matrix multiplications,
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 P(X, Y) can be converted to homogenous coordinates by P’ (Xh, Yh, h).

The three basic operations have a little different form.
We want the same form and introduce homogeneous coordinates.
This means that we write a position vector like this:
2DHomogenVector
Then we can write all basic operations as multiplication between a 3 x 3 matrix and a 1 x 3 vector.
Translation
2DTranslate
Scaling
2DScale
Rotation
2DRotate
We have a general form
                                                P2=M·P1
   
where P1 and P2 are expressed in homogeneous coordinates.

                                    -----------------------------



The Sutherland - Hodgman algorithm performs a clipping of a polygon against each window edge in turn.
It accepts an ordered sequence of verices v1, v2, v3, ..., vn and puts out a set of vertices defining the clipped polygon.

 This figure represents a polygon (the large, solid, upward pointing arrow) before clipping has occurred.



















        
The following figures show how this algorithm works at each edge, clipping the polygon.
 
a.      Clipping against the left side of the clip window.
b.     Clipping against the top side of the clip window.
c.      Clipping against the right side of the clip window.
d.     Clipping against the bottom side of the clip window.
When the polygon is clipped against the window there are four types of window edges.
The four types of edges are:
1.     Edges that are totally inside the clip window. - add the second inside vertex point
2.     Edges that are leaving the clip window. - add the intersection point as a vertex
3.     Edges that are entirely outside the clip window. - add nothing to the vertex output list
4.     Edges that are entering the clip window. - save the intersection and inside points as vertices

For each clip edge - scan the polygon and consider the relation between successive vertices of the polygon
Each iteration adds 0, 1 or 2 new vertices.
Assume vertex s has been dealt with, vertex p follows:
/* Calculating the Intersection Points for the polygon:
Assume that, a polgon's edge with vertices at (x1,y1) and (x2,y2) is clipped against a clip window with vertices at (xmin, ymin) and (xmax,ymax).
The location (IX, IY) of the intersection of the edge with the left side of the window is:
       i.            IX = xmin
     ii.            IY = slope*(xmin-x1) + y1, where the slope = (y2-y1)/(x2-x1)
The location of the intersection of the edge with the right side of the window is:
       i.            IX = xmax
     ii.            IY = slope*(xmax-x1) + y1, where the slope = (y2-y1)/(x2-x1)
The intersection of the polygon's edge with the top side of the window is:
       i.            IX = x1 + (ymax - y1) / slope
     ii.            IY = ymax
Finally, the intersection of the edge with the bottom side of the window is:
       i.            IX = x1 + (ymin - y1) / slope
     ii.            IY = ymin
*/---------
Calculating the Intersection Points for the polygon:
Assume that, a polgon's edge with vertices at (x1,y1) and (x2,y2) is clipped against a clip window with vertices at (xmin, ymin) and (xmax,ymax).
Every polygon edge has to be considered as a line and the intersection point has to be calculated similar to line clipping and using the slope equation for a line.
Example: