Top Banner
1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai
113

1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

Dec 20, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

1

CSCE 441 Computer Graphics:Clipping Lines

Jinxiang Chai

Page 2: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

2/135

OpenGL Geometric Primitives

All geometric primitives are specified by vertices

GL_QUAD_STRIPGL_QUAD_STRIP

GL_POLYGONGL_POLYGON

GL_TRIANGLE_STRIPGL_TRIANGLE_STRIP

GL_TRIANGLE_FANGL_TRIANGLE_FAN

GL_POINTSGL_POINTSGL_LINESGL_LINES

GL_LINE_LOOPGL_LINE_LOOPGL_LINE_STRIPGL_LINE_STRIP

GL_TRIANGLESGL_TRIANGLES

GL_QUADSGL_QUADS

Page 3: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

3/135

Review: Drawing General Polygons

How to draw every interior pixels?

?

Scanline conversion of polygons!

Page 4: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

4/135

Review: Curved Boundaries

How to deal with curved boundaries?

Boundary fill algorithm!

Page 5: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

5/135

Review: How to deal with this?

Multiple color boundaries?

Flood fill algorithm!

Page 6: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

6/135

Why Clip?

We do not want to waste time drawing objects that are outside of viewing window (or clipping window)

Page 7: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

7/135

Clipping Points

Given a point (x, y) and clipping window (xmin, ymin), (xmax, ymax), determine if the point should be drawn

(xmin,ymin)

(xmax,ymax)

(x,y)

Page 8: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

8/135

Clipping Points

Given a point (x, y) and clipping window (xmin, ymin), (xmax, ymax), determine if the point should be drawn

(xmin,ymin)

(xmax,ymax)

(x,y)

xmin=<x<=xmax?ymin=<y<=ymax?

Page 9: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

9/135

Clipping Points

Given a point (x, y) and clipping window (xmin, ymin), (xmax, ymax), determine if the point should be drawn

(x1,y1)

(x2,y2)

(xmin,ymin)

(xmax,ymax)

xmin=<x<=xmax?ymin=<y<=ymax?

Page 10: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

10/135

Clipping Points

Given a point (x, y) and clipping window (xmin, ymin), (xmax, ymax), determine if the point should be drawn

(x1,y1)

(x2,y2)

(xmin,ymin)

(xmax,ymax)

xmin=<x1<=xmax Yes

ymin=<y1<=ymax Yes

Page 11: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

11/135

Clipping Points

Given a point (x, y) and clipping window (xmin, ymin), (xmax, ymax), determine if the point should be drawn

(x1,y1)

(x2,y2)

(xmin,ymin)

(xmax,ymax)

xmin=<x2<=xmax Noymin=<y2<=ymax No

Page 12: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

12/135

Clipping Lines

1P

2P

3P4P

5P

6P

7P

8P 9P

10P

Page 13: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

13/135

Clipping Lines

5P

6̂P

7P

8P

9̂P

10P̂

Page 14: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

14/135

Clipping Lines

Given a line with end-points (x0, y0), (x1, y1)

and clipping window (xmin, ymin), (xmax, ymax),

determine if line should be drawn and clipped end-points of line to draw.

(x0, y0)

(x1, y1)

(xmin,ymin)

(xmax,ymax)

Page 15: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

15/135

Clipping Lines

1P

2P

3P4P

5P

6P

7P

8P 9P

10P

Page 16: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

16/135

Outline

Simple line clipping algorithm Cohen-Sutherland Liang-Barsky

Page 17: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

17/135

Clipping Lines

1P

2P

3P4P

5P

6P

7P

8P 9P

10P

Page 18: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

18/135

Clipping Lines – Simple Algorithm

If both end-points inside rectangle, draw line If one end-point outside,

intersect line with all edges of rectangle

clip that point and repeat test

Page 19: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

19/135

