0% found this document useful (0 votes)
5K views

Midpoint Subdivision Line Clipping Algorithm

The midpoint subdivision line clipping algorithm assigns 4-bit region codes to the endpoints of a line to categorize it as: 1. Totally visible if the logical OR of the codes is 0. 2. Totally invisible if the logical AND is not 0. 3. A clipping candidate otherwise. It then recursively subdivides the line at the midpoint, reassigns codes, and classifies the sub-lines until reaching totally visible or invisible segments. This allows finding the visible portion of a line intersecting a rectangular clipping window.

Uploaded by

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

Midpoint Subdivision Line Clipping Algorithm

The midpoint subdivision line clipping algorithm assigns 4-bit region codes to the endpoints of a line to categorize it as: 1. Totally visible if the logical OR of the codes is 0. 2. Totally invisible if the logical AND is not 0. 3. A clipping candidate otherwise. It then recursively subdivides the line at the midpoint, reassigns codes, and classifies the sub-lines until reaching totally visible or invisible segments. This allows finding the visible portion of a line intersecting a rectangular clipping window.

Uploaded by

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

Midpoint Subdivision Line Clipping Algorithm

The Algorithm employs an efficient procedure for finding the category of lines. It assigns a 4-bit
region code to each end-points of the line. The code is determined according to which of the
following nine regions of the plane the end-point lies in

Starting from the most significant bit, each bit of the code is set to true(1) or false (0) according to
the following scheme.
Bit 1≡ end-point is above the window = sign(y-ymax)
Bit 2≡ end-point is below the window = sign(ymin-y)
Bit 3≡ end-point is right of the window = sign(x-xmax)
Bit 4≡ end-point is left of the window = sign(xmin-x)
We use the convention that sign(a)= 1, if a is positive
0, otherwise

That means region code 0000 means that the point is inside the window.

There are three possible cases for any given line.


1. Category 1: Totally visible: Bitwise OR of region of two end-points of line is 0 (Both
points are inside the rectangle)
2. Category 2:Totally invisible: Both endpoints share at least one outside region which
implies that the line does not cross the visible region. Bitwise OR of region of two end-
points of line is not 0.
3. Category 3:Clipping Candidate: If the line is in neither Category 1 nor Category 2.

Algorithm:

Step 1: Input the bottom-left and top-right corners of the rectangular window.
Step 2: Input the two end-points of the line
Step 3: Find the region codes of the two end-points of the line with respect to the window.
Step 4: Perform bitwise logical OR operation on the region codes of the two end-points.
Step 4.1: If the Result = 0000, then the line is completely visible (Trivially Accept)
Step 4.2: Else, Perform bitwise logical AND operation on the region codes of the two
end-points
Step 4.2.1: If the Result <> 0000, then the line is completely invisible (Trivially
Reject)
Step 4.2.2: Else the line is clipping candidate
Step 5: Divide the line segment in equal parts and repeat Step 3 through Step 5 for both
subdivided line segments until we get completely visible or completely invisible line
segment.
Step 6: Stop
Example: Using Midpoint Subdivision Line clipping Algorithm find the visible portion of the line
P1P2, where P1(120,5), P2(180,30) with respect to a clipping window ABCD where A
(100, 10), B (160,10), C(160,40), D (100,40).

Ans: The endpoints of the given line P1P2 be P1(120,5) and P2(180,30)
Thus the region codes of the given endpoints are Code_P1 = 0100, Code_P2 = 0010

Code_P1 OR Code_P2 = 0100 OR 0010 <> 0000, thus the line P1P2 is not totally visible.
Code_P1 AND Code_P2 = 0100 AND 0010 = 0000, thus the line P1P2 is a clipping candidate.

Now we find out the midpoint Pm of the line P1P2 ,


Pm = ( P1 + P2 )/2 = ((120+180)/2 , ( 5+30)/2) = (150,18)
Code_Pm = 0000

Fig 1: Window with the given line. We save PmP2 for later
consideration, and continue with P1Pm.

P1 Code_P1 P2 Code_P2 Pm Code_Pm Comment

Save PmP2, continue with P1Pm


(120,5) 0100 (180,30) 0010 (150,18) 0000
Continue with P1Pm, as PmP2 is
(120,5) 0100 (150,18) 0000 (135,12) 0000
totally visible
Continue with PmP2, as P1Pm is
(120,5) 0100 (135,12) 0000 (128,9) 0100
totally invisible
Succeeds. This is the intersection
(128,9) 0100 (135,12) 0000 (132,10) 0000 point of line with the Bottom
window edge.

The following figures viz Fig 2, Fig 3 and Fig 4 describe all the steps of the above table.

Fig 2: As Code_P2 = Code_Pm, we discard PmP2, and


consider P1Pm for next iteration and P2 is replaced by Pm
[ We discard PmP2 as Code_Pm AND Code_P2 <> 0000, hence completely
invisible, and consider P1Pm as Code_P1 AND Code_Pm = 0000, hence a
clipping candidate. ]

Fig 3: As Code_P1 = Code_Pm, we discard P1Pm, and


consider PmP2 for next iteration and P1 is replaced by Pm
[ We discard P1Pm as Code_P1 AND Code_Pm <> 0000, hence completely
invisible, and consider PmP2 as Code_Pm AND Code_P2 = 0000, hence a
clipping candidate. ]

Fig 4: The midpoint of the line segment P1P2 lies on the window
boundary, hence we get one intersection point

Thus one of the intersection point has been found at (132,10).

For the Second intersection point, we consider another half of the given line.:

Fig 5
P1 Code_P1 P2 Code_P2 Pm Code_Pm Comment

Continue with Save PmP2


(120,5) 0100 (180,30) 0010 (150,18) 0000
Continue with P1Pm, as PmP2 is
(150,18) 0000 (180,30) 0010 (165,24) 0010
totally invisible
Continue with PmP2, as P1Pm is
(150,18) 0000 (165,24) 0010 (158,21) 0000
totally visible.
Continue with P1Pm, as PmP2 is
(158,21) 0000 (165,24) 0010 (162,23) 0010
totally invisible
Succeeds. This is the
(158,21) 0000 (162,23) 0010 (160,22) 0000 intersection point of line with
the Right window edge.

Thus the second intersection point has been found at (160,22).


Hence the visible portion of the given line is AB, where A=(132,10) and B=(160,22).

You might also like