Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Graphics Lecture 14 CLIPPING I Taqdees A. Siddiqi

Similar presentations


Presentation on theme: "Computer Graphics Lecture 14 CLIPPING I Taqdees A. Siddiqi"— Presentation transcript:

1 Computer Graphics Lecture 14 CLIPPING I Taqdees A. Siddiqi cs602@vu
Computer Graphics Lecture 14 CLIPPING I Taqdees A. Siddiqi

2 CLIPPING I Taqdees A. Siddiqi cs602@vu.edu.pk

3 Concept Restricting the effect of graphics primitives to a sub-region of the canvas Protecting other portions of the canvas

4 Primitives lying outside the clip rectangle are not drawn.
All primitives are clipped to the boundaries of this clipping rectangle; Primitives lying outside the clip rectangle are not drawn.

5 The default clipping rectangle is the full canvas (the screen); We cannot see any graphics primitives outside the screen

6 Example This is a simple example of line clipping:
the display window is the canvas and also the default clipping rectangle, thus all line segments inside the canvas are drawn.

7

8

9 Point Clipping xmin <= x <= xmax ymin <= y <= ymax

10 Line Clipping Clipping of lines against rectangles

11 Clipping Individual Points
Simpler problem Xmin < X < Xmax And Ymin < Y < Ymax

12 Trivial Accept – save a line with both endpoints inside all clipping boundaries

13 Trivial Reject – discard a line with both endpoints outside the clipping boundaries

14 For all other lines – compute intersections of line with clipping boundaries

15 Parametric Representation Line
x = x1 + u (x2 - x1) y = y1 + u (y2 - y1) and 0 <= u <= 1

16 If 0 <= u <= 1 then the line does enter the interior of the window at that boundary
otherwise, the line does not enter the interior of the window at that boundary.

17 Solve Simultaneous Equations
Need to consider only endpoints of a line: If both endpoints lie inside the clip rectangle; it can be trivially accepted.

18 If one endpoint lies inside and one outside, the line intersects the clip rectangle and we must compute the intersection point.

19 If both endpoints are outside the clip rectangle, we need to perform further calculations to determine whether there are any intersections.

20 The Brute Force Approach
Intersect that line with each of the four clip rectangle edges. if so, the line cuts the clip rectangle and is partially inside.

21 For each line and clip-rectangle edge, we therefore take the two mathematically infinite lines that contain them and intersect them.

22 Test whether this intersection point is "interior" -- that is, whether it lies within both the clip rectangle edge and the line.

23 If so, there is an intersection with the clip rectangle.

24 Line Clipping Algorithm
Cohen Sutherland Line Clipping Algorithm More efficient; Performs initial tests on a line to determine whether intersection calculations can be avoided.

25 End-point pairs are checked using the out-code.
If not divided into two segments at a clip edge, iteratively clipped by testing trivial-acceptance or trivial-rejection.

26 Trivial Accept / Reject Test

27 Each bit in the out-code is set to either
1 (true) or 0 (false); The 4 bits in the code correspond to the following conditions:

28 Bit 1: outside half-plane of top edge, above top edge Y > Ymax
Bit 2: outside half-plane of bottom edge, below bottom edge Y < Ymin

29 Bit 3: outside half-plane of right edge, to the right of right edge X > Xmax
Bit 4: outside half-plane of left edge, to the left of left edge X < Xmin

30 Conclusion Algorithm is efficient when out-code testing can be done cheaply

31 Liang-Barsky Algorithm
Faster line clippers based on analysis of the parametric equation of a line segment:

32 x = x1 + u x y = y1 + u y, where 0 <= u <= 1, x = x2 - x1 and y = y2 - y1

33 Following the Liang-Barsky approach, we first write the point-clipping in a parameteric way:
xmin <= x1 + u x <= xmax ymin <= y1 + u y <= ymax

34 These four inequalities can be expressed as
u * pk <= qk, for k = 1, 2, 3, 4

35 Where parameters p and q are defined as:
  p1 = -x, q1 = x1 - xmin p2 = -x, q2 = xmax - x1 p3 = -y, q3 = y1 - ymin p4 = -y, q4 = ymax - y1

36 Any line that is parallel to one of the clipping boundaries having pk = 0 If, for that value of k, we also find qk >= 0, the line is inside the parallel clipping boundary.

37 When pk < 0, the infinite extension of the line proceeds from outside to the inside of the infinite extension of the particular clipping boundary.

38 If pk > 0, the line proceeds from the inside to the outside.

39 For a nonzero value of pk, we can calculate the value of u that corresponds to the point where the infinitely extended line intersects the extension of boundary k as: u = qk / pk

40 For each line, we can calculate values for parameters u1 and u2 which define that part of the line which lies within the clip rectangle.

41 The value of u1 is determined by looking at the rectangle edges for which the line proceeds from the outer-side to the inner-side (p < 0). For these edges we calculate rk = qk / pk.

42 The value of u1 is taken as the largest of the set consisting of 0 and the various values of r.

43 The value of u2 is determined by examining the boundaries for which the line proceeds from inside to outside (p > 0).

44 Algorithm Line intersection parameters are initialized to the values:
u1 = 0 and u2 = 1.

45 For each clipping boundary, the appropriate values for p and q are calculated and used to determine whether the line can be rejected or the intersection parameters are to be adjusted.

46 A value of rk is calculated for each of these boundaries and the value of u2 is the minimum of the set consisting of 1 and the calculated r values.

47 If u1 > u2, the line is completely outside the clip window and it can be rejected.

48 Otherwise, the end points of the clipped line are calculated from the two values of parameter u.

49 When p < 0, the parameter r is used to update u1;

50 If updating u1 or u2 results in u1 > u2, we reject the line.

51 Otherwise, we update the appropriate u parameter only if the new value results in a shortening of the line.

52 When p = 0 and q < 0, we can discard the line since it is parallel to and outside of this boundary.

53 If the line has not been rejected after all four values of p and q have been tested, the endpoints of the clipped line are determined from values of u1 and u2.

54 More efficient; intersection calculations are reduced.
Conclusion More efficient; intersection calculations are reduced.

55 Each update of parameters u1 and u2 requires only one division; and window intersections of the line are computed only once, when the final values of u1 and u2 are computed.

56 Cohen-Sutherland algorithm can repeatedly calculate intersections along a line path, even though the line may be completely outside the clip window, and, each intersection calculation requires both a division and a multiplication.

57 Computer Graphics Lecture 14


Download ppt "Computer Graphics Lecture 14 CLIPPING I Taqdees A. Siddiqi"

Similar presentations


Ads by Google