Clipping Lines – Simple Algorithm

),( 00 yx

),( minmin yx

),( maxmax yx

),( 11 yx

Page 20: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

20/135

Clipping Lines – Simple Algorithm

),( 00 yx

),( minmin yx

),( maxmax yx

),( 11 yx

Page 21: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

21/135

Intersecting Two Lines

),( 00 yx

),( 22 yx

),( 11 yx

),( 33 yx

Page 22: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

22/135

Intersecting Two Lines

),( 00 yx

),( 22 yx

),( 11 yxtxxxtx )()( 010 ),( 33 yx

tyyyty )()( 010

10 t

Page 23: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

23/135

Intersecting Two Lines

),( 00 yx

),( 22 yx

),( 11 yxtxxxtx )()( 010 ),( 33 yx

tyyyty )()( 010

0)0( xx 0)0( yy

1)1( xx 1)1( yy

Page 24: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

24/135

Intersecting Two Lines

),( 00 yx

),( 22 yx

),( 11 yx

sxxxtxxx )()( 232010

),( 33 yx

syyytyyy )()( 232010

Page 25: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

25/135

Intersecting Two Lines

),( 00 yx

),( 22 yx

),( 11 yx

02

02

3201

3201

yy

xx

s

t

yyyy

xxxx

),( 33 yx

Page 26: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

26/135

Intersecting Two Lines

),( 00 yx

),( 22 yx

),( 11 yx

02

02

3201

3201

yy

xx

s

t

yyyy

xxxx

),( 33 yx

Substitute t or s back into equation to find intersection

Page 27: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

27/135

Clipping Lines – Simple Algorithm

),( 00 yx

),( minmin yx

),( maxmax yx

),( 11 yx

Page 28: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

28/135

Clipping Lines – Simple Algorithm

),( 00 yx

),( minmin yx

),( maxmax yx

),( 11 yx

Page 29: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

29/135

Clipping Lines – Simple Algorithm

),( 00 yx

),( minmin yx

),( maxmax yx

),( 11 yx

Page 30: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

30/135

Clipping Lines – Simple Algorithm

),( 00 yx

),( minmin yx

),( maxmax yx

),( 11 yx

Page 31: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

31/135

Clipping Lines – Simple Algorithm

)ˆ,ˆ( 00 yx

),( minmin yx

),( maxmax yx

),( 11 yx

Page 32: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

32/135

Clipping Lines – Simple Algorithm

)ˆ,ˆ( 00 yx

),( minmin yx

),( maxmax yx

),( 11 yx

Page 33: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

33/135

Clipping Lines – Simple Algorithm

)ˆ,ˆ( 00 yx

),( minmin yx

),( maxmax yx

),( 11 yx

Page 34: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

34/135

Clipping Lines – Simple Algorithm

)ˆ,ˆ( 00 yx

),( minmin yx

),( maxmax yx

),( 11 yx

Page 35: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

35/135

Clipping Lines – Simple Algorithm

),( 00 yx

),( minmin yx

),( maxmax yx

),( 11 yx

Page 36: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

36/135

Clipping Lines – Simple Algorithm

),( 00 yx

),( minmin yx

),( maxmax yx

),( 11 yx

Page 37: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

37/135

Clipping Lines – Simple Algorithm

)ˆ,ˆ( 00 yx

),( minmin yx

),( maxmax yx

),( 11 yx

Page 38: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

38/135

Clipping Lines – Simple Algorithm

)ˆ,ˆ( 00 yx

),( minmin yx

),( maxmax yx

),( 11 yx

Page 39: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

39/135

Clipping Lines – Simple Algorithm

)ˆ,ˆ( 00 yx

),( minmin yx

),( maxmax yx

),( 11 yx

Page 40: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

40/135

Clipping Lines – Simple Algorithm

)ˆ,ˆ( 00 yx

),( minmin yx

),( maxmax yx

)ˆ,ˆ( 11 yx

Page 41: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

41/135

Clipping Lines – Simple Algorithm

)ˆ,ˆ( 00 yx

),( minmin yx

),( maxmax yx

)ˆ,ˆ( 11 yx

Page 42: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

42/135

Clipping Lines – Simple Algorithm

Lots of intersection tests makes algorithm expensive

Complicated tests to determine if intersecting rectangle

Is there a better way?

Page 43: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

43/135

Trivial Accepts

Big Optimization: trivial accepts/rejects How can we quickly decide whether line

segment is entirely inside window Answer: test both endpoints

Page 44: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

44/135

Trivial Accepts

Big Optimization: trivial accepts/rejects How can we quickly decide whether line

segment is entirely inside window Answer: test both endpoints

Page 45: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

45/135

Trivial Rejects

How can we know a line is outside of the window

Answer:

Page 46: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

46/135

Trivial Rejects

How can we know a line is outside of the window

Answer:

Wrong side

Page 47: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

47/135

Trivial Rejects

How can we know a line is outside of the window

Answer: both endpoints on wrong side of same edge, can trivially reject the line

Page 48: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

48/135

Cohen-Sutherland Algorithm

Classify p0, p1 using region codes c0, c1

If , trivially reject If , trivially accept Otherwise reduce to trivial cases by splitting

into two segments

010 cc

010 cc

Page 49: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

49/135

Cohen-Sutherland Algorithm

Every end point is assigned to a four-digit binary value, i.e. Region code

Each bit position indicates whether the point is inside or outside of a specific window edges

bit 4 bit 3 bit 2 bit 1

leftrightbottomtop

Page 50: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

50/135

Cohen-Sutherland Algorithm

bit 4 bit 3 bit 2 bit 1

leftrightbottomtop

top

rightleft

bottom

Region code?

Page 51: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

51/135

Cohen-Sutherland Algorithm

bit 4 bit 3 bit 2 bit 1

leftrightbottomtop

top

rightleft

bottom

0010

?

Page 52: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

52/135

Cohen-Sutherland Algorithm

bit 4 bit 3 bit 2 bit 1

leftrightbottomtop

top

rightleft

bottom

0010

0100

Page 53: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

53/135

Cohen-Sutherland Algorithm

00000001

0101

1001 1000 1010

0010

01100100

Page 54: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

54/135

Cohen-Sutherland Algorithm

00000001

0101

1001 1000 1010

0010

01100100

),( 00 yx

),( 11 yx

Page 55: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

55/135

Cohen-Sutherland Algorithm

Classify p0, p1 using region codes c0, c1

If , trivially reject If , trivially accept Otherwise reduce to trivial cases by splitting

into two segments

010 cc

010 cc

Page 56: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

56/135

Cohen-Sutherland Algorithm

Classify p0, p1 using region codes c0, c1

If , trivially reject If , trivially accept Otherwise reduce to trivial cases by splitting

into two segments

010 cc

010 cc

Page 57: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

57/135

Cohen-Sutherland Algorithm

Classify p0, p1 using region codes c0, c1

If , trivially reject If , trivially accept Otherwise reduce to trivial cases by splitting

into two segments

010 cc

010 cc

00000001

0101

1001 1000 1010

0010

01100100

Line is outside the window! reject

Page 58: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

58/135

Cohen-Sutherland Algorithm

Classify p0, p1 using region codes c0, c1

If , trivially reject If , trivially accept Otherwise reduce to trivial cases by splitting

into two segments

010 cc

010 cc

00000001

0101

1001 1000 1010

0010

01100100

Line is outside the window! reject

Page 59: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

59/135

Cohen-Sutherland Algorithm

Classify p0, p1 using region codes c0, c1

If , trivially reject If , trivially accept Otherwise reduce to trivial cases by splitting

into two segments

010 cc

010 cc

00000001

0101

1001 1000 1010

0010

01100100

Line is inside the window! draw

Page 60: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

60/135

Cohen-Sutherland Algorithm

Classify p0, p1 using region codes c0, c1

If , trivially reject If , trivially accept Otherwise reduce to trivial cases by splitting

into two segments

010 cc

010 cc

00000001

0101

1001 1000 1010

0010

01100100

Page 61: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

61/135

Window Intersection

(x1, y1), (x2, y2) intersect with vertical edge at xright

yintersect = y1 + m(xright – x1)

• where m=(y2-y1)/(x2-x1)

(x1, y1), (x2, y2) intersect with horizontal edge at ybottom

xintersect = x1 + (ybottom – y1)/m

• where m=(y2-y1)/(x2-x1)

Page 62: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

62/135

Example 1

00000001

0101

1001 1000 1010

0010

01100100

Page 63: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

63/135

Example 1

00000001

0101

1001 1000 1010

0010

01100100

Page 64: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

64/135

Example 1

00000001

0101

1001 1000 1010

0010

01100100

Page 65: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

65/135

Example 1

00000001

0101

1001 1000 1010

0010

01100100

Page 66: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

66/135

Example 2

00000001

0101

1001 1000 1010

0010

01100100

Page 67: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

67/135

Example 2

00000001

0101

1001 1000 1010

0010

01100100

Page 68: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

68/135

Example 2

00000001

0101

1001 1000 1010

0010

01100100

Page 69: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

69/135

Example 2

00000001

0101

1001 1000 1010

0010

01100100

Page 70: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

70/135

Example 2

00000001

0101

1001 1000 1010

0010

01100100

Page 71: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

71/135

Example 2

00000001

0101

1001 1000 1010

0010

01100100

Page 72: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

72/135

Example 2

00000001

0101

1001 1000 1010

0010

01100100

Page 73: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

73/135

Example 3

00000001

0101

1001 1000 1010

0010

01100100

Page 74: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

74/135

Example 3

00000001

0101

1001 1000 1010

0010

01100100

Page 75: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

75/135

Example 3

00000001

0101

1001 1000 1010

0010

01100100

Page 76: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

76/135

Example 3

00000001

0101

1001 1000 1010

0010

01100100

Page 77: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

77/135

Cohen Sutherland

Java applet: click here

Page 78: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

78/135

Cohen-Sutherland Algorithm

Extends easily to 3D line clipping27 regions6 bits

Page 79: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

79/135

Cohen-Sutherland Algorithm

Use region codes to quickly eliminate/include lines

Best algorithm when trivial accepts/rejects are common

Must compute viewing window clipping of remaining lines

Non-trivial clipping cost More efficient algorithms exist

Page 80: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

80/135

Liang-Barsky Algorithm

Parametric definition of a line: x = x1 + uΔx

y = y1 + uΔy

Δx = (x2-x1), Δy = (y2-y1), 0<=u<=1

Lines are oriented: classify lines as moving inside to out or outside to in

Goal: find range of u for which x and y both inside the viewing window

Page 81: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

81/135

Liang-Barsky Algorithm

Current clipped line (u1=0,u2=1)

(-.5,-.5)

(2,1)

Page 82: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

82/135

Liang-Barsky Algorithm

Current clipped line (u1=0.333,u2=0.6)

(-.5,-.5)

(2,1)

Page 83: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

83/135

Liang-Barsky Algorithm

For lines starting outside of boundary, update its starting point (u1)

For lines starting inside of boundary, update end point (u2)

For lines paralleling the boundaries and outside window,

reject it.

Page 84: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

84/135

Liang-Barsky Algorithm

For lines starting outside of boundary, update its starting point (u1)

For lines starting inside of boundary, update end point (u2)

For lines paralleling the boundaries and outside window,

reject it.

Page 85: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

85/135

Liang-Barsky Algorithm

Mathematically: xmin <= x1 + uΔx <= xmax

ymin <= y1 + uΔy <= ymax

Rearranged 1: u*(-Δx) <= (x1 – xmin) 2: u*(Δx) <= (xmax – x1) 3: u*(-Δy) <= (y1 – ymin) 4: u*(Δy) <= (ymax – y1) gen: u*(pk) <= (qk), k=1,2,3,4

Page 86: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

86/135

Liang-Barsky Algorithm

Rules:

1) pk = 0: the line is parallel to boundaries If for that same k, qk < 0, it’s outside Otherwise it’s inside

