140 likes | 442 Views
2D Clipping. 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
E N D
2D Clipping 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
E G F Ymax A D Ymin B C Xmin Xmax (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 (x1,y1),(x2,y2) If ( max{x1,x2} < xmin or min{x1,x2} > xmax or max{y1,y2} < ymin or min{y1,y2} > ymax) 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.
Ymax Ymin Xmin Xmax 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. (1001) (1000) (1010) (0001) (0000) (0010) (0001) (0100) (0110)
(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 < xmin) code = 1 if (x > xmax) code = 2 if (y < ymin) code += 4 if (y > ymax) 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 Xmax and not on the boundary of the window? (0100) or (1000) depending on y
(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).
(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. Ymax (0001) (0010) (0000) Ymin Xmin Xmax
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 Xmin, Xmax, Ymin, or Ymax, depending on which region endpoint #1 occupies (via IC1). bit 1 = 1 toward Ymax bit 2 = 1 toward Ymin bit 3 = 1 toward Xmax bit 4 = 1 toward Xmin If endpoint #1 towards Ymax , then { Ymax = Y = X1 + t(Y2 - Y1), find t and calculate X = X1 + t(X2 - X1) } 6. Go To Step #1 above.
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) B(6,12) A(3,10) Ymax =8 D(10,6) Ymin=2 C(4,1) Xmin=2 Xmax=8
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)
How will the algorithm handle these examples? (A) (B) (C) (D)