07.2D Viewing Clipping MCA
07.2D Viewing Clipping MCA
&
Clipping
Computer Graphics – M.C.A.
PG Wing of SBRR Mahajana FG College
Mysore
2017-18
Windows & Viewports
Windows & Viewports
➔Window
➔ A world-coordinate area selected for display.
➔ defines what is to be viewed
➔Viewport
➔ Anarea on a display device to which a window is
mapped.
➔ defines where it is to be displayed
➔Viewing transformation
➔ The mapping of a part of a world-coordinate scene
to device coordinates.
➔A window could be a rectangle to have any orientation.
Windows & Viewports
2D Viewing Transformation Pipeline
2D Viewing Transformation - Terminology
➔Window
➔ A world-coordinate area selected for display.
➔ defines what is to be viewed
➔Viewport
➔ An area on a display device to which a
window is mapped.
➔ defines where it is to be displayed
➔Viewing transformation
➔ The mapping of a part of a world-coordinate
scene to device coordinates.
2D Viewing Transformation - Terminology
➔Modelling Coordinate System – Representation
of an object measured in some physical units
➔World Coordinate System (Object Space) -
Representation of a set of objects, all measured in
the same physical units.
➔Screen Coordinate System (Image Space) - The
space within the image is displayed
➔Interface Window - The visual representation of
the screen coordinate system for “window” displays
(coordinate system moves with interface window).
2D Viewing Transformation – Viewing Effects
➔Zooming effects
➔ Successively mapping different-sized windows on a
fixed-sized viewports.
➔Panning effects
➔ Moving a fixed-sized window across the various
objects in a scene.
2D Viewing Normalised Coordinates
➔Viewports are typically defined within the unit square
(normalized coordinates). This makes them Device
independent
Window to Viewport Transformation
➔Need to maintain relative size and position between
clipping window and viewport.
Window to Viewport Transformation
Workstation Transformation
➔Sometimes, a single viewport (in normalised
coordinates) may be mapped to different video
monitors with
workstation
transformations.
Clipping
➔Any procedure that identifies those portions of a
picture that are either inside or outside of a specified
region of space is called as a clipping algorithm or
simply clipping.
➔The region against which an object is to be clipped is
called a clip window.
Clipping
➔Point Clipping
➔Line Clipping (straight line segments)
➔Area Clipping (Polygon Clipping)
➔Curve Clipping
➔Text Clipping
Line Clipping
➔Parametic equiation of a line segment from (x 1, y1) and
(x2, y2) :
x = x1 + u(x2 - x1 )
y = y1 + u(y2 - y1 ) where 0 ≤ u ≤ 1
Line Clipping
➔A line can be :
➔ Completely inside the clipping window
➔ Completely outside the window
➔ Partially inside the window
Cohen-Sutherland Line Clipping Algorithm
➔Intersection calculations are expensive.
➔Main Idea :
➔ Find first lines completely inside or certainly outside
clipping window.
➔ Apply intersection only to undecided lines.
➔ Perform cheaper tests before proceeding to
expensive intersection calculations.
➔How ?
➔ Assign code to every endpoint of line segment.
Cohen-Sutherland Line Clipping Algorithm
➔Assign code to every endpoint of line segment.
➔ Borderlines of clipping window divide the plane into 9
regions.
➔ A point can be characterized by a 4-bit code according
to its location with respect to each of the borderlines.
➔ Location bit is 0 if the
point is inside,
1 otherwise.
➔ Code assignment
involves comparisons
or subtractions.
Cohen-Sutherland Line Clipping Algorithm
➔How to calculate OutCodes ?
➔ Calculate differences between endpoint coordinates
and clipping boundaries.
➔ Use the resultant
sign bit of each
difference
calculation to set
the corresponding
bit value.
Cohen-Sutherland Line Clipping Algorithm
➔Endpoint codes are 0000 for both iff line is completely
inside.
➔If endpoint codes has 1 in same bit, line is certainly
outside.
➔Lines that cannot be decided are intersected with
window border lines.
Cohen-Sutherland Line Clipping Algorithm
➔Case 1:
➔ Both endpoints of line segment inside all four lines
➔ Draw (accept) line segment as is
➔ AB: outcode(A) = outcode(B) = 0000
Cohen-Sutherland Line Clipping Algorithm
➔Case 2: Both endpoints outside all lines and on same
side of a line
➔ Discard (reject) the line segment
➔ EF: outcode(E) logically ANDed with outcode(F)
(bitwise) ≠ 0
➔ Both outcodes have a 1 bit in the same place
➔ Line segment is outside of corresponding side of
clipping window
Cohen-Sutherland Line Clipping Algorithm
➔Case 3: One endpoint inside, one outside
➔ Must do at least one intersection
➔CD: outcode (C) = 0, outcode(D) ≠ 0
➔ Location of 1 in outcode(D) determines which edge to
intersect with
➔ Note if there were a segment from C to a point in a
region with 2 ones in outcode, we might have to do
two interesections
Cohen-Sutherland Line Clipping Algorithm
➔Case 4: Both outside
➔ May have part inside. Must do at least one intersection
➔GH and IJ: same outcodes, neither zero but logical AND
yields zero
➔ Shorten line segment by intersecting with one of sides
of window Compute outcode of intersection
(newendpoint of shortened line segment)
➔ Reexecute
algorithm
Cohen-Sutherland Line Clipping Algorithm
➔Efficiency :
➔ Brute-Force approach :
➔ Compute intersections with all sides of clipping
window
➔ Inefficient: one division per intersection
➔ Cohen-Sutherland Algorithm - Advantages:
➔ In many applications, the clipping window is small
relative to the size of the entire data base
➔ Most line segments can be eliminated based on
their outcodes.
➔ Intersections calculated only for remaining lines.
Cohen-Sutherland Line Clipping Algorithm
➔Cohen-Sutherland Algorithm – Disadvantages :
➔ For undecided lines, we still have to perform upto 4
intersection calculations.
➔ Worst case scenario : A very long line, that lies totally
outside the clipping window, but not on the same side,
will require upto 4 intersection calculations before we
reject that line.
Mid-Point Subdivision Algorithm
➔This method is normally used along with Cohen-
Sutherland algorithm.
➔Cohen-Sutherland algorithm will accept those lines that
are totally inside and reject those lines that are totally
outside (on one side of the window).
➔For undecided lines, instead of computing the
intersection points, this Mid-Point Subdivision method
uses binary search method to search for the intersection
point.
➔ Divide the line in the middle and check if each of the
line segments is inside or outside.
Mid-Point Subdivision Algorithm
➔Divide the line in the middle and run Cohen-Sutherland
algorithm on each of the new line segments.
➔ If the segment is totally inside, accept it, and return.
➔ If it is totally outside, reject it, and return
➔ If undecided (i.e., case 3 and case 4), again divide
that line into two segments in the middle.
➔This goes on till segments are
either accepted or rejected.
Mid-Point Subdivision Algorithm
➔Advantage :
➔ No multiplication/division for intersection calculation.
➔ The subdivision search for the intersection point is of
order of logarithmetic complexity.
Polygon Clipping
➔Not as simple as line segment clipping
➔ Clipping a line segment yields at most one line
segment
➔ Clipping a polygon can yield multiple polygons