2) pk < 0: the line starts outside this boundary rk = qk/pk

u1 = max(0, rk, u1) /**update starting point**/ pk > 0: the line starts inside the boundary

rk = qk/pk

u2 = min(1, rk, u2) /** update end point**/ If u1 > u2, the line is completely outside

Page 87: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

87/135

Liang-Barsky Algorithm

Current clipped line (u1=0,u2=1)

(-.5,-.5)

(2,1)

Page 88: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

88/135

Liang-Barsky Algorithm

Current clipped line (u1=0,u2=1)

(-.5,-.5)

(2,1)

Δx = (x2-x1)=2.5

Δy = (y2-y1)=1.5

p1 = -Δx =-2.5<0

q1 = (x1-xmin)=-.5

r1 = q1/p1=0.2

Page 89: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

89/135

Liang-Barsky Algorithm

Current clipped line (u1=0,u2=1)

(-.5,-.5)

(2,1)

Δx = (x2-x1)=2.5

Δy = (y2-y1)=1.5

p1 = -Δx =-2.5<0

q1 = (x1-xmin)=-.5

r1 = q1/p1=0.2line starts outside this boundary

Page 90: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

90/135

Liang-Barsky Algorithm

Current clipped line (u1=0,u2=1)

(-.5,-.5)

(2,1)

