Chpater4-Window and Clipping

Download as pdf or txt
Download as pdf or txt
You are on page 1of 49

Chapter 4

WINDOW AND
CLIPPING
INTRODUCTION
• There are two types of coordinate system
mainly-
1. World coordinate system
2. Screen coordinate system
1. WORLD COORDINATE SYSTEM
• The CAD Software stores graphical
information in the model database with
respect to coordinate system, called WCS
or Model Coordinate System
• This is the only coordinate system that
software recognizes when storing or
retrieving graphical information.
2) SCREEN COORDINATE SYSTEM
• It is the coordinate system which is used to
display screen.
• User input the graphical information with
respect to this coordinate system.
• It is also known as Working Coordinate
System or User Coordinate System.
COORDINATE SYSTEMS
WINDOW AND VIEWPORT
• When geometric model is complex and
crowded with intrinsic details, it is
difficult to read it. In such situation, it is
convenient to display only certain
portion of geometric model which is of
interest to used at that time.
• Here comes, effect of WINDOW and
VIEWPORT.
WINDOW
• It is the portion of geometric model which
is selected for viewing. This technique of
selecting portion is called Windowing.
• Generally, window is rectangular in shape
with edges parallel to axes of world
coordinate system
• It is the finite portion in World Coordinate
System.
VIEWPORT
• Viewport is the part of screen on which
window is displayed.
• Viewport is also rectangular in shape with
its edges parallel to axes of User
coordinate system.
• Content of Viewport can be edited in
viewport.
• By selecting number of windows on
geometric model, multiple viewport can be
placed on screen.
WINDOW TO VIEWPORT
TRANSFORMATION
• The mapping of geometric model from
world coordinate system to user coordinate
system is referred as Window to Viewport
transformation or Viewing
transformation.
• Transformation is implemented in such a
manner that relative position of all points
on object is not affected.
• Viewing transformation can be achieved
with the help of Scaling and Translation.
Windows- to- Viewport co-ordinate
transformation
• A window is specified by four world co-
ordinates 𝒙𝒘𝒎𝒊𝒏 , 𝒙𝒘𝒎𝒂𝒙 , 𝒚𝒘𝒎𝒊𝒏 , 𝒚𝒘𝒎𝒂𝒙 .
• Similarly a viewport is described by four
normalized device co- ordinates 𝒙𝒗𝒎𝒊𝒏 ,
𝒙𝒗𝒎𝒂𝒙 , 𝒚𝒗𝒎𝒊𝒏 , 𝒚𝒗𝒎𝒂𝒙 .
• The objective of the window to viewport
mapping is to convert the world coordinate
(xw ,yw) of any point to its corresponding
normalized device coordinates (xv,yv).
• In order to maintain the same relative
placement of the point in the viewport as in
the window, we require,
• Solve these expressions for viewport position
(xv, yv), we can rewrite above equation.
• Similarly for Y,
• The conversion is performed with the
following sequence of transformation:
• Perform a scaling transformation using
fixed point position of (𝒙𝒘𝒎𝒊𝒏 , 𝒚𝒘𝒎𝒊𝒏 ) that
scales the window area to the size of the
viewport.
• Translate the scaled window area to the
position of the viewport.
Clipping
• Definition:- Elimination of parts of scene
outside a window or viewport is called
clipping.
• Any procedure that identifies those
portions of a picture that are either inside
or out-side of a specified region of space is
referred to as a clipping algorithm, or
simply clipping.
• On Viewport only those entities are supposed
to be visible which are inside the windows in
WCS.
• If a line is drawn outside the boundaries of
window then it will not be visible in viewport
of screen coordinate system.
• Portion which lies outside the boundary of
window is clipped and only the part which
lies inside the boundary of window is visible
in viewport.
In the following sections, we consider
algorithms for clipping the following primitive
types
• Point Clipping
• Line Clipping (straight-line segments)
• Area Clipping (polygons)
• Curve Clipping
• Text Clipping
Point Clipping
• Assuming that the clip window is a rectangle in
standard position, we save a point P = (x, y) for
display if the following inequalities are satisfied:
𝒙𝒘𝒎𝒊𝒏 ≤ 𝒙 ≤ 𝒙𝒘𝒎𝒂𝒙
𝒚𝒘𝒎𝒊𝒏 ≤ 𝒚 ≤ 𝒚𝒘𝒎𝒂𝒙
• where the edges of the clip window (𝒙𝒘𝒎𝒊𝒏 ,
𝒙𝒘𝒎𝒂𝒙 , 𝒚𝒘𝒎𝒊𝒏 , 𝒚𝒘𝒎𝒂𝒙 ) 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.
• The point (x, y) lies inside the clip area:
Line Clipping
• A Line clipping procedure involves several parts.
• First, we can test a given line segment to determine
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 cannot identify a line as completely
inside or completely outside, we must perform
intersection calculations with one or more clipping
boundaries.
• We process lines through the "inside-outside'' tests
by checking the line endpoints.
BIT CODE METHOD
ALGORITHM FOR CLIPPING LINE
SEGMENT
1. Find 4-bit code for point P1 AND P2.
2. Check whether condition 1 is satisfied or not. If
it is satisfied then, line is invisible.
3. If condition 2 is satisfied then line is visible.
4. If both condition 1 and condition 2 are not
satisfied then line is partially visible an
partially invisible. In that case, we find
intersection point of line with edges of
window.
Cohen-Sutherland line clipping:
• The Cohen-Sutherland algorithm is a line clipping
algorithm.
• This is the efficient algorithm which performs initial
tests on a line to determine whether intersection
calculations can be avoided.
• The algorithm divides a 2D space into 9 regions, of
which only the middle part (viewport) is visible.
• Every line end point in a picture is assigned a four-
digit binary code, called a region code.
• Region code identifies the location of the point
relative to the boundaries of the clipping rectangle.
Observation:- All lines fall into one of three
categories:
1. Both endpoints lie inside the clip rectangle
- Accept entire line.
2. Both endpoints outside clip rectangle on
the same side of one of its borders reject
entire line.
3. Neither 1 nor 2 - Clip part of line outside
one of borders and repeat.
Cohen-Sutherland algorithm:
• Step 1. A 4-bit region code number is assigned to an
endpoint (x, y).
• Step 2. Bit one is set to 1 if x < 𝒙𝒘𝒎𝒊𝒏 . Other three bits
can be determined similarly.
• Step 3. A value of 1 in any bit position indicates that the
point is in the relative position; otherwise the bit position
is set to 0.
• Step 4. Once the region codes for all line endpoints are
known, we can determine which line are completely
inside the clip window or completely outside.
• Step 5. The lines that are completely within the window
boundaries have a region code of 0000 for both
endpoints, we accept these lines.
• Step 6. Any lines that a 1 in the same bit position in
the region codes for each endpoint are completely
outside the clipping rectangle, we reject these lines.
• Step 7. Lines are tested for total clipping by
performing the logical AND operation with both
region codes.
• Step 8. If the result is not 0000, the line is completely
outside the clipping region (Reject).
• Step 9. If the result is 0000, the line is neither
completely outside nor inside.
• Step 10. So the algorithm checks the line endpoints
against window boundaries in order left, right, bottom
and top
• Step 11. Intersection points are calculated using equation
parameters.
• Step 12. For a line with endpoint (𝒙𝟏 , 𝒚𝟏 ) and (𝒙𝟐 , 𝒚𝟐 ) the
y coordinate of the intersection point with a vertical
boundary can be calculated as:
𝒚 = 𝒚𝟏 + 𝒎 𝒙 − 𝒙𝟏
Where 𝐱 = 𝒙𝒘𝒎𝒊𝒏 𝒐𝒓 𝒙𝒘𝒎𝒂𝒙
And slope of the line is
(𝒚𝟐 −𝒚𝟏 )
m=
(𝒙𝟐 −𝒙𝟏 )
• Similarly the intersection point with a horizontal boundary, the a
coordinates can be calculated as
(𝒚𝟐 −𝒚)
𝐱 = 𝒙𝟏 +
𝒎
Where y= 𝒚𝒘𝒎𝒊𝒏 𝒐𝒓 𝒚𝒘𝒎𝒂𝒙
Example
Polygon Clipping
• A polygon clipping, algorithm 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
• Sutherland-Hodgeman polygon clipping is used
for polygon clipping.
• The algorithm operates on the vertices of the
polygon.
• The simple problem is to clip a polygon against a
single clipping edge.
• We clip the polygon on all four edges by clipping
the entire polygon against one edge, then taking
the resulting polygon and clipping against a
second edge and so on for all four edges.
Figure: Clipping a polygon against successive window boundaries
• There are four possible cases when processing vertices in
sequence. Each pair of adjacent polygon vertices is passed
to a window boundary clipper.
We make the following tests:
• Case 1: If the first vertex is outside the window boundary
and the second vertex is in-side, both the intersection point
of the polygon edge with the wind boundary and the second
vertex are added to the output vertex list.
• Case 2: If both input vertices are inside the window
boundary, only the second vertex is added to the output
vertex list.
• Case 3: If the first vertex is inside the window boundary
and the second vertex is out-side, only the edge intersection
with the window boundary is added to the output vertex list.
• Case 4: If both input vertices are outside the window
boundary, nothing is added to the output list.
• These four cases are illustrated in diagram for successive pairs of
polygon vertices.
Example: We illustrate the example by processing
the area against the left window boundary.

