Вы находитесь на странице: 1из 9

CG (Basics of line

clipping)
Dr. Tathagata Ray
Assistant 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
Point Clipping

The condition for a point (x,y) is not clipped:


wxmin ≤ x ≤ wxmax AND wymin ≤ y ≤ wymax
If any of the four inequalities fail the point is outside clip
rectangle.
P3 Clipped
Clipped

Window P2
wymax
P4
P1
Points Within the Window
are Not Clipped

wymin

wxmin wxmax 3
BITS Pilani, Hyderabad Campus
Line Clipping
• Only consider the end-points of each line to see if they are
in the window or not
Situation Solution Example

Both end-points inside


Don’t clip
the window

One end-point inside


the window, one Must clip
outside

Both end-points outside


Don’t know!
the window
4
BITS Pilani, Hyderabad Campus
Brute Force Line Clipping

• Brute force line clipping can be


performed as follows:
– Don’t clip lines with both
end-points within the
window
– For lines with one end-
point inside the window
and one end-point
outside, calculate the
intersection point (using the
equation of the line) and clip
from this point out.
5
BITS Pilani, Hyderabad Campus
Brute Force Line Clipping

– For lines with both end-


points outside the window
test the line for intersection
with all of the window
boundaries, and clip
appropriately

• However, calculating line intersections is computationally


expensive as you will soon see.
• Because a scene can contain so many lines, the brute
force approach to clipping is much too slow.

6
BITS Pilani, Hyderabad Campus
How to test whether two lines
segments intersect
Approach 1:
– Take the two infinite lines that contain the original line
segments say <edge, line> pair.

– Compute the intersection point


• Approach a: Compute the slope-intercept form for each line
and solve for the intersection point. Be careful with vertical
lines!

• Approach b: Use parametric equation of line


x = x0 + t(x1-x0) and y = y0 + t(y1 – y0).

– Check if intersection point lies inside both the line


segments
• In approach b the value of tedge and tline will be calculated
and checked if their value is within [0,1]. If yes then there is a
valid intersection.
7
BITS Pilani, Hyderabad Campus
How to test whether two lines
segments intersect
Approach 2: Let ab and cd be the two line segments. If abd
and abc are oriented in same manner then there is no
possible intersection of line segments. If abc and abd are
oriented differently then check orientation of cda and cdb.
Different orientation would mean lines intersect.
c c
b b

b
c
a d a d

d
a
8
BITS Pilani, Hyderabad Campus
Signed Area Test

• Signed area of a triangle: Twice the area of a triangle


T = (a, b, c) is given by
ax ay 1
2 A(T )  bx by 1
cx cy 1

• If the points a, b, and c in this order make a


counterclockwise rotation then the signed area is positive.
• The advantage of using this test is that division operation
is completely avoided.
• Disadvantage: It can only test if there is an intersection.
On success, the intersection points are to be computed
separately.
9
BITS Pilani, Hyderabad Campus

Вам также может понравиться