Δx = (x2-x1)=2.5

Δy = (y2-y1)=1.5

p1 = -Δx =-2.5<0

q1 = (x1-xmin)=-.5

r1 = q1/p1=0.2

Page 91: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

91/135

Liang-Barsky Algorithm

Current clipped line (u1=0,u2=1)

(-.5,-.5)

(2,1)

Δx = (x2-x1)=2.5

Δy = (y2-y1)=1.5

p1 = -Δx =-2.5<0

q1 = (x1-xmin)=-.5

r1 = q1/p1=0.2

Page 92: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

92/135

Liang-Barsky Algorithm

Current clipped line (u1=0,u2=1)

(-.5,-.5)

(2,1)

Δx = (x2-x1)=2.5

Δy = (y2-y1)=1.5

p1 = -Δx =-2.5<0

q1 = (x1-xmin)=-.5

r1 = q1/p1=0.2u1 = max(0, r1, u1)

Page 93: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

93/135

Liang-Barsky Algorithm

Current clipped line (u1=0.2,u2=1)

(-.5,-.5)

(2,1)

u1<u2?

- yes: continue

- no: the line is completely outside window

Page 94: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

94/135

Liang-Barsky Algorithm

Current clipped line (u1=0.2,u2=1)

(-.5,-.5)

(2,1)

