0% found this document useful (0 votes)
119 views24 pages

CS8092 Computer Graphics and Multimedia UNIT II-Two Dimensional Graphics 2.1 Two Dimensional Geometric Transformations

Uploaded by

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

CS8092 Computer Graphics and Multimedia UNIT II-Two Dimensional Graphics 2.1 Two Dimensional Geometric Transformations

Uploaded by

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

CS8092 Computer Graphics and Multimedia

UNIT II- Two Dimensional Graphics

2.1 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()
▪ 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.

Matrix Representation and homogeneous Coordinates


Many graphics applications involve sequences of geometric transformations. An animation, for
example, might require an object to be translated and rotated at each increment of the motion.

In order to combine sequence of transformations we have to eliminate the matrix addition. To


achieve this we have represent matrix as 3 X 3 instead of 2 X 2 introducing an additional dummy
coordinate h. Here points are specified by three numbers instead of two. This coordinate system is
called as Homogeneous coordinate system and it allows to express transformation equation as
matrix multiplication
Cartesian coordinate position (x,y) is represented as homogeneous coordinate triple(x,y,h)
Represent coordinates as (x,y,h)
• Actual coordinates drawn will be (x/h,y/h)
For Translation
 x '  1 0 t x   x 
 y ' = 0 1 t   y 
   y  

 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

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


 y ' =  sin  cos  0  y 
  
 1   0 0 1  1 
P' = R( ) • P
2.1.1 Composite Transformations
A composite transformation is a sequence of transformations; one followed by the other. we can
set up a matrix for any sequence of transformations as a composite transformation matrix by
calculating the matrix product of the individual transformations

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.

1 0 tx2 1 0 tx1 1 0 tx1 + tx2


0 1 ty2.0 1 ty1 = 0 1 ty1 + ty2
    
0 0 1  0 0 1  0 0 1 
Or
T(tx2,ty2).T(tx1,ty1) = T(tx1+tx2,ty1+ty2)
Which demonstrated the two successive translations are additive.
Rotations
Two successive rotations applied to point P produce the transformed position
P’=R(θ2).{R(θ1).P}={R(θ2).R(θ1)}.P
By multiplying the two rotation matrices, we can verify that two successive rotation are additive
R(θ2).R(θ1) = R(θ1+ θ2)
So that the final rotated coordinates can be calculated with the composite rotation matrix as
P’ = R(θ1+ θ2).P
cos  2 − sin  2 0 cos 1 − sin  1 0 cos( 2 +  1) − sin(  2 + 1) 0
 sin  2 cos  2 0. sin 1 cos 1 0 =  sin(  2 + 1) cos( 2 +  1) 0
    
 0 0 1  0 0 1  0 0 1
Scaling
Concatenating transformation matrices for two successive scaling operations produces the
following composite scaling matrix

 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

Which can also be expressed as T(xr,yr).R(θ).T(-xr,-yr) = R(xr,yr,θ)


General fixed point scaling
Translate object so that the fixed point coincides with the coordinate origin
Scale the object with respect to the coordinate origin
Use the inverse translation of step 1 to return the object to its original position

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

Implementation of composite transformations

2.1.2 Other Transformations


1. Reflection
2.Shear

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

Reflection of an object about the x axis

Reflection the x axis is accomplished with the transformation matrix


1 0 0 
 
0 − 1 0 
0 0 1 
 
Reflection of an object about the y axis
Reflection the y axis is accomplished with the transformation matrix
− 1 0 0 
 
0 1 0
0 0 1
 

Reflection of an object about the coordinate origin

Reflection about origin is accomplished with the transformation matrix

− 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

The Transformations matrix for x-shear is


1 sh x 0 
 
0 1 0
0 0 1 

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
The Transformations matrix for y-shear is
 1 0 0 
 
 sh y 1 0 
 0 0 1
 
which transforms the coordinates as
x’ =x
y’ = y+ sh y .x
XY-Shear
The transformation matrix for xy-shear

which transforms the coordinates as


x’ =x+ shx .y
y’ = y+ sh y .x
 x'  1 shx 0  x 
 y ' =  sh 1 0  y 
   y
 1   0 0 1  1 

