19
Introduction to Computer Graphics Chapter 6 – 2D Viewing Pt 2 1

Introduction to Computer Graphics Chapter 6 – 2D Viewing Pt 2 1

Embed Size (px)

Citation preview

Page 1: Introduction to Computer Graphics Chapter 6 – 2D Viewing Pt 2 1

Introduction to Computer GraphicsChapter 6 – 2D Viewing Pt 2

1

Page 2: Introduction to Computer Graphics Chapter 6 – 2D Viewing Pt 2 1

2

CLIPPING OPERATIONS

Procedures that identifies those portions Procedures that identifies those portions of a picture that are either inside or of a picture that are either inside or outside a specified region of a space is outside a specified region of a space is referred to as referred to as clippingclipping algorithmalgorithm or or clippingclipping

The region against which an object is toThe region against which an object is toclipped is called clipped is called clip window.clip window.

before after

Page 3: Introduction to Computer Graphics Chapter 6 – 2D Viewing Pt 2 1

3

Clipping Operation

• Note: different graphic elements may require different clipping techniques. Character, need to include all or completely omit depending on whether or not its center lies within the window.

Page 4: Introduction to Computer Graphics Chapter 6 – 2D Viewing Pt 2 1

4

Clipping

• Application of clipping include:• Extracting part of defined scene for viewing.• Identifying visible surfaces in three-dimensional

views.• Anti-aliasing line segments or object boundaries.• Creating objects using solid-modeling procedures.• Displaying a multiwindow environment.• Drawing and painting operations that allow parts of

picture to be selected for copying, moving, erasing, or duplication.

Page 5: Introduction to Computer Graphics Chapter 6 – 2D Viewing Pt 2 1

5

Clipping• Clipping algorithm can be applied in both world

coordinate and normalized device coordinates

• In world coordinate:• only the contents of the window interior are mapped

to device coordinates• world coordinate clipping removes those primitives

outside the window from further consideration. Eliminating the unnecessary processing to transform

• In normalized device coordinate:• mapping is fully done from world to normalized

device.• clipping will reduce calculations by allowing

concatenation of viewing and geometric transformation matrices.

Page 6: Introduction to Computer Graphics Chapter 6 – 2D Viewing Pt 2 1

6

Clipping Algorithms

• Algorithms have been developed for clipping for the following primitives types:

1. Point Clipping.2. Line Clipping (straight-line segments)3. Area Clipping (polygons)4. Curve Clipping5. Text Clipping.

Page 7: Introduction to Computer Graphics Chapter 6 – 2D Viewing Pt 2 1

7

Point Clipping

• Assuming clip window is standard rectangle position, point P-(x,y) will be save for display if its satisfied:

xwmin <= x <= xwmax

ywmin <= y <= ywmax

• where the edges of the clip window (xwmin, xwmax, ywmin, ywmax) can be either the world coordinate window boundaries or viewport boundaries.

Page 8: Introduction to Computer Graphics Chapter 6 – 2D Viewing Pt 2 1

8

Line Clipping

• Cohen-Sutherland Outcode Algorithm

• This algorithm quickly removes lines which entirely to one side of the clipping where both endpoints above, below, right or left.

• The advantage of the algorithm is, it uses bit operations for testing.

Page 9: Introduction to Computer Graphics Chapter 6 – 2D Viewing Pt 2 1

9

Cohen Sutherland

0000

1000 1010

0010

011001000101

0001

1001

A

B

CD

E

G

F

I K

L

M

J

S

T

H

Page 10: Introduction to Computer Graphics Chapter 6 – 2D Viewing Pt 2 1

10

Cohen Sutherland• Bit convention 1

• First bit y > ymax• Second bit y < ymin• Third bit x > xmax• Fourth bit x < xmin

• Bit convention 2 (1 through 4 from right to left)

• bit 1 : left• bit 2 : right• bit 3 : below• bit 4 : above

Page 11: Introduction to Computer Graphics Chapter 6 – 2D Viewing Pt 2 1

11

Cohen Sutherland• Segment endpoints are each given 4-bit binary

codes.• The high order bit is set to 1 if the point above window• The next bit set to 1 if point is below window• The third and fourth indicate right or left of window.