Page 95: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

95/135

Liang-Barsky Algorithm

Current clipped line (u1=0.2,u2=1)

(-.5,-.5)

(2,1)

Δx = (x2-x1)=2.5

Δy = (y2-y1)=1.5

p2 = Δx =2.5>0

q2 = (xmax-x1)=1.5

r2 = q2/p2=0.6

Page 96: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

96/135

Liang-Barsky Algorithm

Current clipped line (u1=0.2,u2=1)

(-.5,-.5)

(2,1)

Δx = (x2-x1)=2.5

Δy = (y2-y1)=1.5

p2 = Δx =2.5>0

q2 = (xmax-x1)=1.5

r2 = q2/p2=0.6u2 = min(1, r2, u2)

Page 97: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

97/135

Liang-Barsky Algorithm

Current clipped line (u1=0.2,u2=0.6)

(-.5,-.5)

(2,1)

Δx = (x2-x1)=2.5

Δy = (y2-y1)=1.5

p2 = Δx =2.5>0

q2 = (xmax-x1)=1.5

r2 = q2/p2=0.6

Page 98: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

98/135

Liang-Barsky Algorithm

Current clipped line (u1=0.2,u2=0.6)

(-.5,-.5)

(2,1)

u1<u2?

- yes: continue

- no: the line is completely outside window

