DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Experiment 8
Student Name: Tekrat Prajapati UID: 22BC13545
Branch: CSE Section/Group: 22BCS_IOT-635-A
Semester: 6th Date of Performance: 28th March,2025
Subject Name: Computer Graphics Subject Code: 22CSH-352
1. Aim: a) Apply the Cohen-Sutherland Line Clipping algorithm to clip a line intersecting
at one point with a given window.
b) Apply the Cohen-Sutherland Line Clipping algorithm to clip a line intersecting at two
or more points with a given window
2. Objective: To clip a line intersecting at a single point and two or more points with a
window using the Cohen-Sutherland Line Clipping algorithm.
3. Implementation/Code:
#include <graphics.h>
#include <iostream>
using namespace std;
void onePointClipping(int x1, int y1, int x2, int y2, int xmin, int ymin, int xmax, int ymax, int choice) {
if (choice == 1) {
setcolor(WHITE);
line(x1, y1, x2, y2);
if (x2 > xmax) x2 = xmax;
if (y2 > ymax) y2 = ymax;
if (x2 < xmin) x2 = xmin;
if (y2 < ymin) y2 = ymin;
setcolor(WHITE);
line(x1, y1, x2, y2);
} }
void twoPointClipping(int &x1, int &y1, int &x2, int &y2, int xmin, int ymin, int xmax, int ymax) {
if (x1 < xmin) x1 = xmin;
if (y1 < ymin) y1 = ymin;
if (x1 > xmax) x1 = xmax;
if (y1 > ymax) y1 = ymax;
if (x2 < xmin) x2 = xmin;
if (y2 < ymin) y2 = ymin;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
if (x2 > xmax) x2 = xmax;
if (y2 > ymax) y2 = ymax; }
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, NULL);
setbkcolor(RED);
cleardevice();
int xmin, ymin, xmax, ymax;
int x1, y1, x2, y2;
int clipChoice, displayChoice;
cout << "Enter the bottom-left coordinate of viewport: ";
cin >> xmin >> ymin;
cout << "Enter the top-right coordinate of viewport: ";
cin >> xmax >> ymax;
cout << "Enter the coordinates for starting point of line: ";
cin >> x1 >> y1;
cout << "Enter the coordinates for ending point of line: ";
cin >> x2 >> y2;
cout << "\nChoose Clipping Method:\n";
cout << "1. One Point Clipping\n";
cout << "2. Two Point Clipping\n";
cout << "Enter choice: ";
cin >> clipChoice;
cout << "\nChoose Display Option:\n";
cout << "1. Show Original Line\n";
cout << "2. Show Clipped Line\n";
cout << "Enter choice: ";
cin >> displayChoice;
setcolor(YELLOW);
rectangle(xmin, ymin, xmax, ymax);
switch (clipChoice) {
case 1:
onePointClipping(x1, y1, x2, y2, xmin, ymin, xmax, ymax, displayChoice);
break;
case 2:
if (displayChoice == 1) {
setcolor(WHITE);
line(x1, y1, x2, y2);
} else {
twoPointClipping(x1, y1, x2, y2, xmin, ymin, xmax, ymax);
setcolor(WHITE);
line(x1, y1, x2, y2);
}
break;
default:
cout << "Invalid choice!";
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
}
getch();
closegraph();
return 0;
}
4. Output:
Figure 1: Original Line (One Point) Figure 2: Clipped Line (One Point)
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Figure 3: Original Line (Two Point) Figure 4: Clipped Line (Two Point)
5. Learning Outcome:
• I have learnt how to implement algorithms for Clipping in graphics programming.
• Learn how to manipulate coordinates and plot points or lines using specific coordinates in a
window.
• Understand how to display region codes and other relevant information in the graphical window
based on user input.
• Implement a simple form of the Cohen-Sutherland clipping algorithm to handle line clipping
based on window boundaries.
• I have understood how loop optimizations and integer-based calculations can improve the
performance of graphics algorithms, especially in real-time applications.
• Learn how one-point and two-point clipping techniques work to trim lines within a given
viewport.