• Testing result:• 0000 if both endpoints are inside window.• if the line segment lies entirely on one side of the window, then

both endpoints will have a 1 in the outcode bit position for that side.

• How?:• By taking AND operation. If NONZERO then line

segment may be rejected. Ex: AB and CD

Page 12: Introduction to Computer Graphics Chapter 6 – 2D Viewing Pt 2 1

12

Cohen Sutherland (Problem?)

• Certain lines cannot be identified as completely inside or outside a clip window by doing AND test. Lines which completely outside the clip window can also produce AND 0000. So, process of discarding is required and intersection with boundary must be calculated (divide and conquer).

• Difficulty is when a line crossed one or more of the lines which contain the clipping boundary. Ex: EF and DJ

Page 13: Introduction to Computer Graphics Chapter 6 – 2D Viewing Pt 2 1

13

Cohen Sutherland (Solution)

• The point of intersection between the line segment and clipping lines may be used to break up the line segment.

• Intersection points with a clipping boundary can be calculated using the slope-intercept form of the line equation.

Page 14: Introduction to Computer Graphics Chapter 6 – 2D Viewing Pt 2 1

14

Cohen Sutherland (Solution)• For a line with endpoint (x1,y1) to (x2,y2)

• Y coordinate of the intersection point with vertical boundary is obtained with

y = y1 + m(x-x1)

• where x value is set either xwmin or xwmax and the slope of the line is m = (y2 – y1) / (x2 – x1)

• X coordinate of the intersection point with horizontal boundary is calculated by

x = x1 + (y-y1)/m

• where y is set either to ywmin or to ywmax

Page 15: Introduction to Computer Graphics Chapter 6 – 2D Viewing Pt 2 1

15

Cohen Sutherland (Pseudocode)• Brief outline of the algorithm.

• First compute the outcodes for the two endpoints (p1 and p2).

• Enter the loop, check whether both outcodes are zero

• Both zero, then enter segment in display list.• If not both zero, perform the AND function. If nonzero,

reject the line. If zero, subdivide the line segment. Repeat the loop.

Page 16: Introduction to Computer Graphics Chapter 6 – 2D Viewing Pt 2 1

16

Liang Barsky Line Clipping• Parametric equation of a line segment (x1,y1) and

(x2,y2)• x = x1 + u∆x• y = y1 + u∆y

• Where 0 <= u <=1• ∆x = x2 – x1• ∆y = y2 –y1

• From point clipping, we have• xwmin < = x <= xwmax• Ywmin <= y <= ywmax

• For line clipping, we derived• xwmin < = x1 + u∆x <= xwmax• ywmin <= y1 + u∆y <= ywmax

Page 17: Introduction to Computer Graphics Chapter 6 – 2D Viewing Pt 2 1

17

Liang-Barsky Line Clipping• Based on these four equations, the following rules

are introducedupk <= qk

• where k = 1 (left boundary) 2 (right boundary)

3 (bottom boundary) 4 (top boundary)

parameters p and q are defined as:p1 = - ∆x, q1 = x1 - xwmin

p2 = ∆x, q2 = xwmax – x1

p3= - ∆y, q3 = y1 - ywmin

p4= ∆y, q4 = ywmax – y1

Page 18: Introduction to Computer Graphics Chapter 6 – 2D Viewing Pt 2 1

18

Liang-Barsky Line Clipping

• If any line that is parallel to one of the clipping boundaries, pk = 0. If qk < 0 then line is completely outside the boundary.

• When pk < 0, the infinite extension of the line proceeds from the outside to the inside.

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

• For nonzero value of pk , we can calculate the value of u corresponds to the point where infinitely extended line intersects the extension boundary k as

u = qk/ pk • u1(outside to inside) where its value ranges from 0 to r

• u2 (inside to outside) where its value ranges from 1 to r.• If u1 > u2, the line is completely outside the clip window and can be

rejected

Page 19: Introduction to Computer Graphics Chapter 6 – 2D Viewing Pt 2 1

19

L.B vs C.S

• Liang-Barsky algorithm is more efficient than Cohen since intersection calculations are reduced.

• Liang-Barsky require only one division and window intersections of the line are computed only once.

• Cohen required repeated calculation even though the line may be completely outside the clip window.