2 A Line Clipping

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 31

Eafit

COMPUTER GRAPHICS – ST0275


AGENDA
 Line Clipping in 2D
 Cohen-Sutherland algorithm
 Liang-Barsky algorithm
LINE CLIPPING
P2
 The idea is to
determine whether
P1
all, part or no part
of a line segment P3
lies inside a
P5 P4
rectangle:
 Line P1P2 is
completely outside
 Line P3P4 is P6
completely inside
 Line P5P6 is partly
inside
LINE CLIPPING – COHEN-SUTHERLAND

Video: https://www.youtube.com/watch?v=NtwZXGprxag
LINE CLIPPING – COHEN-SUTHERLAND
 Cohen and
Sutherland (1968)
developed a method 1001 1000 1010

based on coding the


regions of a plane as 0001 0000 0010
follows: Clipping
Area
 Bit 1: left (least- 0101 0100 0110
significant)
 Bit 2: right
 Bit 3: below
 Bit 4: above
CONT…
LINE CLIPPING – COHEN-SUTHERLAND
 Trivially accept: both ends are code of
0000 for both end points.
 Trivially reject:
 (code(Point1) & code(Point2)) != 0.
 (note the SINGLE ´&´).
 Otherwise:
 Start from an outside end point and check
what part of the line segment is inside the
clipping window (if any)
LINE CLIPPING – COHEN-SUTHERLAND
P2
 For example:
 Segment P1P2
becomes P1’P2 after P2’
intersection with the P2’’
bottom side.
 Segment P1’P2
P1’
becomes P1’P2’ after
intersection with the
left side. P1
 Segment P1’P2’
becomes P1’P2’’ after
intersection with the
top side.
 Continue until a trivial
LINE CLIPPING – COHEN-SUTHERLAND
 Note that the intersection with a side of the
window takes place only if the corresponding
bit, in the outside point, is 1.
 Intersections with a vertical boundary:
y=y1+m(x-x1)
(x is either xmin or xmax)
 Intersections with a horizontal boundary:
x=x1+(y-y1)/m
(y is either ymin or ymax)
EXAMPLE
LINE CLIPPING – COHEN SUTHERLAND
 See complete implementation in the text book
(section 6.7):
 Hearn, D., Baker, P. “Computer Graphics with
OpenGL”. Prentice Hall. 2004.
 Also in Wikipedia:
http://en.wikipedia.org/wiki/Cohen%E2%80%93Suthe
rland_algorithm
 (includes C/C++ implementation)
 Original paper: A clipping divider. R. Sproull and I.
Sutherland. Proceedings of the Fall Joint Computer
Conference, 1968.
 Download from: http://dl.acm.org/citation.cfm?
id=1476687
LINE CLIPPING – LIANG-BARSKY (1984)
 Based on the parametric equation of a line
segment:
x = x1 + tDx
y = y1 + tDy
(0 <= t <= 1)

 Where Dx = x2 – x1 and Dy = y2 – y1
 The parametric equation of the line segment:
 Defines a starting and ending point
 Defines a direction
 Can be easily extended to 3D
 Is better than the standard line equation for many
computer graphics applications
LINE CLIPPING – LIANG-BARSKY (1984)
 The point clipping conditions in the parametric form
are:
xwmin <= x1 + tDx <= xwmax
ywmin <= y1 + tDy <= ywmax
(0<=t<=1)

 Each of these four inequalities can be expressed as


(explanation
tpk <= qk, k = 1, 2, 3, 4
 Where p and q are defined as (explanation in next slide):
p1 = -Dx, q1 = x1 – xwmin (left)
p2 = Dx, q2 = xwmax - x1 (right)
CONT…
CONT…
LINE CLIPPING – LIANG-BARSKY
 Horizontal or vertical lines:
 Any line that is parallel to a boundary has
pk = 0 for the value of k corresponding to
the boundary.
 If, for that value of k, we also find that qk
< 0, then the line is completely outside
and can be eliminated. If qk >= 0, the line
is inside the boundary and parallel to it.
LINE CLIPPING – LIANG-BARSKY
 Lines entering or leaving the boundary:
 When pk<0, the infinite extension of the
line proceeds from the outside to the inside
(entering) of the infinite extension of this
particular clipping boundary
 When pk>0, the line proceeds from the
inside to the outside (leaving)
 The value of u for the intersection with
boundary k is
u=qk/pk
CONT…

Both changes
EXAMPLE

T1 changes, so search it, but


T2 equals to 1, T2 inside so
no search for it
LINE CLIPPING – LIANG-BARSKY
 For each line, u1 and u2 define the part
of the line that lies within the clip
rectangle.
 u1 correspond to edges for which the line
proceeds from the outside to the inside.
u1 is the largest of 0 and the “entering” u
values.
 u2 corresponds to edges for which the line
proceeds from the inside to the outside.
u2 is the minimum of 1 and the “leaving” u
values.
LINE CLIPPING – LIANG-BARSKY
 If u1 > u2, the line lies completely
outside of the clipping area.
 Otherwise the segment from u1 to u2
lies inside the clipping window.
 See implementation on the text book
(sect. 6.7)
LINE CLIPPING
 Reading: Hearn and Baker, sect. 6.7.
(Cohen-Sutherland and Liang-Barsky
only).
 Java implementation in Wikipedia:
 http://en.wikipedia.org/wiki/Liang-Barsk
y
 Artículo: Liang, Y.D., and Barsky, B., "A
New Concept and Method for Line
Clipping", ACM Transactions on
Graphics, 3(1):1-22, January 1984.

You might also like