0% found this document useful (0 votes)
24 views

Lecture 11

Uploaded by

sstories569
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Lecture 11

Uploaded by

sstories569
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Lecture 11

Line clipping Algorithm


Lecture Outlines
• Line Clipping Algorithms -
✓ Cohen-Sutherland Algorithm
✓ Midpoint Subdivision Algorithm
Line Clipping
• Line clipping procedure -
• Test a given line segment to determine whether it lies completely inside the
clipping window.
• If it doesn’t, we try to determine whether it lies completely outside the
window.
• If we can’t identify a line as completely inside or completely outside, we must
perform intersection calculations with one or more clipping boundaries.
Continue…
• Checking the line endpoints ⇒ inside-outside test.

• Line clipping Algorithm:


• Cohen-Sutherland Algorithm;
• Midpoint Subdivision Algorithm;
• Liang-Barsky Algorithm.
Cohen-Sutherland Algorithm
• Divide the line clipping process into two phases:
1) Identify those lines which intersect the clipping window and so need to be clipped;
2) Perform the clipping.

• All lines fall into one of the following clipping categories:


1) Visible: Both end points of the line lie within the window.
2) Not visible: The line definitely lies outside the window. This will occur if the line
from (x1,y1) to (x2,y2) satisfies any one of the following inequalities:

3) Clipping candidate: the line is in neither category 1 nor 2.


Continue…

is in category 1(Visible)
is in category 2(Not Visible)
is in category 3(Clipping candidate)
Continue…

ymax y < ymax y > ymin

interior = ∩
ymin

xmin xmax x > xmin x < xmax


Continue…
• Assign a four-bit pattern (Region Code) to each endpoint of the given segment. The code is
determined according to which of the following nine regions of the plane the endpoint lies in.

1001 1000 1010


ymax
0001 0000 0010
ymin
0101 0100 0110
xmin xmax bit 1 : bit 2 : bit 3 : bit 4
Top : Bottom : Right : Left:
• Of course, a point with code 0000 is inside the window.
Continue…

1001 1000 1010


ymax

0001 0000 0010

ymin
0101 0100 0110

xmin xmax
Continue…
• If both endpoint codes are 0000, the line segment is visible (inside).
• The logical AND of the two endpoint codes -
• not completely 0000 , the line segment is not visible (outside).
• completely 0000, the line segment maybe inside (and outside).

• Lines that cannot be identified as being completely inside or


completely outside, a clipping window are then checked for
intersection with the window border lines.
D
1000B
Continue… 1010 ymax

0001 A B’
D’
A ’
C’ 0010
ymin
0100 C
xmin xmax
Continue…
• Replace endpoint (x1,y1) with the intersection point(xi,yi), effectively
eliminating the portion of the original line that is on the outside of the
selected window boundary.
• The new endpoint is then assigned an updated region code and the clipped
line re-categoriged and handled in the same way.
• This iterative process terminates when we finally reach a clipped line that
belongs to either category 1(visible) or category 2(not visible).
Continue…
• Use simple tests to classify easy cases first:
P7

P1
P4 P8
P3
P2
P6
P10

P5 P9
14
Continue…
• Classify some lines quickly by AND of bit-codes representing regions of two
endpoints (must be 0).
1001 P7 0001 0101
Bit 4
P1
P4 P8
1000 P3 0000 0100
P2
P6
P10
Bit 3
1010 P5 0010 P9 0110
Bit 1 Bit 2
15
Continue…

1001 P7 0001 0101


Bit 4
P1
P4 P8
1000 P3 0000 0100
P2
P6
P10
Bit 3
1010 P5 0010 P9 0110
Bit 1 Bit 2
16
Continue…

1001 P7 0001 0101


Bit 4

P4 P8
1000 P3 0000 0100

P6
P10
Bit 3
1010 P5 0010 P9 0110
Bit 1 Bit 2
17
Continue…
• Compute intersections with window boundary for lines that can’t be classified
quickly:
1001 P7 0001 0101
Bit 4

P4 P8
1000 P3 0000 0100

P6
P10
Bit 3
1010 P5 0010 P9 0110
Bit 1 Bit 2
18
Continue…