Page 99: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

99/135

Liang-Barsky Algorithm

Current clipped line (u1=0.2,u2=0.6)

(-.5,-.5)

(2,1)

Δx = (x2-x1)=2.5

Δy = (y2-y1)=1.5

p3 = -Δy =-1.5<0

q3 = (y1-ymin)=0.5

r3 = q3/p3=0.333

Page 100: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

100/135

Liang-Barsky Algorithm

Current clipped line (u1=0.2,u2=0.6)

(-.5,-.5)

(2,1)

Δx = (x2-x1)=2.5

Δy = (y2-y1)=1.5

p3 = -Δy =-1.5<0

q3 = (y1-ymin)=0.5

r3 = q3/p3=0.333u1 = max(0, r3, u1)

Page 101: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

101/135

Liang-Barsky Algorithm

Current clipped line (u1=0.333,u2=0.6)

(-.5,-.5)

(2,1)

Δx = (x2-x1)=2.5

Δy = (y2-y1)=1.5

p3 = -Δy =-1.5<0

q3 = (y1-ymin)=0.5

r3 = q3/p3=0.333

Page 102: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

102/135

Liang-Barsky Algorithm

Current clipped line (u1=0.333,u2=0.6)

(-.5,-.5)

(2,1)

Page 103: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

103/135

Liang-Barsky Algorithm

Current clipped line (u1=0.333,u2=0.6)

(-.5,-.5)

(2,1)

Δx = (x2-x1)=2.5

Δy = (y2-y1)=1.5

p3 = -Δy =-1.5<0

q3 = (y1-ymin)=0.5

r3 = q3/p3=0.333

Page 104: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

104/135

Liang-Barsky Algorithm

Current clipped line (u1=0.333,u2=0.6)

(-.5,-.5)

(2,1)

Δx = (x2-x1)=2.5

Δy = (y2-y1)=1.5

p4 = Δy =1.5>0

q4 = (ymax-y1)=1.5

r4 = q4/p4=1

Page 105: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

105/135

Liang-Barsky Algorithm

Current clipped line (u1=0.333,u2=0.6)

(-.5,-.5)

(2,1)

Δx = (x2-x1)=2.5

Δy = (y2-y1)=1.5

p4 = Δy =1.5>0

q4 = (ymax-y1)=1.5

r4 = q4/p4=1u2 = min(1, r4, u2)

Page 106: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

106/135

Liang-Barsky Algorithm

Check the left edge (u1=0.333,u2=0.6)

(-.5,-.5)

(2,1)

u1<u2

xnew1=x1+Δx*u1

ynew1=y1+Δy*u1

xnew2=x2+Δx*u2

ynew2=y2+Δy*u2

Page 107: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

107/135

Liang-Barsky Algorithm

Faster than Cohen-Sutherland

Extension to 3D is easy

- Parametric representation for 3D lines

- Compute u1,u2 based on the intersection between line and plane

Page 108: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

108/135

Comparison

Cohen-Sutherland Repeated clipping is expensive Best used when trivial acceptance and rejection is

possible for most lines Liang-Barsky

Computation of t-intersections is cheap (only one division)

Computation of (x,y) clip points is only done once

Algorithm doesn’t consider trivial accepts/rejects Best when many lines must be clipped

Page 109: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

109/135

Curve Clipping

Page 110: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

110/135

Curve Clipping

Page 111: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

111/135

Curve Clipping

Approximate a curve using a set of straight-line segments

Apply line clipping for curve clipping

Page 112: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

112/135

Next Lecture

Polygon fill-area clipping

Page 113: 1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai.

113/135

Next Lecture

Polygon fill-area clipping