Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Graphics : Viewing In 2D

Similar presentations


Presentation on theme: "Computer Graphics : Viewing In 2D"— Presentation transcript:

1 Computer Graphics : Viewing In 2D
UNIT-3

2 Contents Windowing Concepts Clipping Area Clipping Introduction
Brute Force Cohen-Sutherland Clipping Algorithm Area Clipping Sutherland-Hodgman Area Clipping Algorithm

3 Windowing I A scene is made up of a collection of objects specified in world coordinates World Coordinates

4 Windowing II When we display a scene only those objects within a particular window are displayed Window wymax wymin wxmin wxmax World Coordinates

5 Windowing III Because drawing things to a display takes time we clip everything outside the window Window wymax wymin wxmin wxmax World Coordinates

6 Clipping For the image below consider which lines and points should be kept and which ones should be clipped P4 Window P2 wymax P6 P3 P1 P7 P5 P9 P8 wymin P10 wxmin wxmax

7 Points Within the Window are Not Clipped
Point Clipping Easy - a point (x,y) is not clipped if: wxmin ≤ x ≤ wxmax AND wymin ≤ y ≤ wymax otherwise it is clipped P4 Clipped Clipped Window P2 wymax Clipped P5 P1 P7 Points Within the Window are Not Clipped P9 P8 wymin Clipped P10 wxmin wxmax

8 Line Clipping Harder - examine the end-points of each line to see if they are in the window or not Situation Solution Example Both end-points inside the window Don’t clip One end-point inside the window, one outside Must clip Both end-points outside the window Don’t know!

9 Cohen-Sutherland Algorithm
Idea: eliminate as many cases as possible without computing intersections Start with four lines that determine the sides of the clipping window y = ymax x = xmin x = xmax y = ymin

10 The Cases Case 1: both endpoints of line segment inside all four lines
Draw (accept) line segment as is Case 2: both endpoints outside all lines and on same side of a line Discard (reject) the line segment y = ymax x = xmin x = xmax y = ymin

11 The Cases Case 3: One endpoint inside, one outside
Must do at least one intersection Case 4: Both outside May have part inside y = ymax x = xmin x = xmax

12 Defining Outcodes For each endpoint, define an outcode
Outcodes divide space into 9 regions Computation of outcode requires at most 4 subtractions b0b1b2b3 b0 = 1 if y > ymax, 0 otherwise b1 = 1 if y < ymin, 0 otherwise b2 = 1 if x > xmax, 0 otherwise b3 = 1 if x < xmin, 0 otherwise

13 Using Outcodes Consider the 5 cases below
AB: outcode(A) = outcode(B) = 0 Accept line segment

14 Using Outcodes CD: outcode (C) = 0, outcode(D)  0
Compute intersection Location of 1 in outcode(D) determines which edge to intersect with Note if there were a segment from A to a point in a region with 2 ones in outcode, we might have to do two interesections

15 Using Outcodes 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 reject

16 Using Outcodes 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 (new endpoint of shortened line segment) Reexecute algorithm

17 Efficiency In many applications, the clipping window is small relative to the size of the entire data base Most line segments are outside one or more side of the window and can be eliminated based on their outcodes Inefficiency when code has to be reexecuted for line segments that must be shortened in more than one step

18 Liang – Barsky clipping
In computer graphics, the Liang–Barsky algorithm (named after You-Dong Liang and Brian A. Barsky) is a line clipping algorithm. The Liang–Barsky algorithm uses the parametric equation of a line and inequalities describing the range of the clipping window to determine the intersections between the line and the clipping window. With these intersections it knows which portion of the line should be drawn. This algorithm is significantly more efficient than Cohen– Sutherland.

19 The idea of the Liang-Barsky clipping algorithm is to do as much testing as possible before computing line intersections. Consider first the usual parametric form of a straight line: A point is in the clip window, if

20 To compute the final line segment:
A line parallel to a clipping window edge has   for that boundary. If for that  the line is completely outside and can be eliminated. When   the line proceeds outside to inside the clip window and when , the line proceeds inside to outside. For nonzero  ,  gives the intersection point.

21 Here

22 Liang-Barsky Leave Enter Leave Leave Leave Enter Enter Enter

23 Liang-Barsky - Algorithm
Compute entering u values, which are qk/pk for each pk<0 Compute leaving u values, which are qk/pk for each pk>0 Parameter value for small u end of line is:usmall= max(0, entering u’s) parameter value for large t end of line is: ularge=min(1, leaving u’s) If usmall<ularge, there is a line segment - compute endpoints by substituting t values Improvement (and actual Liang-Barsky): compute u’s for each edge in turn (some rejects occur earlier like this.)

24 Area Clipping Similarly to lines, areas must be clipped to a window boundary Consideration must be taken as to which portions of the area must be clipped

25 Sutherland-Hodgman Area Clipping Algorithm
Sutherland turns up again. This time with Gary Hodgman with whom he worked at the first ever graphics company Evans & Sutherland A technique for clipping areas developed by Sutherland & Hodgman Put simply the polygon is clipped by comparing it against each boundary in turn Original Area Clip Left Clip Right Clip Top Clip Bottom

26

27 The Sutherland-Hodgeman Polygon-Clipping Algorithms clips a given polygon successively against the edges of the given clip- rectangle. These clip edges are denoted with e1, e2, e3, and e4, here. The closed polygon is represented by a list of its vertices (v1 to vn; Since we got 15 vertices in the example shown above, vn = v15.). Clipping is computed successively for each edge. The output list of the previous clipping run is used as the input list for the next clipping run.

28

29

30

31

32

33

34 Sutherland-Hodgman Area Clipping Algorithm (cont…)
To clip an area against an individual boundary: Consider each vertex in turn against the boundary Vertices inside the boundary are saved for clipping against the next boundary Vertices outside the boundary are clipped If we proceed from a point inside the boundary to one outside, the intersection of the line with the boundary is saved If we cross from the outside to the inside intersection point and the vertex are saved

35 Sutherland-Hodgman Example
Each example shows the point being processed (P) and the previous point (S) Saved points define area clipped to the boundary in question S P Save Point P S P Save Point I I P S No Points Saved S P Save Points I & P I

36 Other Area Clipping Concerns
Clipping concave areas can be a little more tricky as often superfluous lines must be removed Clipping curves requires more work For circles we must find the two intersection points on the window boundary Window Window


Download ppt "Computer Graphics : Viewing In 2D"

Similar presentations


Ads by Google