Shearing Relative to other reference line


We can apply x shear and y shear transformations relative to other reference lines. In x shear
transformations we can use y reference line and in y shear we can use x reference line.
X shear with y reference line
We can generate x-direction shears relative to other reference lines with the transformation matrix
1 shx − shx . yref 
0 1 0 
 
0 0 1 
which transforms the coordinates as
x’ =x+ shx (y- y ref )
y’ = y
Example
Shx = ½ and yref=-1
Y shear with x reference line
We can generate y-direction shears relative to other reference lines with the transformation matrix
1 shx − shx . y ref 
0 1 0 
 
0 0 1 
which transforms the coordinates as
x’ =x
y’ = shy (x- xref) + y
Example
Shy = ½ and xref=-1

2.2 TWO DIMENSIONAL VIEWING

A world coordinate area selected for display is called a window


An area on a display device to which a window a mapped is called a view port
The window defines what is to be viewed
The viewport defines where it is to be displayed
The mapping of a part of a world coordinate scene to device coordinates is referred to as a viewing
transformation

The viewing pipeline


A world coordinate area selected for display is called a window. An area on a display device to
which a window is mapped is called a view port. The window defines what is to be viewed the
view port defines where it is to be displayed.
The mapping of a part of a world coordinate scene to device coordinate is referred to as viewing
transformation. The two dimensional viewing transformation is referred to as window to view port
transformation of windowing transformation.
A viewing transformation using standard rectangles for the window and viewport

The two dimensional viewing transformation pipeline


The viewing transformation in several steps, as indicated in Fig. First, we construct the
scene in world coordinates using the output primitives. Next to obtain a particular orientation for
the window, we can set up a two-dimensional viewing-coordinate system in the world coordinate
plane, and define a window in the viewing-coordinate system.
The viewing- coordinate reference frame is used to provide a method for setting up arbitrary
orientations for rectangular windows. Once the viewing reference frame is established, we can
transform descriptions in world coordinates to viewing coordinates.
We then define a viewport in normalized coordinates (in the range from 0 to 1) and map the
viewing-coordinate description of the scene to normalized coordinates.
At the final step all parts of the picture that lie outside the viewport are clipped, and the contents of
the viewport are transferred to device coordinates. By changing the position of the viewport, we
can view objects at different positions on the display area of an output device.

Window to view port coordinate transformation:

A point at position (xw,yw) in a designated window is mapped to viewport coordinates (xv,yv) so


that relative positions in the two areas are the same. The figure illustrates the window to view port
mapping.
A point at position (xw,yw) in the window is mapped into position (xv,yv) in the associated view
port. To maintain the same relative placement in view port as in window
xv − xvmin xw − xwmin
=
xvmax − xvmin xwmax − xwmin
yv − yvmin yw − ywmin
=
yv − yv yw − yw
solving these expressions for view port position (xv,yv)
(xvmax − xvmin )
xv = xvmin + (xw − xwmin )
xwmax − xwmin
( yv max − yv min )
yv = yv min + ( yw − ywmin )
ywmax − ywmin
where scaling factors are

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).

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.

To set up elements of window to view port mapping


evaluateViewMappingMatrix (xwmin, xwmax, ywmin, ywmax, xvmin, xvmax, yvmin, yvmax, error,
viewMappingMatrix)

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)

setWorkstationViewport (ws, xwsVPortmin, xwsVPortmax,


ywsVPortmin, ywsVPortmax)
where was gives the workstation number. Window-coordinate extents are specified in the range
from 0 to 1 and viewport limits are in integer device coordinates.

2.3 CLIPPING OPERATION


Any procedure that identifies those portions of a picture that are inside or outside of a specified
region of space is referred to as clipping algorithm or clipping. The region against which an
object is to be clipped is called clip window.

Algorithm for clipping primitive types:


• Point clipping
• Line clipping (Straight-line segment)
• Area clipping
• Curve clipping
• Text clipping
Line and polygon clipping routines are standard components of graphics packages.

2.3.1 Point Clipping