Window
3
2 2’

1
4
5’
6
5

Fig: Clipping Polygon against the left boundary


Traversal Type Action
Vertices 1-> 2 out – out Don’t save
Vertices 2-> 3 out – in Save intersection point 2’ & 3
Vertices 3-> 4 in – in Save 4
Vertices 4-> 5 in – in Save 5
Vertices 5-> 6 in – out Save intersection point 5’
Vertices 6-> 1 Out – out Don’t save

Using the five saved points repeat the process for


the next window boundary
Curve Clipping
• Curve-clipping procedures will involve nonlinear equations,
however, 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.
• For a circle, we can use the coordinate extents of individual
quadrants and then octants for preliminary testing before
calculating curve-window intersections.
• For an ellipse, we can test the coordinate extents of
individual quadrants.
• When clipping a curved object against a general polygon
clip region.
• 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.
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 used with 3 categories
1) All-or-none String clipping
2) All-or-none Character clipping
3) Components of individual Characters:
1) All-or-none String clipping:
• 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.

GRAPHICS GRAPHICS
GRAPHICS

GRAPHICS
Before Clipping After Clipping
2) All-or-none Character clipping:
• We use this for rejecting an entire character string that
overlaps a window boundary
• 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.

GRAPHICS GRAPHICS
GRAPHICS HICS

Before Clipping After Clipping


3) 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.
• Outline character fonts formed with line segments can be
processed in this way using a line clipping algorithm.
• Characters defined with bit maps would be clipped by
comparing the relative position of the individual pixels in
the character grid patterns to the clipping boundaries.

GRAPHICS GRAPHICS

GRAPHICS

Before Clipping After Clipping


EXTERIOR CLIPPING
• We have considered only procedures for clipping a picture
to the interior of a region by eliminating everything
outside the clipping region.
• What is saved by these procedures is inside the region.
• We want 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 referred to as exterior clipping.
• Example multiple window systems.
KLE BCA DWD
• To correctly display the screen windows, we often need to
apply both internal and external clipping.
• Objects within a window are clipped to the interior of that
window.
• When other higher-priority windows overlap these
objects, the objects are also clipped to the exterior of the
overlapping windows.
• Exterior clipping is used also in other applications that
require overlapping pictures.
• Examples here include the design of page layouts in
advertising or publishing applications or for adding labels
or design patterns to a picture.
• The technique can also be used for combining graphs,
maps, or schematics.
• For these applications, we can use exterior clipping to
provide a space for an small picture to insert into a larger
picture.

You might also like