20072cgraphics-unit 8b 2d clipping v2

Upload: altaf188

Post on 08-Apr-2018

226 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/6/2019 20072CGraphics-Unit 8B 2D Clipping v2

    1/44

    1

    Computer Graphics

    311321

  • 8/6/2019 20072CGraphics-Unit 8B 2D Clipping v2

    2/44

    2

    CHAPTER SIX

    Two-DimensionalClipping

  • 8/6/2019 20072CGraphics-Unit 8B 2D Clipping v2

    3/44

    Clipping

    For the image below consider which points shouldbe kept and which ones should be clipped

    wymax

    wymin

    wxmin wxmax

    Window

    P1

    P2

    P3P6

    P5P7

    P10

    P9

    P4

    P8

  • 8/6/2019 20072CGraphics-Unit 8B 2D Clipping v2

    4/44

    Point Clipping

    Easy - a point (x,y) is not clipped if: wxmin x wxmax AND wymin y wymaxotherwise it is clipped

    wymax

    wymin

    wxmin wxmax

    Window

    P1

    P2

    P5

    P7

    P10

    P9

    P4

    P8

    Clipped

    Points Within the Window

    are Not Clipped

    Clipped

    Clipped

    Clipped

  • 8/6/2019 20072CGraphics-Unit 8B 2D Clipping v2

    5/44

    5

    Two-Dimensional Point Clipping

    For a clipping rectangle in standard position, we save a two-

    dimensional point P = (x,y) for display if the following inequalities

    are satisfied:

    If any of these four inequalities is not satisfied, the point is clipped

    (not saved for display).

    Point clipping is useful in applications when pictures are modeledwithparticle systems. For example, scenes involving clouds and

    smoke.

    12)-(6maxmin

    maxmin

    ywyyw

    xwxxw

  • 8/6/2019 20072CGraphics-Unit 8B 2D Clipping v2

    6/44

    Point Clipping

    6

    Exercise: P1 = (10, 20), P2 = (30, 50), P3 = (60, 90), and

    P4 = (130, 150). Suppose that the coordinates of the two

    opposite corners of the clip window are

    (xwmin, ywmin) = (30, 30) and (xwmax, ywmax) = (130, 110).Which of the above points will be clipped?

  • 8/6/2019 20072CGraphics-Unit 8B 2D Clipping v2

    7/44

    7

    Two-Dimensional Line Clipping

    Figure 6-11 illustrates possible positions for straight-line segments

    in relationship to a standard clipping window.

    A line-clipping algorithm processes each line in a scene through a

    series of tests and intersection calculations to determine whether

    the entire line or any part of it is to be saved for display.

  • 8/6/2019 20072CGraphics-Unit 8B 2D Clipping v2

    8/44

    Line Clipping

    8

  • 8/6/2019 20072CGraphics-Unit 8B 2D Clipping v2

    9/44

    Sutherland: Line Clipping

    9

    - 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 thenumber of intersections that must be calculated.

    - Efficient method of accepting or rejecting lines that

    dont intersect the window edges.

  • 8/6/2019 20072CGraphics-Unit 8B 2D Clipping v2

    10/44

    Sutherland: Line Clipping

    10

    Lines that cross a boundary of the window, are clipped

    by computing the coordinates of the new boundary

    endpoint of the line where it crosses the edge of the window.

    Assign a binary 4 bit code to each vertex by comparing

    it to clip coordinates:

    Bit 1 (left): the sign bit ofxxwmin.

    Bit 2 (right): the sign bit ofxwmaxx.

    Bit 3 (below): the sign bit ofy - ywmin

    Bit 4 (above): the sign bit ofywmaxy.

  • 8/6/2019 20072CGraphics-Unit 8B 2D Clipping v2

    11/44

    Sutherland: World Division

    11

  • 8/6/2019 20072CGraphics-Unit 8B 2D Clipping v2

    12/44

  • 8/6/2019 20072CGraphics-Unit 8B 2D Clipping v2

    13/44

    Sutherland: Labelling

    Every end-point is labelled with theappropriate region code

    wymax

    wymin

    wxmin wxmax

    Window

    P3 [0001]P6 [0000]

    P5 [0000]

    P7 [0001]

    P10 [0100]

    P9 [0000]

    P4 [1000]

    P8 [0010]

    P12 [0010]

    P11 [1010]

    P13 [0101] P14 [0110]

  • 8/6/2019 20072CGraphics-Unit 8B 2D Clipping v2

    14/44

    Sutherland: Lines In The Window

    Lines completely contained within the windowboundaries have region code [0000] for both

    end-points so are not clipped

    wymax

    wymin

    wxmin wxmax

    Window

    P3 [0001]P6 [0000]

    P5 [0000]

    P7 [0001]

    P10 [0100]

    P9 [0000]

    P4 [1000]

    P8 [0010]

    P12 [0010]

    P11 [1010]

    P13 [0101] P14 [0110]

  • 8/6/2019 20072CGraphics-Unit 8B 2D Clipping v2

    15/44

    Sutherland: Lines Outside The Window

    Any lines with a common set bit in the regioncodes of both end-points can be clipped

    The AND operation can efficiently check this

    wymax

    wymin

    wxmin wxmax

    Window

    P3 [0001]P6 [0000]

    P5 [0000]

    P7 [0001]

    P10 [0100]

    P9 [0000]

    P4 [1000]

    P8 [0010]

    P12 [0010]

    P11 [1010]

    P13 [0101] P14 [0110]

  • 8/6/2019 20072CGraphics-Unit 8B 2D Clipping v2

    16/44

    Sutherland: Other Lines

    Lines that cannot be identified ascompletely inside or outside the window mayor may not cross the window interior

    These lines are processed as follows: Compare an end-point outside the window to a

    boundary (choose any order in which toconsider boundaries e.g. left, right, bottom, top)

    and determine how much can be discarded

    If the remainder of the line is entirely inside oroutside the window, retain it or clip itrespectively

  • 8/6/2019 20072CGraphics-Unit 8B 2D Clipping v2

    17/44

    Sutherland: Other Lines (cont)

    Otherwise, compare the remainder of the lineagainst the other window boundaries

    Continue until the line is either discarded or a

    segment inside the window is foundWe can use the region codes to determinewhich window boundaries should beconsidered for intersection

    To check if a line crosses a particularboundary we compare the appropriate bits inthe region codes of its end-points

    If one of these is a 1 and the other is a 0 thenthe line crosses the boundary

  • 8/6/2019 20072CGraphics-Unit 8B 2D Clipping v2

    18/44

  • 8/6/2019 20072CGraphics-Unit 8B 2D Clipping v2

    19/44

    Sutherland Examples (cont)

    Consider the line P3 to P4 below

    Start at P4 From the region codes

    of the two end-pointswe know the linecrosses the left

    boundary so calculatethe intersection point togenerate P4

    The line P3 to P4 is completely outside the

    window so is clipped

    wymax

    wymin

    wxmin wxmax

    WindowP4 [1001]

    P3 [0001]

    P4 [1000]

    P3 [0001]

  • 8/6/2019 20072CGraphics-Unit 8B 2D Clipping v2

    20/44

  • 8/6/2019 20072CGraphics-Unit 8B 2D Clipping v2

    21/44

  • 8/6/2019 20072CGraphics-Unit 8B 2D Clipping v2

    22/44

    Sutherland: Line Clipping

    Compute the end point Outcodes of A and B;

    if (A == 0000 && B == 0000)

    the whole line is visible (accept it);

    Else if (A AND B != 0000) the whole line is outside of window (reject it);

    else repeat the following {

    compute intersection;

    clip outside part;

    test again;

    }

    22

  • 8/6/2019 20072CGraphics-Unit 8B 2D Clipping v2

    23/44

  • 8/6/2019 20072CGraphics-Unit 8B 2D Clipping v2

    24/44

  • 8/6/2019 20072CGraphics-Unit 8B 2D Clipping v2

    25/44

    Sutherland Example

    25

  • 8/6/2019 20072CGraphics-Unit 8B 2D Clipping v2

    26/44

    Sutherland Example

    26

  • 8/6/2019 20072CGraphics-Unit 8B 2D Clipping v2

    27/44

  • 8/6/2019 20072CGraphics-Unit 8B 2D Clipping v2

    28/44

    Calculating Line Intersections (cont)

    The x-coordinate of an intersection with ahorizontal window boundary can be calculatedusing:

    x = x1 + (yboundary - y1) /mwhereyboundary can be set to either wymin orwymax

    m is the slope of the line in question and canbe calculated as m = (y2 - y1) / (x2 - x1)

  • 8/6/2019 20072CGraphics-Unit 8B 2D Clipping v2

    29/44

  • 8/6/2019 20072CGraphics-Unit 8B 2D Clipping v2

    30/44

  • 8/6/2019 20072CGraphics-Unit 8B 2D Clipping v2

    31/44

  • 8/6/2019 20072CGraphics-Unit 8B 2D Clipping v2

    32/44

  • 8/6/2019 20072CGraphics-Unit 8B 2D Clipping v2

    33/44

  • 8/6/2019 20072CGraphics-Unit 8B 2D Clipping v2

    34/44

    34

    To clip the polygon, we need to keep the fill area as an

    entity as it is processed through the clipping stages as

    shown in the following Figure:

  • 8/6/2019 20072CGraphics-Unit 8B 2D Clipping v2

    35/44

  • 8/6/2019 20072CGraphics-Unit 8B 2D Clipping v2

    36/44

  • 8/6/2019 20072CGraphics-Unit 8B 2D Clipping v2

    37/44

  • 8/6/2019 20072CGraphics-Unit 8B 2D Clipping v2

    38/44

  • 8/6/2019 20072CGraphics-Unit 8B 2D Clipping v2

    39/44

  • 8/6/2019 20072CGraphics-Unit 8B 2D Clipping v2

    40/44

    Shutherland-Hodgman Polygon Clipping

    v1

    v2

    v3'

    v4

    v5

    v6

    Left border:v1 v2both inside v1 v2v2 v3both inside v2 v3..... ........

    v1,v2,v3,v4v5,v6,v1 Bottom Border:

    v1 v2both inside v1 v2v2 v3v2 i, v3 o v2 v3'v3 v4 both outside nonev4 v5 both outside none

    v5 v6v5 o, v6 i v5' v6v6 v1both inside v6 v1v1,v2,v3',v5',v6,v1

    v3

    v5'

  • 8/6/2019 20072CGraphics-Unit 8B 2D Clipping v2

    41/44

    Shutherland-Hodgman Polygon Clipping

    v1

    v2

    v3

    v4

    v5

    v6

    v1,v2,v3',v5',v6,v1

    Right border:v1 v2 v1 i, v2 o v1 v2'v2 v3 v2 o, v3'i v2'' v3'v3' v5 both inside v3' v5'v5' v6 both inside v5' v6v6 v1 both inside v6 v1

    v1,v2',v2'',v3',v5',v6,v1 Top Border:

    v1 v2' both outside nonev2' v2'' v2' o, v2'' i v2''' v2''v2'' v3' both inside v2'' v3'v3' v5' both inside v3' v5'

    v5' v6 both inside v5' v6v6 v1 v6 i, v1 o v6 v1'v2''',v2'',v3',v5',v6,v1'

    v3'

    v5'

    v2'

    v2''

    v2'''

    v1'

  • 8/6/2019 20072CGraphics-Unit 8B 2D Clipping v2

    42/44

  • 8/6/2019 20072CGraphics-Unit 8B 2D Clipping v2

    43/44

  • 8/6/2019 20072CGraphics-Unit 8B 2D Clipping v2

    44/44