Computer Graphics and Animation
Unit 3: Clipping
               By
         Gaurav Bhattarai
            GauravBhattarai
     Two Dimensional Viewing (2D-Viewing)
• It is a formal mechanism for displaying views of a picture on an output device.
• A graphics package allows the user to specify:
     ➢ Which part of the defined picture is to be displayed(Window).
     ➢ Where the part is to be placed on the display device(Viewport).
     ➢ A view is selected by specifying a sub area of the total picture area.
• Much like what we see in real life through a small window or the view finder of a
  camera.
• In computer graphics several coordinate systems (reference frame) are used to
  construct, select views, and display a 2D scene or image.
     • Modeling coordinate system
           • It is used to define coordinates that are used to construct the shape of
              individual parts (objects) of a 2D scene.
     • World Coordinate system
           • Used to organize the individual objects (points, lines, circles, etc.) into a
              scene.
           • A scene is made up of a collection of objects.
           • These objects make up the "scene" or "world" that we want to view, and
              the coordinates that we use to define the scene are called world
              coordinates.
                                         GauravBhattarai
• Viewing coordinate system
    • Used to define particular view of a 2D scene. Translation, scaling, and
      rotation of the window will generate a different view of the scene.
    • For a 2-D picture, a view is selected by specifying a subarea of the total
      picture area.
• Normalized viewing coordinates
   • These are viewing coordinates between 0 and 1. The lower left corner
      corresponds to (0, 0), and the upper right corner corresponds to (1, 1).
   • NVC’s are used to make the viewing process independent of the output
      device (monitor, mobile, hard copy devices).
   • Clipping is usually and more efficiently done in these coordinates.
                     Fig: several view of the same car
                                   GauravBhattarai
Device Coordinate or Screen Coordinate system
• Used to define coordinates in an output device.
• Device coordinates are integers within the range (0, 0) to (xmax, ymax) for a particular
  output device.
• Depends on screen resolution.
• Normalized Device Coordinates:
    ➢ These are viewing coordinates between 0 and 1.
    ➢ Device coordinates are specific to output device: printer page, screen display,
       etc. but Normalized Device co-ordinates are independent.
• Any convenient Cartesian coordinate system; referred to as the world coordinate
  reference frame can be used to define the picture. The picture parts within the
  selected areas are then mapped onto specified areas of the device co-ordinates.
                                           GauravBhattarai
Viewing Pipeline/2D-viewing transformation
• The process of mapping of a part of world coordinate scene to device coordinate is
   called the 2D viewing transformation.
• That is, it is a process of mapping the coordinates of the points and lines that form
   the picture into the appropriate coordinates on the device where the image is to
   be displayed. Sometimes also referred to as the window-to-viewport
   transformation or the windowing transformation.
• Transformations from world to device coordinates involve translation, rotation and
   scaling operations, as well as procedures for deleting those parts of the picture that
   are outside the limits of a selected display area(clipping).
                        Fig: Two-dimensional viewing pipeline
Window
• A world-coordinate area selected for display is called a window. That is, window is
  the section of the 2D scene that is selected for viewing.
• The window defines what is to be viewed.
• A window can be specified by four world    coordinates: xwmin, xwmax, ywmin and
                                         GauravBhattarai
  ywmax.
Viewport
• An area on a display device to which a window is mapped is called a viewport.
• The viewport indicates where on an output device selected part will be displayed.
• A viewport can be described by four device coordinates: xvmin, xvmax, yvmin and
   yvmax.
Figure given below illustrates the mapping of a 2D picture that falls within a
rectangular window onto a designated rectangular viewpoint.
                                        GauravBhattarai
• Window and viewport are often rectangular in standard positions, because it
  simplifies the transformation process and clipping process.
• Other shapes such as polygons, circles take longer time to process.
Applications:
• By changing the position of the viewport, we can view objects at different positions
  on the display area of an output device.
• Also by varying the size of viewports, we can change size of displayed objects.
• Zooming effects can be obtained by successively mapping different-sized windows
  on a fixed-sized viewport.
• Panning effects are produced by moving a fixed-size window across the various
  objects in a scene.
                                         GauravBhattarai
    Windows to Viewport Transformation
• It is the mechanism for displaying view of a picture on an output device.
• The world coordinate selected for display is called window.
• The area on the display device to which window is mapped is called viewport.
• So, window defines what is to be viewed and viewport defines where it is to be
  displayed.
• The mapping of part of world co-ordinate scene to device co-ordinate is called
  viewing transformation or window-to-viewport transformation.
                                   GauravBhattarai
GauravBhattarai
GauravBhattarai
Q) Window port is given by (100, 100, 300, 300) and viewport is given by (50, 50, 150,
150). Convert the window port coordinate (200, 200) to the view port coordinate.
                                          GauravBhattarai
Q) Find the normalization transformation matrix for window to viewport which uses
the rectangle whose lower left corner is at (2, 2) and upper right corner is at (6, 10)
as a window and the viewport that has lower left corner at (0, 0) and upper right
corner at (1,1).
                                         GauravBhattarai
Q) A world coordinate & viewport have the following geometry:
         Window (left, right, bottom, top) = (200, 600, 100, 400)
         Viewport (left, bottom, width, height) = (0, 0, 800, 600)
The following vertices are drawn in the world:- P1: (356, 125), P2: (200, 354), P3:
(230,400), P4: (564, 200). What coordinate will each occupy in viewport?
                                          GauravBhattarai
GauravBhattarai
      Clipping
• The process of discarding (cutting off) those parts of a picture which are outside of
  a specified region is called clipping.
• Any procedure that identifies those parts of a picture that are either inside or
  outside of the specified region is called a clipping algorithm.
