 # CGPage: 1 東吳資訊科學 江清水 The process of clipping decides which part, if any, of a primitive lies inside the window. The algorithms used for line clipping can.

## Presentation on theme: "CGPage: 1 東吳資訊科學 江清水 The process of clipping decides which part, if any, of a primitive lies inside the window. The algorithms used for line clipping can."— Presentation transcript:

CGPage: 1 東吳資訊科學 江清水 The process of clipping decides which part, if any, of a primitive lies inside the window. The algorithms used for line clipping can be divided into two parts: (1) Check all the line segments and separate those that intersect the window boundary. (The curve is represented by a sequence of line segment) (2) Clip the line segments obtained from step 1 by calculating their intersections with the window boundaries. Clipping 2D Clipping

CGPage: 2 東吳資訊科學 江清水 (1) How can we sure that a line segment is totally inside the window (such as A) ? Both endpoints of the segment fall within the boundaries. (2) How can we sure that a line segment is totally outside the window (such as E,F,G)? Let two endpoints are (x 1,y 1 ),(x 2,y 2 ) If ( max{x 1,x 2 } x max or max{y 1,y 2 } y max ) then the line segment is totally outside the window. We introduce an efficient procedure, the Cohen-Sutherland algorithm, to check the intersection property and further clip the line segment if necessary. X min X max Y min Y max A B C D E F G

CGPage: 3 東吳資訊科學 江清水 Cohen-Sutherland Algorithm The 2D plane is divided into 9 regions. Each region is designated by a 4-bit (base 2) integer code. Each bit of the code is set to 1 (true) or 0 (false), starting with the left most one. The bits are assigned by the follow rule: bit 1 = 1 if the region is above the window. bit 2 = 1 if the region is below the window. bit 3 = 1 if the region is on the right of the window. bit 4 = 1 if the region is on the left of the window. X min X max Y min Y max (1001) (0001)(0000)(0010) (0100)(0110) (1000)(1010) (0001)

CGPage: 4 東吳資訊科學 江清水 (1) Given a point (x,y), we would like to assign a "region code" to it by using Cohen-Suterlandalgorithm. In C this can be coded: code = 0 if (x < x min ) code = 1 if (x > x max ) code = 2 if (y < y min ) code += 4 if (y > y max ) code += 8 (2) According to the C code in (1), what code will be assigned if the point is on the boundary of the window? (0000)  0 (3) How about the point on the line X max and not on the boundary of the window? (0100) or (1000) depending on y

CGPage: 5 東吳資訊科學 江清水 (4) Draw precise picture for the partition of 2D plane according to the C code in (1). (Using solid line and dotted line) Let's consider the line clipping now. At first, the visibility of the line segment is determined as follows: (1) Visible -> Both endoint codes are (0000).

CGPage: 6 東吳資訊科學 江清水 (2) Invisible -> Bitwise logical AND of the endpoint codes is not (0000). (3) Indeterminate -> Bitwise logical AND of the endpoint codes is (0000), but at least one of the endpoint does not have a (0000) code. On the third situation, the line endpoints are "pushed" towards the boundary line until the first or the second situation occurs. (0001) (0010) (0000) Y max Y min X min X max

CGPage: 7 東吳資訊科學 江清水 The algorithm steps: 1. Calculate IC1 and IC2, the code of the two endpoints. 2. If (IC1 + IC2.eq. 0) then "Visible". 3. If ( (IC1.AND. IC2).NE. 0) then "Invisible". 4. If ( IC1.EQ. 0) then { Swap(IC1, IC2); Swap(X1,X2); Swap(Y1,Y2); } // At lease one point is outside the // viewport. Insure that point #1 is // one of the outside point. 5. Push endpoint #1 towards X min, X max, Y min, or Y max, depending on which region endpoint #1 occupies (via IC1). bit 1 = 1  toward Y max bit 2 = 1 toward Y min bit 3 = 1 toward X max bit 4 = 1 toward X min If endpoint #1 towards Y max, then { Y max = Y = X 1 + t(Y 2 - Y 1 ), find t and calculate X = X 1 + t(X 2 - X 1 ) } 6. Go To Step #1 above.

CGPage: 8 東吳資訊科學 江清水 Example: Let Xmin = 2, Xmax = 8, Ymin = 2 and Ymax = 8, check the visibility of the following segments using the Cohen-Sutherland Algorithm and, if necessary, clip them against the appropriate window boundaries. Line AB: A(3,10) B(6,12) Line CD: C(4,1) D(10,6) X min =2X max =8 Y min =2 Y max =8 A(3,10) B(6,12) C(4,1) D(10,6)

CGPage: 9 東吳資訊科學 江清水 Example: Let Xmin = 2, Xmax = 8, Ymin = 2 and Ymax = 8, check the visibility of the following segments using the Cohen-Sutherland Algorithm. Line AB: A(3,10) B(6,12) Line CD: C(4,1) D(10,6)

CGPage: 10 東吳資訊科學 江清水 How will the algorithm handle these examples? (A)(B) (C)(D)

Download ppt "CGPage: 1 東吳資訊科學 江清水 The process of clipping decides which part, if any, of a primitive lies inside the window. The algorithms used for line clipping can."

Similar presentations