CS8092 Computer Graphics and Multimedia UNIT II-Two Dimensional Graphics 2.1 Two Dimensional Geometric Transformations
CS8092 Computer Graphics and Multimedia UNIT II-Two Dimensional Graphics 2.1 Two Dimensional Geometric Transformations
• Translation
▪ T(tx, ty)
▪ Translation distances
• Scale
▪ S(sx,sy)
▪ Scale factors
• Rotation
▪ R()
▪ Rotation angle
1. 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
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 = (t x , t y )
x' = x + t x
y' = y + t y
x ' x t x
y ' = y + t
y
P' = P + T
Moving a polygon from one position to another position with the translation
vector (-5.5, 3.75)
2. Rotations:
A two-dimensional rotation is applied to an object by repositioning it along a circular path
on xy plane. To generate a rotation, specify a rotation angle θ and the position (xr,yr) of the rotation
point (pivot point) about which the object is to be rotated.
Positive values for the rotation angle define counter clock wise rotation about pivot point.
Negative value of angle rotate objects in clock wise direction. The 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 coordinate
origin
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.
The transformed coordinates in terms of angle θ and Ф
x’ = rcos(θ+Ф) = rcosθ cosФ – rsinθsinФ
y’ = rsin(θ+Ф) = rsinθ cosФ + rcosθsinФ
The original coordinates of the point in polar coordinates
x = rcosФ, y = rsinФ
the transformation equation for rotating a point at position (x,y) through an angle θ about origin
x’ = xcosθ – ysinθ
y’ = xsinθ + ycosθ
Rotation equation
P’= R . P
Rotation Matrix
Note : Positive values for the rotation angle define counterclockwise rotations about the rotation
point and negative values rotate objects in the clockwise.
3. 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’)
x’= x.Sx y’ = y.Sy
scaling factor Sx scales object in x direction while Sy scales in y direction.
The transformation equation in matrix form
x' s x 0 x
y ' = 0 s y y
or
P’ = S. P Where S is 2 by 2 scaling matrix
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
To get uniform scaling it is necessary to assign same value for sx and sy. Unequal values for sx and
sy result in a non uniform scaling.
1 0 0 1 1
P ' = T (t x , t y ) • P
For Scaling
x' s x 0 0 x
y ' = 0 sy 0 y
1 0 0 1 1
P ' = S (s x , s y ) • P
For rotation
Translation
If two successive translation vectors (tx1,ty1) and (tx2,ty2) are applied to a coordinate position P,
the final transformed location P’ is calculated as
P’=T(tx2,ty2).{T(tx1,ty1).P}
={T(tx2,ty2).T(tx1,ty1)}.P
Where P and P’ are represented as homogeneous-coordinate column vectors.
sx 2 0 0 sx1 0 0 sx 2.sx1 0 0
0 sy 2 0. 0 sy1 0 = 0 sy 2.sy1 0
0 0 1 0 0 1 0 0 1
General Pivot-point Rotation
1. Translate the object so that pivot-position is moved to the coordinate origin
2. Rotate the object about the coordinate origin
Translate the object so that the pivot point is returned to its original position
The composite transformation matrix for this sequence is obtain with the concatenation
Concatenating the matrices for these three operations produces the required scaling matix
Can also be expressed as T(xf,yf).S(sx,sy).T(-xf,-yf) = S(xf, yf, sx, sy)
Note : Transformations can be combined by matrix multiplication
Reflection
A reflection is a transformation that produces a mirror image of an object. The mirror image for a
two-dimensional reflection is generated relative to an axis of reflection by rotating the object 180o
about the reflection axis. We can choose an axis of reflection in the xy plane or perpendicular to the
xy plane or coordinate origin
− 1 0 0
0 −1 0
0 0 1
Reflection axis as the diagonal line y = x
To obtain transformation matrix for reflection about diagonal y=x the transformation sequence is
1. Clock wise rotation by 450
2. Reflection about x axis
3. counter clock wise by 450
Reflection about the diagonal line y=x is accomplished with the transformation matrix
0 1 0
1 0 0
0 0 1
Reflection axis as the diagonal line y = -x
To obtain transformation matrix for reflection about diagonal y=-x the transformation sequence is
1. Clock wise rotation by 450
2. Reflection about y axis
3. counter clock wise by 450
Reflection about the diagonal line y=-x is accomplished with the transformation matrix
0 −1 0
− 1 0 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.
X- Shear
The x shear preserves the y coordinates, but changes the x values which cause vertical lines to tilt
right or left as shown in figure
Otherwise world objects will be stretched or contracted in either the x or y direction when
displayed on output device. For normalized coordinates, object descriptions are mapped to various
display devices.
Any number of output devices can be open in particular application and another window
view port transformation can be performed for each open output device. This mapping called the
work station transformation is accomplished by selecting a window area in normalized apace and a
view port are in coordinates of display device.
Mapping selected parts of a scene in normalized coordinate to different video monitors with
work station transformation.
Two Dimensional viewing functions
Viewing reference system in a PHIGS application program has following function.
evaluateViewOrientationMatrix(x0,y0,xv,yv,error, viewMatrix)
where x0,y0 are coordinate of viewing origin and parameter xv, yv are the world coordinate
positions for view up vector.An integer error code is generated if the input parameters are in error
otherwise the view matrix for world-to-viewing transformation is calculated. Any number of
viewing transformation matrices can be defined in an application.
Here window limits in viewing coordinates are chosen with parameters xwmin, xwmax, ywmin,
ywmax and the viewport limits are set with normalized coordinate positions xvmin, xvmax, yvmin,
yvmax.
The combinations of viewing and window view port mapping for various workstations in a viewing
table with
setViewRepresentation(ws,viewIndex,viewMatrix,viewMappingMatrix, xclipmin,
xclipmax, yclipmin, yclipmax, clipxy)
Where parameter ws designates the output device and parameter view index sets an integer
identifier for this window-view port point. The matrices viewMatrix and viewMappingMatrix can
be concatenated and referenced by viewIndex.
setViewIndex(viewIndex)
selects a particular set of options from the viewing table.
At the final stage we apply a workstation transformation by selecting a work station window
viewport pair.
setWorkstationWindow (ws, xwsWindmin, xwsWindmax,
ywsWindmin, ywsWindmax)
Binary region codes assigned to line end points according to relative position with respect to
the clipping rectangle.
Regions are set up in reference to the boundaries. Each bit position in region code is used to
indicate one of four relative coordinate positions of points with respect to clip window: to the left,
right, top or bottom. By numbering the bit positions in the region code as 1 through 4 from right to
left, the coordinate regions are corrected with bit positions as
bit 1: left
bit 2: right
bit 3: below
bit 4: above
A value of 1 in any bit position indicates that the point is in that relative position. Otherwise
the bit position is set to 0. If a point is within the clipping rectangle the region code is 0000. A point
that is below and to the left of the rectangle has a region code of 0101.
Bit values in the region code are determined by comparing endpoint coordinate values (x,y)
to clip boundaries. Bit1 is set to 1 if x <xwmin.
For programming language in which bit manipulation is possible region-code bit values can
be determined with following two steps.
(1) Calculate differences between endpoint coordinates and clipping boundaries.
(2) Use the resultant sign bit of each difference calculation to set the corresponding value in the
region code.
bit 1 is the sign bit of x – xwmin
bit 2 is the sign bit of xwmax - x
bit 3 is the sign bit of y – ywmin
bit 4 is the sign bit of ywmax - y.
Once we have established region codes for all line endpoints, we can quickly determine which lines
are completely inside the clip window and which are clearly outside.
Any lines that are completely contained within the window boundaries have a region code
of 0000 for both endpoints, and we accept
these lines. Any lines that have a 1 in the same bit position in the region codes for each endpoint
are completely outside the clipping rectangle, and we reject these lines.
We would discard the line that has a region code of 1001 for one endpoint and a code of
0101 for the other endpoint. Both endpoints of this line are left of the clipping rectangle, as
indicated by the 1 in the first bit position of each region code.
A method that can be used to test lines for total clipping is to perform the logical and
operation with both region codes. If the result is not 0000,the line is completely outside the clipping
region.
Lines that cannot be identified as completely inside or completely outside a clip window by
these tests are checked for intersection with window boundaries.
Line extending from one coordinates region to another may pass through the clip window, or
they may intersect clipping boundaries without entering window.
Cohen-Sutherland line clipping starting with bottom endpoint left, right , bottom and top
boundaries in turn and find that this point is below the clipping rectangle.
Starting with the bottom endpoint of the line from P1 to P2, we check P1 against the left,
right, and bottom boundaries in turn and find that this point is below the clipping rectangle. We
then find the intersection point P1’ with the bottom boundary and discard the line section from P1
to P1’.
The line now has been reduced to the section from P1’ to P2,Since P2, is outside the clip
window, we check this endpoint against the boundaries and find that it is to the left of the window.
Intersection point P2’ is calculated, but this point is above the window. So the final intersection
calculation yields P2”, and the line from P1’ to P2”is saved. This completes processing for this line,
so we save this part and go on to the next line.
Point P3 in the next line is to the left of the clipping rectangle, so we determine the
intersection P3’, and eliminate the line section from P3 to P3'. By checking region codes for the
line section from P3'to P4 we find that the remainder of the line is below the clip window and can
be discarded also.
Intersection points with a clipping boundary can be calculated using the slope-intercept
form of the line equation. For a line with endpoint coordinates (x1,y1) and (x2,y2) and the y
coordinate of the intersection point with a vertical boundary can be obtained with the calculation.
y =y1 +m (x-x1)
where x value is set either to xwmin or to xwmax and slope of line is calculated as
m = (y2- y1) / (x2- x1)
the intersection with a horizontal boundary the x coordinate can be calculated as
x= x1 +( y- y1) / m
with y set to either to ywmin or to ywmax.
2.3.4 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 0<=u<=1
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
Each of these four inequalities can be expressed as
µpk <= qk. k=1,2,3,4
the parameters p & q are defined as
p1 = -∆x q1 = x1 - xwmin
p2 = ∆x q2 = xwmax - x1
P3 = -∆y q3 = y1- ywmin
P4 = ∆y q4 = ywmax - y1
Any line that is parallel to one of the clipping boundaries have pk=0 for values of k
corresponding to boundary k=1,2,3,4 correspond to left, right, bottom and top boundaries. For
values of k, find qk<0, the line is completely out side the boundary.
These four regions determine a unique boundary for the line segment. For instance, if P2 is in
region L, we clip the line at the left boundary and save the line segment from this intersection point
to P2. But if P2 is in region LT, we save the line segment from the left window boundary to the top
boundary. If P2 is not in any of the four regions, L, LT, LR, or LB, the entire line is clipped.
For the third case, when P1 is to the left and above the clip window, we usethe clipping regions in
Fig.
Fig : The two possible sets of clipping regions used in NLN algorithm when P1 is above and to
the left of the clip window
In this case, we have the two possibilities shown, depending on the position of P1, relative to the
top left corner of the window. If P2, is in one of the regions T, L, TR, TB, LR, or LB, this
determines a unique clip window edge for the intersection calculations. Otherwise, the entire line is
rejected.
To determine the region in which P2 is located, we compare the slope of the
line to the slopes of the boundaries of the clip regions. For example, if P1 is left of
the clipping rectangle (Fig. a), then P2, is in region LT if
slopeP1PTR<slopeP1P2<slopeP1PTL
or
yT – y1 < y2 – y1 < yT – y1
xR – x1 x2 – x1 xL – x1
And we clip the entire line if
(yT – y1)( x2 – x1) < (xL – x1 ) ( y2 – y1)
The coordinate difference and product calculations used in the slope tests are saved and also
used in the intersection calculations. From the parametric equations
x = x1 + (x2 – x1)u
y = y1 + (y2 – y1)u
an x-intersection position on the left window boundary is x = xL,, with
u= (xL – x1 )/ ( x2 – x1) so that the y-intersection position is
y = y1 + y2 – y1 (xL – x1 )
x2 – x1
And an intersection position on the top boundary has y = yT and u = (yT – y1)/ (y2 – y1) with
x = x1 + x2 – x1 (yT – y1 )
y2 – y1
For polygon clipping, we require an algorithm that will generate one or more closed areas that are
then scan converted for the appropriate area fill. The output of a polygon clipper should be a
sequence of vertices that defines the clipped polygon boundaries.
Clipping a polygon against the left boundary of a window, starting with vertex 1. Primed
numbers are used to label the points in the output vertex list for this window boundary.
vertices 1 and 2 are found to be on outside of boundary. Moving along vertex 3 which is inside,
calculate the intersection and save both the intersection point and vertex 3. Vertex 4 and 5 are
determined to be inside and are saved. Vertex 6 is outside so we find and save the intersection
point. Using the five saved points we repeat the process for next window boundary.
Implementing the algorithm as described requires setting up storage for an output list of vertices as
a polygon clipped against each window boundary. We eliminate the intermediate output vertex lists
by simply by clipping individual vertices at each step and passing the clipped vertices on to the
next boundary clipper.
A point is added to the output vertex list only after it has been determined to be inside or on a
window boundary by all boundary clippers. Otherwise the point does not continue in the pipeline.
A polygon overlapping a rectangular clip window
Processing the vertices of the polygon in the above fig. through a boundary clipping pipeline.
After all vertices are processed through the pipeline, the vertex list is { v2”, v2’, v3,v3’}
Left Right Bottom Top
in out
Clipper Clipper Clipper Clipper
The basic idea in this algorithm is that instead of always proceeding around the polygon
edges as vertices are processed, we sometimes want to follow the window boundaries. Which path
we follow depends on the polygon-processing direction (clockwise or counterclockwise) and
whether the pair of polygon vertices currently being processed represents an outside-to-inside pair
or an inside- to-outside pair. For clockwise processing of polygon vertices, we use the following
rules:
• For an outside-to-inside pair of vertices, follow the polygon boundary.
• For an inside-to-outside pair of vertices,. follow the window boundary in a clockwise
direction.
In the below Fig. the processing direction in the Weiler-Atherton algorithm and the resulting
clipped polygon is shown for a rectangular clipping window.
Curve-clipping procedures will involve nonlinear equations, and this requires more
processing than for objects with linear boundaries. The bounding rectangle for a circle or other
curved object can be used first to test for overlap with a rectangular clip window.
If the bounding rectangle for the object is completely inside the window, we save the object.
If the rectangle is determined to be completely outside the window, we discard the object. In either
case, there is no further computation necessary.
But if the bounding rectangle test fails, we can look for other computation-saving
approaches. For a circle, we can use the coordinate extents of individual quadrants and then octants
for preliminary testing before calculating curve-window intersections.
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.
Clipping a filled circle
A final method for handling text clipping is to clip the components of individual characters.
We now treat characters in much the same way that we treated lines. If an individual character
overlaps a clip window boundary, we clip off the parts of the character that are outside the window.
Exterior clipping:
Procedure for clipping a picture to the interior of a region by eliminating everything outside
the clipping region. By these procedures the inside region of the picture is saved. To clip a picture
to the exterior of a specified region. The picture parts to be saved are those that are outside the
region. This is called as exterior clipping.
Objects within a window are clipped to interior of window when other higher priority
window overlap these objects. The objects are also clipped to the exterior of overlapping windows.