You are on page 1of 13

CG (Line clipping: Cyrus

Beck Line clipping)


Prof. Tathagata Ray
Associate Professor, BITS Pilani, Hyderabad Campus
BITS Pilani
Hyderabad Campus rayt@hyderabad.bits-pilani.ac.in
Clipping
• For the example window below consider which lines and points should
be kept and which ones should be clipped.
• Lines intersecting a rectangular clip regions are always clipped to a
single line segment.
• Lines lying in the clip rectangle’s border are considered inside and
hence are displayed.
P4

Window
wymax
P3 P
P1
P5 6
P7

P9
P8
wymin
P10
P2

wxmin wxmax
BITS Pilani, Hyderabad Campus
Cyrus-Beck Algorithm

• Cohen Sutherland worked for axis aligned rectangular


window.
• What if the clip window is not axis aligned?
• We wish to optimize line/line intersection
– Start with parametric equation of line:
• P(t) = P0 + (P1 - P0) t
– And a point and normal for each edge
• PL, NL

3
BITS Pilani, Hyderabad Campus
Cyrus-Beck Algorithm

Find t such that 𝑁𝐿 ∙ 𝑃 𝑡 − 𝑃𝐿 = 0 NL


P1
PL

P(t)
Outside Inside
P0
Substitute line equation for P(t).
𝑁𝐿 ∙ 𝑃0 + 𝑃1 − 𝑃0 𝑡 − 𝑃𝐿 = 0
Solve for t
𝑁𝐿 ∙[𝑃0 −𝑃𝐿 ] 𝑁𝐿 ∙[𝑃0 −𝑃𝐿 ]
• 𝑡= =
−𝑁𝐿 ∙[𝑃1 −𝑃0 ] −𝑁𝐿 ∙𝐷
4
BITS Pilani, Hyderabad Campus
Cyrus-Beck Algorithm

• Compute t for line intersection with all


four edges.
• Discard all (t < 0) and (t > 1)
• Classify each remaining intersection
as PL
– Potentially Entering (PE) P1
– Potentially Leaving (PL)
P(t)
• 𝑁𝐿 ∙ 𝑃1 − 𝑃0 > 0 implies PL Inside
P0
• 𝑁𝐿 ∙ 𝑃1 − 𝑃0 < 0 implies PE
– Note that we computed this term
when computing t.
5
BITS Pilani, Hyderabad Campus
Cyrus-Beck Algorithm
• Compute PE with largest t.
• Compute PL with smallest t.
• Clip to these two points.

PL P1
PL

PE
PE

P0
6
• Edges are
processed in the
order L, R, B, T.
• Normals are
inwards.
• 𝑤𝑖 = 𝑃0 − 𝑓𝑖 .
• 𝑓1 = 𝑓3 =
𝐿𝑜𝑤𝑒𝑟 𝑙𝑒𝑓𝑡 𝑐𝑜𝑟𝑛𝑒𝑟

• 𝑓2 = 𝑓4 =
𝑈𝑝𝑝𝑒𝑟 𝑟𝑖𝑔ℎ𝑡 𝑐𝑜𝑟𝑛𝑒𝑟

7
Cyrus-Beck Algorithm

Clip edge Normal PL P0 - PL t = NL.(P0-PL)/-Ni.D


NL

Left:x = xmin (-1,0) (xmin,y) (x0-xmin,y0-y) -(x0 - xmin) / (x1 - x0)

Right:x = xmax (1,0) (xmax,y) (x0-xmax,y0-y) -(x0 - xmax) / (x1 - x0)

Bottom:y = (0,-1) (x,ymin) (x0-x,y0-ymin) -(y0 - ymin) / (y1 - y0)


ymin

Top:y = ymax (0,1) (x,ymax) (x0-x,y0-ymax) -(y0 - ymax) / (y1 - y0)

8
BITS Pilani, Hyderabad Campus
Comparison

• Cohen-Sutherland
– Repeated clipping is expensive
– Best used when trivial acceptance and rejection is
possible for most lines
• Cyrus-Beck
– Computation of t-intersections is cheap
– Computation of (x,y) clip points is only done once
– Best when many lines must be clipped
• Liang-Barsky: Optimized Cyrus-Beck

9
BITS Pilani, Hyderabad Campus
Liang Barsky Clipping
Algorithm
• Optimized version of Cyrus-Beck, catered for rectangular
axis-aligned window.
• P(t) = P1 + (P2 – P1) t
• All those points which are inside window satisfy 𝑥𝑚𝑖𝑛 ≤
𝑥1 + ∆𝑥 𝑡 ≤ 𝑥𝑚𝑎𝑥 and 𝑦𝑚𝑖𝑛 ≤ 𝑦1 + ∆𝑦 𝑡 ≤ 𝑦𝑚𝑎𝑥 .
𝑥𝑚𝑖𝑛 ≤ 𝑥1 + ∆𝑥 𝑡 ֜ 𝑥1 − 𝑥𝑚𝑖𝑛 ≥ −∆𝑥 𝑡 ֜ 𝑞1 ≥ 𝑝1 𝑡.
𝑥𝑚𝑎𝑥 ≥ 𝑥1 + ∆𝑥 𝑡 ֜ 𝑥𝑚𝑎𝑥 − 𝑥1 ≥ ∆𝑥 𝑡 ֜ 𝑞2 ≥ 𝑝2 𝑡.

10
BITS Pilani, Hyderabad Campus
Liang Barsky Clipping
k qk pk pk =0 qk<0
1(Left) 𝑥1 − 𝑥𝑚𝑖𝑛 −∆𝑥 || to y-axis Left of window
2(right) 𝑥𝑚𝑎𝑥 − 𝑥1 ∆𝑥 || to y-axis Right of
window
3(bottom) 𝑦1 − 𝑦𝑚𝑖𝑛 −∆𝑦 || to x-axis Bottom of
window
4(top) 𝑦𝑚𝑎𝑥 − 𝑦1 ∆𝑦 || to x-axis Top of window

11
BITS Pilani, Hyderabad Campus
Liang Barsky Clipping
k qk pk Normal(N) N.(P2-P1) PE(PL)
1(Left) 𝑥1 − 𝑥𝑚𝑖𝑛 −∆𝑥 (-1.i + 0.j) −∆𝑥 = 𝑝1 −∆𝑥 = 𝑝1 < 0(> 0)
2(right) 𝑥𝑚𝑎𝑥 − 𝑥1 ∆𝑥 (1.i + 0.j) ∆𝑥 = 𝑝2 ∆𝑥 = 𝑝2 < 0(> 0)
3(bottom) 𝑦1 − 𝑦𝑚𝑖𝑛 −∆𝑦 (0.i - 1.j) −∆y = 𝑝3 −∆y = 𝑝3 < 0(> 0)
4(top) 𝑦𝑚𝑎𝑥 − 𝑦1 ∆𝑦 (0.i + 1.j) ∆y = 𝑝4 ∆y = 𝑝4 < 0(> 0)

12
BITS Pilani, Hyderabad Campus
Liang-Barsky Line clipping
algorithm
• If pk = 0 and qk < 0 for any k, eliminate the line and stop.
Otherwise proceed to the next step.
• For all k such that pk < 0, calculate rk = qk/pk. Let u1 be
the maximum of the set containing 0 and the calculated r
values.
• For all k such that pk > 0, calculate rk = qk/pk. Let u2 be
the minimum of the set containing 1 and the calculated r
values.
• If u1 > u2, eliminate the line since it is completely outside
the clipping window. Otherwise use u1 and u2 to
calculate the endpoints of the clipped line.

13
BITS Pilani, Hyderabad Campus

You might also like