INTRODUCTION TO COMPUTER GRAPHICS LESSON 7
LINE AND POINT CLIPPING
7.1 ••What is clipping?
A procedure that identifies the portions of a picture that are either lying inside or outside of a
specified region of space is referred to as clipping and can be done by using the clipping
algorithms.
• The region against which an object is to be clipped is called a clip window, as shown in Fig 1.
Fig 1. Clipping window
• Applications of clipping include extracting part of a defined scene for viewing, identifying
visible surface in three-dimensional views, object boundary, displaying a multi-window
environment, and many more. The clip window can be a general polygon or any curved
boundary.
• For viewing transformation, we want to display only those picture parts that are within the
window area.
• Everything outside the window is discarded.
• Clipping algorithm can be applied in world coordinates, so that only the contents of the
window interior are mapped to the device coordinates.
• Clipping can be done on a point, lines, polygons, and text.
• In Fig 2, lines and points within the window should be kept (line (P5-P6) and point P8), and the
line lying outside the window should be clipped (line (P1 to P2), line (P3 to P4), and point P7).
Fig 2. Example of clipping
7.2 Point Clipping
Point clipping is a process to define the position of the point.
Point clip
Consider the clipping window is a rectangle in the standard position, a point P (x, y) is used for
display with the following constraints
xwmin ≤ x ≤ xwmax AND ywmin ≤ y ≤ ywmax
where the edges of the clip window are xwmin, xwmax, ywmin, and ywmax, then the point is
clipped.
• In Fig 3, points B and C are within the window range (xwmin, xwmax, ywmin, ywmax), so they
are not clipped, while points A, D, and E lie outside the window, so they are clipped.
Fig 3. Example of Point Clipping
Examples
• In Fig 4, line P1 to P2 lies completely inside the window, so it does not require clipping and is
saved.
• Line P3 to P4 lies completely outside the window, so it does not require clipping and is not
saved.
• Line P5 to P6 lies inside and outside partially, so it requires clipping and is clipped as shown in
After Clipping of Fig 4.
7.3 LINE CLIPPING
• Removing the portion of the lines that lie outside the edges is called line clipping.
Description:-
In this algorithm, we are given 9 regions on the screen. Out of which one region is the
window, and the remaining 8 regions are around it, given by 4 digit binary. The division of the
regions is based on (x_max, y_max) and (x_min, y_min).
The central part is the viewing region or window; all the lines that lie within this region are
completely visible. A region code is always assigned to the endpoints of the given line.
To check whether the line is visible or not.
Region Codes
Formula to check binary digits:- TBRL, which can be defined as top, bottom, right, and left
accordingly.
TBRL Rule
7.4 Procedure for Line Clipping.
1. Test a given line segment to determine whether it lies completely inside the clipping window,
2. If it does not, we try to determine whether it lies completely outside the window. 3. If the
line segment is not completely inside or completely outside, perform intersection calculations
with one or more clipping boundaries, with the use of various line clipping algorithms.
Fig 4. Example of Line Clipping
Examples
• In Fig 4, line P1 to P2 lies completely inside the window, so it does not require clipping and is
saved.
• Line P3 to P4 lies completely outside the window, so it does not require clipping and is not
saved.
• Line P5 to P6 lies inside and outside partially, so it requires clipping and is clipped as shown in
the After Clipping of Fig 4.
7.5 Algorithm
Steps
1) Assign the region codes to both endpoints.
2) Perform an OR operation on both of these endpoints.
3) if OR = 0000,
Then it is completely visible (inside the window).
else
Perform an AND operation on both these endpoints.
i) if AND? 0000,
Then the line is invisible and not inside the window. Also, it can't
be considered for clipping.
ii) else
AND = 0000, the line is partially inside the window and considered for
clipping.
4) After confirming that the line is partially inside the window, we find the intersection with
the boundary of the window.
By using the following formula:-
Slope:- m= (y2-y1)/(x2-x1)
a) If the line passes through the top or the line intersects with the top boundary of the
window.
x = x + (y_wmax - y)/m
y = y_wmax
b) If the line passes through the bottom or the line intersects with the bottom boundary of
the window.
x = x + (y_wmin - y)/m
y = y_wmin
c) If the line passes through the left region or the line intersects with the left boundary of
the window.
y = y+ (x_wmin - x)*m
x = x_wmin
d) If the line passes through the right region or the line intersects with the right boundary
of the window.
y = y + (x_wmax -x)*m
x = x_wmax
5) Now, overwrite the endpoints with a new one and update it.
6) Repeat the 4th step till your line doesn't get completely clipped
Given a set of lines and a rectangular area of interest, the task is to remove lines that are
outside the area of interest and clip the lines that 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
7.6 Cohen-Sutherland algorithm
It 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 outlined as follows:-
Nine regions are created, eight "outside" regions and one
"inside" region.
For a given line's 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.
1. Completely inside the given rectangle: Bitwise OR of the region of two end points
of the line is 0 (Both points are inside the rectangle)
2. 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).
3. 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 the outside point and the rectangular window
becomes a new corner point, and the algorithm repeats
7.7 Cohen-Sutherland Line Clipping Program :
The following C program implements the Cohen-Sutherland line clipping algorithm. It is a
console application that takes the clipping window coordinates and the line endpoints as input,
then determines if the line is visible, partially visible, or invisible, and prints the clipped
coordinates if applicable.
This code uses standard input/output and math functions, avoiding the older graphics.
h library, making it compatible with modern C compilers (like GCC) across different operating
systems.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
// Define region codes (outcodes) using bit manipulation
const int INSIDE = 0; // 0000
const int LEFT = 1; // 0001
const int RIGHT = 2; // 0010
const int BOTTOM = 4; // 0100
const int TOP = 8; // 1000
// Define the clipping window boundaries (example values)
double x_min = 50, y_min = 50;
double x_max = 200, y_max = 200;
// Function to compute the region code for a point (x, y)
int computeOutCode(double x, double y) {
int code = INSIDE; // initialised as inside the window
if (x < x_min) // to the left of clipping window
code |= LEFT;
else if (x > x_max) // to the right of clipping window
code |= RIGHT;
if (y < y_min) // below the clipping window
code |= BOTTOM;
else if (y > y_max) // above the clipping window
code |= TOP;
return code;
}
// Function to implement the Cohen-Sutherland line clipping algorithm
void cohenSutherlandClip(double x1, double y1, double x2, double y2) {
int outcode1 = computeOutCode(x1, y1);
int outcode2 = computeOutCode(x2, y2);
int accept = 0; // used to check if the line is accepted or not
while (1) {
if (!(outcode1 | outcode2)) {
// Both endpoints are inside, accept the line
accept = 1;
break;
} else if (outcode1 & outcode2) {
// Both endpoints are in the same outside region, reject the line
break;
} else {
// Line is partially inside, we need to clip
double x, y;
int outcodeOut;
// Pick an endpoint that is outside the window
outcodeOut = outcode1 ? outcode1 : outcode2;
// Find the intersection point using the slope (m)
double m = (y2 - y1) / (x2 - x1);
if (outcodeOut & TOP) { // point is above the clip window
x = x1 + (x2 - x1) * (y_max - y1) / (y2 - y1); // or x = x1 + (y_max - y1)/ m
y = y_max;
} else if (outcodeOut & BOTTOM) { // point is below the clip window
x = x1 + (x2 - x1) * (y_min - y1) / (y2 - y1); // or x = x1 + (y_min - y1)/ m
y = y_min;
} else if (outcodeOut & RIGHT) { // point is to the right of clip window
y = y1 + (y2 - y1) * (x_max - x1) / (x2 - x1); // or y = y1 + m *(x_max - x1)
x = x_max;
} else if (outcodeOut & LEFT) { // point is to the left of clip window
y = y1 + (y2 - y1) * (x_min - x1) / (x2 - x1); // or y = y1 + m *(x_min - x1)
x = x_min;
}
// Update the outside endpoint to the intersection point
if (outcodeOut == outcode1) {
x1 = x;
y1 = y;
outcode1 = computeOutCode(x1, y1);
} else {
x2 = x;
y2 = y;
outcode2 = computeOutCode(x2, y2);
}
}
}
if (accept) {
printf("Line accepted from (%.2f, %.2f) to (%.2f, %.2f)\n", x1, y1, x2, y2);
} else {
printf("Line completely rejected\n");
}
}
int main() {
// Example usage:
// First line segment (partially inside)
cohenSutherlandClip(30, 70, 220, 180);
// Second line segment (completely outside)
cohenSutherlandClip(10, 10, 40, 40);
// Third line segment (completely inside)
cohenSutherlandClip(100, 100, 150, 150);
Key Concepts
• Region Codes (Outcodes): The 2D space is divided into 9 regions relative to the clipping
window. Each endpoint of a line is assigned a 4-bit binary code (Top, Bottom, Right, Left)
indicating its position.
• Trivial Acceptance: A line is completely inside and accepted if both endpoints have a
region code of 0000 (bitwise OR is 0).
• Trivial Rejection: A line is completely outside and rejected if the bitwise AND of the
region codes is not 0000 , meaning both points share a common outside region.
• Clipping (Partial Visibility): If neither of the above conditions is met, the line is partially
inside. The algorithm iteratively finds the intersection point with a window boundary
from an outside point, updates the endpoint's coordinates, and re-evaluates the new,
shorter line segment until it is either accepted or rejected.