Given a set of lines and a rectangular area of interest, the task is to remove lines which are outside the area of interest and clip the lines which are partially inside the area.
Input : Rectangular area of interest (Defined by below four values which are coordinates of bottom left and top right) x_min = 4, y_min = 4, x_max = 10, y_max = 8 A set of lines (Defined by two corner coordinates) line 1 : x1 = 5, y1 = 5, x2 = 7, y2 = 7 Line 2 : x1 = 7, y1 = 9, x2 = 11, y2 = 4 Line 2 : x1 = 1, y1 = 5, x2 = 4, y2 = 1 Output : Line 1 : Accepted from (5, 5) to (7, 7) Line 2 : Accepted from (7.8, 8) to (10, 5.25) Line 3 : Rejected
Cohen-Sutherland algorithm divides a two-dimensional space into 9 regions and then efficiently determines the lines and portions of lines that are inside the given rectangular area.
The algorithm can be outlines as follows:-
Nine regions are created, eight "outside" regions and one "inside" region. For a given line extreme point (x, y), we can quickly find its region's four bit code. Four bit code can be computed by comparing x and y with four values (x_min, x_max, y_min and y_max). If x is less than x_min then bit number 1 is set. If x is greater than x_max then bit number 2 is set. If y is less than y_min then bit number 3 is set. If y is greater than y_max then bit number 4 is set
There are three possible cases for any given line.
- Completely inside the given rectangle : Bitwise OR of region of two end points of line is 0 (Both points are inside the rectangle)
- Completely outside the given rectangle : Both endpoints share at least one outside region which implies that the line does not cross the visible region. (bitwise AND of endpoints != 0).
- Partially inside the window : Both endpoints are in different regions. In this case, the algorithm finds one of the two points that is outside the rectangular region. The intersection of the line from outside point and rectangular window becomes new corner point and the algorithm repeats
Step 1 : Assign a region code for two endpoints of given line. Step 2 : If both endpoints have a region code 0000 then given line is completely inside. Step 3 : Else, perform the logical AND operation for both region codes. Step 3.1 : If the result is not 0000, then given line is completely outside. Step 3.2 : Else line is partially inside. Step 3.2.1 : Choose an endpoint of the line that is outside the given rectangle. Step 3.2.2 : Find the intersection point of the rectangular boundary (based on region code). Step 3.2.3 : Replace endpoint with the intersection point and update the region code. Step 3.2.4 : Repeat step 2 until we find a clipped line either trivially accepted or trivially rejected. Step 4 : Repeat step 1 for other lines
Below is implementation of above steps.
Line accepted from 5.00,5.00 to 7.00,7.00 Line accepted from 7.80,8.00 to 10.00,5.25 Line rejected
The Cohen–Sutherland algorithm can be used only on a rectangular clip window. For other convex polygon clipping windows, Cyrus–Beck algorithm is used. We will be discussing Cyrus–Beck Algorithm in next set.
This article is contributed by Saket Modi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Line Clipping | Set 2 (Cyrus Beck Algorithm)
- Polygon Clipping | Sutherland–Hodgman Algorithm
- Point Clipping Algorithm in Computer Graphics
- Bresenham's Algorithm for 3-D Line Drawing
- Mid-Point Line Generation Algorithm
- Bresenham’s Line Generation Algorithm
- Comparions between DDA and Bresenham Line Drawing algorithm
- DDA Line generation Algorithm in Computer Graphics
- Anti-aliased Line | Xiaolin Wu's algorithm
- Klee's Algorithm (Length Of Union Of Segments of a line)
- Slope of the line parallel to the line with the given slope
- Find the other end point of a line with given one end and mid
- Reflection of a point about a line in C++
- Slope of perpendicular to line
- Draw a line in C++ graphics