Chpater4-Window and Clipping
Chpater4-Window and Clipping
Chpater4-Window and Clipping
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
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
GRAPHICS GRAPHICS
GRAPHICS