Clip window is a rectangle in standard position. A point P=(x,y) for display, if following
inequalities are satisfied:
xwmin <= x <= xwmax
ywmin <= y <= ywmax
where the edges of the clip window (xwmin,xwmax,ywmin,ywmax) can be either the world-
coordinate window boundaries or viewport boundaries. If any one of these four inequalities is not
satisfied, the point is clipped (not saved for display).
2.3.2 Line Clipping
A line clipping procedure involves several parts. First we test a given line segment whether it lies
completely inside the clipping window. If it does not we try to determine whether it lies completely
outside the window . Finally if we can not identify a line as completely inside or completely
outside, we perform intersection calculations with one or more clipping boundaries.
Process lines through “inside-outside” tests by checking the line endpoints. A line with both
endpoints inside all clipping boundaries such as line from P1 to P2 is saved. A line with both end
point outside any one of the clip boundaries line P3P4 is outside the window.

Line clipping against a rectangular clip window


All other lines cross one or more clipping boundaries. For a line segment with end points (x1,y1)
and (x2,y2) one or both end points outside clipping rectangle, the parametric representation could
be used to determine values of u for an intersection with the clipping boundary coordinates. If the
value of u for an intersection with a rectangle boundary edge is outside the range of 0 to 1, the line
does not enter the interior of the window at that boundary. If the value of u is within the range
from 0 to 1, the line segment does indeed cross into the clipping area. This method can be applied
to each clipping boundary edge in to determined whether any part of line segment is to displayed.
x = x1 + u (x2 − x1 ),
y = y1 + u ( y2 − y1 ), 0  u  1
2.3.3 Cohen-Sutherland Line Clipping
This is one of the oldest and most popular line-clipping procedures. The method speeds up
the processing of line segments by performing initial tests that reduce the number of intersections
that must be calculated.
Every line endpoint in a picture is assigned a four digit binary code called a region code
that identifies the location of the point relative to the boundaries of the clipping rectangle.

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.

If qk >=0, the line is inside the parallel clipping boundary.


When pk<0 the infinite extension of line proceeds from outside to inside of the infinite
extension of this clipping boundary.
If pk>0, the line proceeds from inside to outside, for non zero value of pk calculate the value
of u, that corresponds to the point where the infinitely extended line intersect the extension of
boundary k as
u = qk / pk
For each line, calculate values for parameters u1and u2 that define the part of line that lies
within the clip rectangle. The value of u1 is determined by looking at the rectangle edges for which
the line proceeds from outside to the inside (p<0).
For these edges we calculate
rk = qk / pk
The value of u1 is taken as largest of set consisting of 0 and various values of r. The value of u2 is
determined by examining the boundaries for which lines proceeds from inside to outside (P>0).
A value of rkis calculated for each of these boundaries and value of u2 is the minimum of
the set consisting of 1 and the calculated r values.
If u1>u2, the line is completely outside the clip window and it can be rejected.
Line intersection parameters are initialized to values u1=0 and u2=1. for each clipping
boundary, the appropriate values for P and q are calculated and used by function
Cliptest to determine whether the line can be rejected or whether the intersection parameter can be
adjusted.
When p<0, the parameter r is used to update u1.
When p>0, the parameter r is used to update u2.
If updating u1 or u2 results in u1>u2 reject the line, when p=0 and q<0, discard the line, it is
parallel to and outside the boundary.If the line has not been rejected after all four value of p and q
have been tested , the end points of clipped lines are determined from values of u1 and u2.
The Liang-Barsky algorithm is more efficient than the Cohen-Sutherland algorithm since
intersections calculations are reduced. Each update of parameters u1 and u2 require only one
division and window intersections of these lines are computed only once.
Cohen-Sutherland algorithm, can repeatedly calculate intersections along a line path, even
through line may be completely outside the clip window. Each intersection calculations require
both a division and a multiplication.

2.3.5 Nicholl-Lee-Nicholl Line clipping