• The region against which the clipping operation is performed is called a clip
  window.
                   Before clipping                          After Clipping
                                         GauravBhattarai
Why Clipping?
• Excludes unwanted graphics from the screen.
• Improves efficiency, as the computation dedicated to objects that appear off
  screen can be significantly reduced.
• Extracting part of a defined scene for viewing.
• Identifying visible surfaces in three-dimensional views.
• Antialiasing line segments or object boundaries.
• Creating objects using solid-modeling procedures.
• Displaying a multi-window environment.
• Drawing and painting operations that allow parts of a picture to be selected for
  copying, moving, erasing, or duplicating.
Applications
• Video editing
• Image Editing
• Defining Animation paths
                                      GauravBhattarai
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.
Algorithm:
1. Get the minimum and maximum coordinates of the viewing plane.
2. Get the coordinates for a point.
3. Check whether given input lies between minimum and maximum coordinates of
   viewing plane.
4. If yes, display the point which lies inside the region,
   otherwise discard it.
                                        GauravBhattarai
Line Clipping
In line clipping, a line or part of line is clipped if it is outside the window port. There
are three possibilities for the line:
• Line can be completely inside the window (This line should be accepted).
• Line can be completely outside of the window (This line will be completely
    removed from the region).
• Line can be partially inside the window (We will find intersection point and draw
    only that portion of line that is inside region).
                                           GauravBhattarai
   Cohen-Sutherland Line Clipping Algorithm
1. Assign the four digit binary value called region code to each end point of a line.
     • A region code is represented as TBRL with 0000 inside clipping window.
     • To calculate region code, perform following steps:
        ➢ Calculate the difference between endpoint coordinates and clipping
           boundary
           i.e. 𝑥 – 𝑥𝑤𝑚𝑖𝑛 , 𝑥𝑤𝑚𝑎𝑥 – 𝑥, 𝑦 – 𝑦𝑤𝑚𝑖𝑛 & 𝑦𝑤𝑚𝑎𝑥 – 𝑦.
        ➢ Use ‘1’ when resultant sign is – 𝑣𝑒 otherwise use ‘0’.
                                       GauravBhattarai
2. Determine which lines are completely inside the clipping window & which lines are
   completely outside.
     • Perform OR operation on line endpoint region code, if we get 0000, the line is
        completely inside clipping window & save these points.
     • Perform AND operation on line endpoint region code, if we get value not
        equal to 0000, the line is completely outside & discard these points.
3. If condition 2 fails, the line crosses the clipping window boundary & find the point
   of intersection.
    • Windows edges are processed in TBRL. Here, the region code for point 𝑃1 is
      0100 & 𝑃2 is 1001.
    • To decide which boundary edges the line crosses, check for the bit position in
      line endpoint.
    • Line crosses these window boundary edges for which bit position value are
      opposite.
         Here,
                                         GauravBhattarai
         Since, the value at T, B & L are opposite, the line P1-P2 crosses the clipping
         window at top, bottom & left edge.
• Now find the point of intersection with the clipping window edge.
• For calculation of intersection point, first find the slope,
                                      GauravBhattarai
Q) Consider a rectangle clipping window with A(20, 20), B(90, 20), C(90, 70) & D(20,
70). Clip the line P1P2 with P1(10, 30) & P2(80, 90) using Cohen-Sutherland line
clipping algorithm.
                                       GauravBhattarai
GauravBhattarai
GauravBhattarai
Q) Use the Cohen-Sutherland algorithm to clip the line P1(70, 20) and P2(100, 10)
against a window lower left hand corner (50, 10) and upper right hand corner (80,
40).
                                      GauravBhattarai
GauravBhattarai
   Liang-Barsky Line Clipping Algorithm
This algorithm is considered to be the faster parametric line-clipping algorithm. The
following concepts are used in this clipping:
1. The parametric equation of the line.
2. The inequalities describing the range of the clipping window which is used to
    determine the intersection between the line and the clip window.
The parametric equation of a line can be given by,
                                                                   -u ∆x ≤ x1- xwmin
                                                                    u ∆x ≤ xwmax - x1
                                                                   -u ∆y ≤ y1- ywmin
                                                                    u ∆y ≤ ywmax – y1
                                       GauravBhattarai
                  u= q1/p1
                  u= q2/p2
                  u= q3/p3
                  u= q4/p4
GauravBhattarai
Else
       Calculate the endpoints of the clipped line as follows:
                 xx1= x1 + u1 ∆ x
                 xx2= x1 + u2 ∆ x
                 yy1= y1 + u1 ∆ y
                 yy2= y1 + u2 ∆ y
                                    GauravBhattarai
GauravBhattarai
Q) Apply Liang Barsky Line Clipping algorithm to the line with coordinates (30, 60)
and (60, 25) against the window (𝒙𝒘𝒎𝒊𝒏 , 𝒚𝒘𝒎𝒊𝒏 ) = (𝟏𝟎, 𝟏𝟎) and (𝒙𝒘𝒎𝒂𝒙, 𝒚𝒘𝒎𝒂𝒙 ) =
(𝟓𝟎, 𝟓𝟎).
                                         GauravBhattarai
GauravBhattarai
                  Note
                  Here,
                          t1= u1
                          t2= u2
GauravBhattarai
GauravBhattarai
Polygon Clipping
              GauravBhattarai
GauravBhattarai
Q) Clip polygon against clipping window PQRS. The coordinates of polygon are A(80,
200), B(220, 120), C(150, 100), D(100, 30), E(10, 120). Coordinates of clipping window
P(200, 50), Q(50, 150), R(200, 150) & S(50, 50).
                                         GauravBhattarai
GauravBhattarai
GauravBhattarai
Thank You
   GauravBhattarai