Midpoint Subdivision Line Clipping Algorithm
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.
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.
Fig 1: Window with the given line. We save PmP2 for later
consideration, and continue with P1Pm.
The following figures viz Fig 2, Fig 3 and Fig 4 describe all the steps of the above table.
Fig 4: The midpoint of the line segment P1P2 lies on the window
boundary, hence we get one intersection point
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