DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Experiment 8
Student Name: Aditya Sharma UID: 22BCS15081
Branch: B.E CSE Section/Group: IOT_604/B
Semester: 6th Date of Performance: 14/03/25
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:
a). For One Point
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
void main() {
int gd = DETECT, gm;
float i, xmax, ymax, xmin, ymin, x1, y1, x2, y2, m;
float start[4], end[4], code[4];
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
printf("\n\tEnter the bottom-left coordinate of viewport: ");
scanf("%f %f", &xmin, &ymin);
printf("\n\tEnter the top-right coordinate of viewport: ");
scanf("%f %f", &xmax, &ymax);
printf("\nEnter the coordinates for starting point of line: ");
scanf("%f %f", &x1, &y1);
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
printf("\nEnter the coordinates for ending point of line: ");
scanf("%f %f", &x2, &y2);
// Initialize region codes
for (i = 0; i < 4; i++) {
start[i] = 0;
end[i] = 0;
}
// Check for vertical line case to avoid division by zero
if (x2 - x1 == 0) {
m = 0; // Avoid division by zero
} else {
m = (y2 - y1) / (x2 - x1);
}
// Calculate region codes
if (x1 < xmin) start[0] = 1;
if (x1 > xmax) start[1] = 1;
if (y1 > ymax) start[2] = 1;
if (y1 < ymin) start[3] = 1;
if (x2 < xmin) end[0] = 1;
if (x2 > xmax) end[1] = 1;
if (y2 > ymax) end[2] = 1;
if (y2 < ymin) end[3] = 1;
for (i = 0; i < 4; i++)
code[i] = start[i] & end[i];
// Case 1: Line is completely visible
if ((code[0] == 0) && (code[1] == 0) && (code[2] == 0) && (code[3] == 0)) {
if ((start[0] == 0) && (start[1] == 0) && (start[2] == 0) && (start[3] == 0) &&
(end[0] == 0) && (end[1] == 0) && (end[2] == 0) && (end[3] == 0)) {
cleardevice();
printf("\n\t\tThe line is totally visible\n\t\tand not a clipping candidate");
rectangle(xmin, ymin, xmax, ymax);
line(x1, y1, x2, y2);
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
getch();
} else {
// Case 2: Line is partially visible
cleardevice();
printf("\n\t\tLine is partially visible");
rectangle(xmin, ymin, xmax, ymax);
line(x1, y1, x2, y2);
getch();
// Perform clipping
if ((start[2] == 0) && (start[3] == 1)) {
x1 = x1 + (ymin - y1) / m;
y1 = ymin;
}
if ((end[2] == 0) && (end[3] == 1)) {
x2 = x2 + (ymin - y2) / m;
y2 = ymin;
}
if ((start[2] == 1) && (start[3] == 0)) {
x1 = x1 + (ymax - y1) / m;
y1 = ymax;
}
if ((end[2] == 1) && (end[3] == 0)) {
x2 = x2 + (ymax - y2) / m;
y2 = ymax;
}
if ((start[1] == 0) && (start[0] == 1)) {
y1 = y1 + m * (xmin - x1);
x1 = xmin;
}
if ((end[1] == 0) && (end[0] == 1)) {
y2 = y2 + m * (xmin - x2);
x2 = xmin;
}
if ((start[1] == 1) && (start[0] == 0)) {
y1 = y1 + m * (xmax - x1);
x1 = xmax;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
if ((end[1] == 1) && (end[0] == 0)) {
y2 = y2 + m * (xmax - x2);
x2 = xmax;
}
// Display clipped line
cleardevice();
printf("\n\t\tAfter clipping:");
rectangle(xmin, ymin, xmax, ymax);
line(x1, y1, x2, y2);
getch();
}
} else {
// Case 3: Line is completely outside
cleardevice();
printf("\nLine is completely outside the viewport.");
rectangle(xmin, ymin, xmax, ymax);
}
getch();
closegraph();
}
b). For Two Points
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
void main() {
int gd = DETECT, gm;
float i, xmax, ymax, xmin, ymin, x1, y1, x2, y2, m;
float start[4], end[4], code[4];
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
printf("\n\tEnter the bottom-left coordinate of viewport: ");
scanf("%lf %lf", &xmin, &ymin);
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
printf("\n\tEnter the top-right coordinate of viewport: ");
scanf("%lf %lf", &xmax, &ymax);
printf("\nEnter the coordinates for starting point of line: ");
scanf("%lf %lf", &x1, &y1);
printf("\nEnter the coordinates for ending point of line: ");
scanf("%lf %lf", &x2, &y2);
for (i = 0; i < 4; i++) {
start[i] = 0;
end[i] = 0;
}
// Handle vertical line case to prevent division by zero
if (x2 - x1 == 0) {
m = 0;
} else {
m = (y2 - y1) / (x2 - x1);
}
if (x1 < xmin) start[0] = 1;
if (x1 > xmax) start[1] = 1;
if (y1 > ymax) start[2] = 1;
if (y1 < ymin) start[3] = 1;
if (x2 < xmin) end[0] = 1;
if (x2 > xmax) end[1] = 1;
if (y2 > ymax) end[2] = 1;
if (y2 < ymin) end[3] = 1;
for (i = 0; i < 4; i++)
code[i] = start[i] & end[i]; // Fixed bitwise operation
if ((code[0] == 0) && (code[1] == 0) && (code[2] == 0) && (code[3] == 0)) {
if ((start[0] == 0) && (start[1] == 0) && (start[2] == 0) && (start[3] == 0) &&
(end[0] == 0) && (end[1] == 0) && (end[2] == 0) && (end[3] == 0)) {
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
cleardevice();
printf("\n\t\tThe line is totally visible\n\t\tand not a clipping candidate");
rectangle(xmin, ymin, xmax, ymax);
line(x1, y1, x2, y2);
getch();
} else {
cleardevice();
printf("\n\t\tLine is partially visible");
rectangle(xmin, ymin, xmax, ymax);
line(x1, y1, x2, y2);
getch();
// Perform clipping
if ((start[2] == 0) && (start[3] == 1)) {
x1 = x1 + (ymin - y1) / m;
y1 = ymin;
}
if ((end[2] == 0) && (end[3] == 1)) {
x2 = x2 + (ymin - y2) / m;
y2 = ymin;
}
if ((start[2] == 1) && (start[3] == 0)) {
x1 = x1 + (ymax - y1) / m;
y1 = ymax;
}
if ((end[2] == 1) && (end[3] == 0)) {
x2 = x2 + (ymax - y2) / m;
y2 = ymax;
}
if ((start[1] == 0) && (start[0] == 1)) {
y1 = y1 + m * (xmin - x1);
x1 = xmin;
}
if ((end[1] == 0) && (end[0] == 1)) {
y2 = y2 + m * (xmin - x2);
x2 = xmin;
}
if ((start[1] == 1) && (start[0] == 0)) {
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
y1 = y1 + m * (xmax - x1);
x1 = xmax;
}
if ((end[1] == 1) && (end[0] == 0)) {
y2 = y2 + m * (xmax - x2);
x2 = xmax;
}
// Display clipped line
cleardevice();
printf("\n\t\tAfter clipping:");
rectangle(xmin, ymin, xmax, ymax);
line(x1, y1, x2, y2);
getch();
}
} else {
cleardevice();
printf("\nLine is invisible");
rectangle(xmin, ymin, xmax, ymax);
}
getch();
closegraph();
}
4. Output
a). For One Points
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
a).
b).
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
5. Learning Outcome
• Understanding Line Clipping – Learned Cohen-Sutherland algorithm, region
codes, and clipping logic.
• Debugging C Graphics – Fixed division by zero, logical errors, and improved code
efficiency.
• Graphics Implementation – Used Turbo C++ functions to create and modify
viewport-based line rendering.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Experiment 9
Student Name: Aditya Sharma UID: 22BCS15081
Branch: B.E CSE Section/Group: IOT_604/B
Semester: 6th Date of Performance: 28/03/25
Subject Name: Computer Graphics Subject Code: 22CSH-352
1. Aim: Demonstrate the result of window-to-viewport transformation by implementing
and visualizing the process.
2. Objective: To Demonstrate the result of window-to-viewport transformation by
implementing and visualizing the process.
3. Implementation/Code:
#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h>
void main() {
int gd=DETECT, gm;
initgraph(&gd, &gm, ""); // Use empty string instead of NULL
clrscr(); // Place after initgraph()
float wxmin=10, wxmax=150, wymin=10, wymax=250;
float vxmin=200, vxmax=600, vymin=10, vymax=250;
int wx1=30, wy1=50, wx2=100, wy2=180;
// Draw window and viewport
rectangle(wxmin, wymin, wxmax, wymax);
rectangle(vxmin, vymin, vxmax, vymax);
// Scaling factors
float sx = (vxmax - vxmin) / (wxmax - wxmin);
float sy = (vymax - vymin) / (wymax - wymin);
// Draw original line
line(wx1, wy1, wx2, wy2);
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
// Transform coordinates
float vx1 = sx * (wx1 - wxmin) + vxmin;
float vy1 = sy * (wy1 - wymin) + vymin;
float vx2 = sx * (wx2 - wxmin) + vxmin;
float vy2 = sy * (wy2 - wymin) + vymin;
// Draw transformed line in viewport
line(vx1, vy1, vx2, vy2);
// Labels
outtextxy(60, 260, "Window");
outtextxy(360, 260, "Viewport");
getch();
closegraph(); // Properly exit graphics mode
}
4. Output
5. Learning Outcome
• Learned how to transform coordinates from a window to a viewport.
• Practiced rectangle(), line(), and outtextxy() for visualization.
• Implemented formulas to scale and position graphical objects.