CGPage: 1 東吳資訊科學 江清水 The process of clipping decides which part, if any, of a...

10
CG Page: 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

Transcript of CGPage: 1 東吳資訊科學 江清水 The process of clipping decides which part, if any, of a...

Page 1: 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.

CG Page: 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

Page 2: 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.

CG Page: 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 (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.

Xmin Xmax

Ymin

Ymax

A

BC

D

E F G

Page 3: 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.

CG Page: 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.

Xmin Xmax

Ymin

Ymax

(1001)

(0001) (0000) (0010)

(0100) (0110)

(1000) (1010)

(0001)

Page 4: 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.

CG Page: 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 < 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

Page 5: 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.

CG Page: 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).

Page 6: 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.

CG Page: 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)

Ymax

Ymin

Xmin Xmax

Page 7: 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.

CG Page: 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 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.

Page 8: 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.

CG Page: 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)

Xmin=2 Xmax=8

Ymin=2

Ymax =8A(3,10)

B(6,12)

C(4,1)

D(10,6)

Page 9: 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.

CG Page: 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)

Page 10: 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.

CG Page: 10 東吳資訊科學 江清水

How will the algorithm handle these examples?

(A) (B)

(C) (D)