By creating more regions around the clip window, the Nicholl-Lee-Nicholl (or NLN) algorithm
avoids multiple clipping of an individual line segment. In the Cohen-Sutherland method, multiple
intersections may be calculated.These extra intersection calculations are eliminated in the NLN
algorithm by carrying out more region testing before intersection positions are calculated.
Compared to both the Cohen-Sutherland and the Liang-Barsky algorithms, the Nicholl-Lee-
Nicholl algorithm performs fewer comparisons and divisions. The trade-off is that the NLN
algorithm can only be applied to two-dimensional dipping, whereas both the Liang-Barsky and the
Cohen-Sutherland methods are easily extended to three-dimensional scenes.
For a line with endpoints P1 and P2 we first determine the position of point P1, for the nine
possible regions relative to the clipping rectangle. Only the three regions shown in Fig. need to be
considered. If P1 lies in any one of the other six regions, we can move it to one of the three regions
in Fig. using a symmetry transformation. For example, the region directly above the clip window
can be transformed to the region left of the clip window using a reflection about the line y = -x, or
we could use a 90 degree counterclockwise rotation.
Three possible positions for a line endpoint p1(a) in the NLN algorithm

Case 1: p1 inside region


Case 2: p1 across edge
Case 3: p1 across corner
Next, we determine the position of P2 relative to P1. To do this, we create some new regions in the
plane, depending on the location of P1. Boundaries of the new regions are half-infinite line
segments that start at the position of P1 and pass through the window corners. If P1 is inside the
clip window and P2 is outside, we set up the four regions shown in Fig
The four clipping regions used in NLN alg when p1 is inside and p2 outside the clip window

The intersection with the


appropriate window boundary is
then carried out, depending on which one of the four regions (L, T, R, or B) contains P2. If both P1
and P2 are inside the clipping rectangle, we simply save the entire line.
If P1 is in the region to the left of the window, we set up the four regions, L, LT, LR, and LB,
shown in Fig.

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

2.3.6 POLYGON CLIPPING


To clip polygons, we need to modify the line-clipping procedures. A polygon boundary
processed with a line clipper may be displayed as a series of unconnected line segments (Fig.),
depending on the orientation of the polygon to the clipping window.

Display of a polygon processed by a line clipping algorithm

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.

Sutherland – Hodgeman polygon clipping:


A polygon can be clipped by processing the polygon boundary as a whole against each
window edge. This could be accomplished by processing all polygon vertices against each clip
rectangle boundary.
There are four possible cases when processing vertices in sequence around the perimeter of
a polygon. As each point of adjacent polygon vertices is passed to a window boundary clipper,
make the following tests:
1. If the first vertex is outside the window boundary and second vertex is inside, both the
intersection point of the polygon edge with window boundary and second vertex are
added to output vertex list.
2. If both input vertices are inside the window boundary, only the second vertex is added
to the output vertex list.
3. If first vertex is inside the window boundary and second vertex is outside only the edge
intersection with window boundary is added to output vertex list.
4. If both input vertices are outside the window boundary nothing is added to the output
list.

Clipping a polygon against successive window boundaries.


Successive processing of pairs of polygon vertices against the left window boundary

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

Weiler- Atherton Polygon Clipping


This clipping procedure was developed as a method for identifying visible surfaces, and so
it can be applied with arbitrary polygon-clipping regions.

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.

An improvement on the Weiler-Atherton algorithm is the Weiler algorithm, which applies


constructive solid geometry ideas to clip an arbitrary polygon against any polygon clipping region.

2.3.7 Curve Clipping

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

2.3.8 Text clipping


There are several techniques that can be used to provide text clipping in a graphics package.
The clipping technique used will depend on the methods used to
generate characters and the requirements of a particular application.
The simplest method for processing character strings relative to a window boundary is to
use the all-or-none string-clipping strategy shown in Fig. . If all of the string is inside a clip
window, we keep it. Otherwise, the string is discarded. This procedure is implemented by
considering a bounding rectangle around the text pattern. The boundary positions of the rectangle
are then compared to the window boundaries, and the string is rejected if there is any overlap. This
method produces the fastest text clipping.

Text clipping using a bounding rectangle about the entire string

An alternative to rejecting an entire character string that overlaps a window boundary is to


use the all-or-none character-clipping strategy. Here we discard only those characters that are not
completely inside the window .In this case, the boundary limits of individual characters are
compared to the window. Any character that either overlaps or is outside a window boundary is
clipped.
Text clipping using a bounding rectangle about individual characters.

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.

Text Clipping performed on the components of individual characters

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.

You might also like