1001 P7 0001 0101


Bit 4

P4 P8
1000 P3 0000 0100

P6
P10
Bit 3
1010 P5 0010 P9 0110
Bit 1 Bit 2
19
Continue…

1001 P7 0001 0101


Bit 4

P4 P8
1000 P3 0000 0100

P6
P10
Bit 3
1010 P’5 0010 0110
P9
Bit 1 Bit 2
20
Continue…

1001 P7 0001 0101


Bit 4

P4 P8
1000 P3 0000 0100

P6
P10
Bit 3
1010 P’5 0010 0110
P9
Bit 1 Bit 2
21
Continue…

1001 P7 0001 0101


Bit 4

P4 P8
1000 P3 0000 0100

P6
P10
Bit 3
1010 P’5 0010 0110
P9
Bit 1 Bit 2
cgvr.korea.ac.kr 22
Continue…

1001 P7 0001 0101


Bit 4

P4 P8
1000 P3 0000 0100

P6
P10
Bit 3
1010 P’5 0010 0110
P9
Bit 1 Bit 2
23
Continue…

1001 00P0’17 0101


Bit 4

P4 P8
1000 P3 0000 0100

P6
P10
Bit 3
1010 P’5 0010 0110
P9
Bit 1 Bit 2
24
Continue…

1001 00P0’17 0101


Bit 4

P4 P8
1000 P3 0000 0100

P6
P10
Bit 3
1010 P’5 0010 0110
P9
Bit 1 Bit 2
25
Continue…

1001 00P0’1 0101


Bit 4
7

P4 P’8
1000 P3 0000 0100

P6
P10
Bit 3
1010 P’5 0010 P 0110
9
Bit 1 Bit 2
26
Continue…

1001 00P0’1 0101


Bit 4
7

P4 P’8
1000 P3 0000 0100

P6
P10
Bit 3
1010 P’5 0010 P 0110
9
Bit 1 Bit 2
27
Continue…

1001 00P0’1 0101


Bit 4
7

P4 P’8
1000 P3 0000 0100

P6
P10
Bit 3
1010 P’5 0010 P 0110
9
Bit 1 Bit 2
28
Continue…

1001 00P0’17 0101


Bit 4

P4 P’8
1000 P3 0000 0100

P6
P10
Bit 3
1010 P’5 0010 0110
P9
Bit 1 Bit 2
29
Continue…

1001 00P0’1 0101


Bit 4
7

P4 P’8
1000 P3 0000 0100

P6
P10
Bit 3
1010 P’5 0010 P’90110
Bit 1 Bit 2
30
Continue…

1001 00P0’1 0101


Bit 4
7

P4 P’8
1000 P3 0000 0100

P6
P10
Bit 3
1010 P’5 0010 P’90110
Bit 1 Bit 2
31
Continue…

1001 00P0’1 0101


Bit 4
7

P4 P’8
1000 P3 0000 0100

P6
Bit 3
1010 P’5 0010 0110
Bit 1 Bit 2
32
Midpoint Subdivision
• An alternative way to process a line in category 3 is based on binary search.
• The line is divided at its midpoint into two shorter line segments.
• The clipping categories of the two new line segments are determined by their region
codes.
• Each segment in category 3 is divided again into shorter segments and categorized.
• This bisection & categorization process continues until each line segment that spans
across a window boundary reaches a threshold for line size and all other segments are
either in category 1 or in category 2.

33
Midpoint Subdivision Algorithm
• Step-1: Calculate the position of both endpoints of the line.
• Step-2: Perform OR operation on both of these endpoints.
• Step-3: If the OR operation gives 0000:
then-
Line is guaranteed to be visible;
else-
Perform AND operation on both endpoints.
If AND ≠ 0000-
the line is invisible;
else
the line is clipped case;
• Step-4: For the line to be clipped. Find midpoint.
Xm=(x1+x2)/2
Ym=(y1+y2)/2
• Step-5: Check each midpoint, whether it nearest to the boundary of a window or not.
• Step-6: If the line is totally visible or totally rejected not found:
Repeat step 1 to 5.
• Step-7: Stop algorithm.
34
Example - 01

35
Continue…

36
Thank you

You might also like