0% found this document useful (0 votes)
5 views8 pages

CG 8 PP

The document outlines two experiments applying the Cohen-Sutherland Line Clipping algorithm in computer graphics. Experiment 8(A) focuses on clipping a line intersecting at one point with a viewport, detailing the algorithm, implementation, and learning outcomes. Experiment 8(B) extends this to lines intersecting at two or more points, emphasizing advanced clipping techniques and graphics programming skills.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views8 pages

CG 8 PP

The document outlines two experiments applying the Cohen-Sutherland Line Clipping algorithm in computer graphics. Experiment 8(A) focuses on clipping a line intersecting at one point with a viewport, detailing the algorithm, implementation, and learning outcomes. Experiment 8(B) extends this to lines intersecting at two or more points, emphasizing advanced clipping techniques and graphics programming skills.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment 8(A)
Student Name: Prince Kumar Singh UID: 22BCS11746
Branch: BE-CSE Section/Group: 612/A
Semester: 6th Date of Performance: /03/25
Subject Name: Computer Graphics Subject Code:22CSH-352
with lab

1. Aim: Apply the Cohen-Sutherland Line Clipping algorithm to clip a line intersecting at one point with a
given window

2. Objective: To clip a line intersecting at a single point with a window using the Cohen-Sutherland
Line Clipping algorithm.

3. Algorithm:
1. Initialize Graphics: Use initgraph() to start the graphics mode.
2. Input Data:
o Accept viewport coordinates (xmin, ymin, xmax, ymax).
o Accept line endpoints (x1, y1, x2, y2).
3. Compute Region Codes using getCode():
o Assign binary region codes to both endpoints based on their position relative to the
viewport.
4. Check for Trivial Cases:
o If both region codes are 0 → Line is completely inside the viewport → Draw the line
directly.
o If (code1 & code2) != 0 → Line is completely outside → Discard it.
5. Clip the Line:
o Use the Cohen-Sutherland algorithm to compute intersections.
o Iterate through clipping conditions:
 If the endpoint is outside, find the intersection with the viewport boundary.
 Update the endpoint and recalculate the region code.
o Stop when both endpoints are inside.
6. Display Results:
o Draw the original line before clipping.
o Clear the screen, draw the viewport, and display the clipped line if it is partially

1
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
inside.
o Show appropriate messages (e.g., "Line fully inside", "After Clipping", or "Line
outside viewport").
7. Terminate Graphics: Use closegraph() to exit the graphics mode.

4. Implementation/Code:
#include <iostream.h>
#include <graphics.h>
#include <conio.h>
#define LEFT 1
#define RIGHT 2
#define BOTTOM 4
#define TOP 8

int getCode(float x, float y, float xmin, float ymin, float xmax, float ymax) {
int code = 0;
if (x < xmin) code |= LEFT;
if (x > xmax) code |= RIGHT;
if (y < ymin) code |= BOTTOM;
if (y > ymax) code |= TOP;
return code;
}
void clipLine(float xmin, float ymin, float xmax, float ymax, float x1, float y1, float x2, float y2) {
int code1 = getCode(x1, y1, xmin, ymin, xmax, ymax);
int code2 = getCode(x2, y2, xmin, ymin, xmax, ymax);
int accept = 0;
float x1_orig = x1, y1_orig = y1, x2_orig = x2, y2_orig = y2;
if (code1 == 0 && code2 == 0) {
cleardevice();
rectangle(xmin, ymin, xmax, ymax);
setcolor(WHITE);
line(x1, y1, x2, y2);
outtextxy(10, 10, "Line is fully inside viewport (No Clipping Needed)");
getch();
return;
}
if (code1 & code2) {
cleardevice();
rectangle(xmin, ymin, xmax, ymax);
outtextxy(10, 10, "Line is completely outside viewport (Not visible)");
getch();
return;
}
while (1) {
if (!(code1 | code2)) {

2
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
accept = 1;
break;
} else {
int codeOut = code1 ? code1 : code2;
float x, y;
if (codeOut & TOP) {
x = x1 + (x2 - x1) * (ymax - y1) / (y2 - y1);
y = ymax;
} else if (codeOut & BOTTOM) {
x = x1 + (x2 - x1) * (ymin - y1) / (y2 - y1);
y = ymin;
} else if (codeOut & RIGHT) {
y = y1 + (y2 - y1) * (xmax - x1) / (x2 - x1);
x = xmax;
} else {
y = y1 + (y2 - y1) * (xmin - x1) / (x2 - x1);
x = xmin;
}
if (codeOut == code1) {
x1 = x; y1 = y;
code1 = getCode(x1, y1, xmin, ymin, xmax, ymax);
} else {
x2 = x; y2 = y;
code2 = getCode(x2, y2, xmin, ymin, xmax, ymax);
}
}
}
cleardevice();
rectangle(xmin, ymin, xmax, ymax);
setcolor(WHITE);
line(x1_orig, y1_orig, x2_orig, y2_orig);
outtextxy(10, 10, "Line before clipping");
getch();
cleardevice();
rectangle(xmin, ymin, xmax, ymax);
if (accept) {
setcolor(WHITE);
line(x1, y1, x2, y2);
outtextxy(10, 10, "After clipping");
}
getch();
}
int main() {
int gd = DETECT, gm;
float xmin, ymin, xmax, ymax, x1, y1, x2, y2;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");

3
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
cout<<"PRINCE KUMAR SINGH"<<endl;
cout<<" 22BCS11746"<<endl;
cout << "Enter bottom-left and top-right viewport coordinates: ";
cin >> xmin >> ymin >> xmax >> ymax;
cout << "Enter line coordinates: ";
cin >> x1 >> y1 >> x2 >> y2;
clipLine(xmin, ymin, xmax, ymax, x1, y1, x2, y2);
closegraph();
return 0;
}
5. Output:

6. Learning Outcomes:
 Understand Cohen-Sutherland Clipping for efficient line rendering.
 Apply Bitwise Operations (&, |) for region code handling.
 Use Graphics Programming (graphics.h) to draw and manipulate shapes.
 Learn Line Intersection Calculations for viewport clipping.
 Handle Different Cases (fully inside, outside, and partially inside).

4
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Experiment 8(B)

1. Aim: 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 two or more points with a window using the Cohen-
Sutherland Line Clipping algorithm.

3. Algorithm:
 Input viewport (xmin, ymin, xmax, ymax) and line endpoints (x1, y1, x2, y2).
 Compute region codes for both endpoints.
 Check trivial cases:
o If both endpoints are inside → Draw without clipping.
o If both share an outside region → Reject the line.
 Clip the line at viewport boundaries:
o Compute intersection points with the clipping edges.
o Update the outside endpoint and recalculate the region code.
o Repeat until both points are inside.
 Draw the clipped line if valid, else display rejection message.

4. Implementation/Code:
#include <iostream.h>
#include <graphics.h>
#include <conio.h>
#define LEFT 1
#define RIGHT 2
#define BOTTOM 4
#define TOP 8
int getCode(float x, float y, float xmin, float ymin, float xmax, float ymax) {
int code = 0;
if (x < xmin) code |= LEFT;
if (x > xmax) code |= RIGHT;
if (y < ymin) code |= BOTTOM;
if (y > ymax) code |= TOP;
return code;
}void clipLine(float xmin, float ymin, float xmax, float ymax, float x1, float y1, float x2, float y2) {
int code1 = getCode(x1, y1, xmin, ymin, xmax, ymax);
int code2 = getCode(x2, y2, xmin, ymin, xmax, ymax);
int accept = 0;
float x1_orig = x1, y1_orig = y1, x2_orig = x2, y2_orig = y2;
if (code1 == 0 && code2 == 0) {
cleardevice();
rectangle(xmin, ymin, xmax, ymax);
setcolor(WHITE);
5
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
line(x1, y1, x2, y2);
outtextxy(10, 10, "Line is fully inside viewport (No Clipping Needed)");
getch();
return;
} if (code1 & code2) {
cleardevice();
rectangle(xmin, ymin, xmax, ymax);
outtextxy(10, 10, "Line is completely outside viewport (Not visible)");
getch();
return;
}while (1) {
if ((code1 == 0) && (code2 == 0)) {
accept = 1;
break;
} else if (code1 & code2) {
break;
} else {
int codeOut = code1 ? code1 : code2;
float x, y;
if (codeOut & TOP) {
x = x1 + (x2 - x1) * (ymax - y1) / (y2 - y1);
y = ymax;
} else if (codeOut & BOTTOM) {
x = x1 + (x2 - x1) * (ymin - y1) / (y2 - y1);
y = ymin;
} else if (codeOut & RIGHT) {
y = y1 + (y2 - y1) * (xmax - x1) / (x2 - x1);
x = xmax;
} else {
y = y1 + (y2 - y1) * (xmin - x1) / (x2 - x1);
x = xmin;
} if (codeOut == code1) {
x1 = x; y1 = y;
code1 = getCode(x1, y1, xmin, ymin, xmax, ymax);
} else {
x2 = x; y2 = y;
code2 = getCode(x2, y2, xmin, ymin, xmax, ymax);
}
}
}
cleardevice();
rectangle(xmin, ymin, xmax, ymax);
setcolor(WHITE);
line(x1_orig, y1_orig, x2_orig, y2_orig);
outtextxy(10, 10, "Line before clipping");
getch();
cleardevice();
rectangle(xmin, ymin, xmax, ymax);
if (accept) {
setcolor(WHITE);
6
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
line(x1, y1, x2, y2);
outtextxy(10, 10, "After clipping");
} else {
outtextxy(10, 10, "Line is completely outside viewport (Not visible)");
}
getch(); }
int main() {
int gd = DETECT, gm;
float xmin, ymin, xmax, ymax, x1, y1, x2, y2;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
cout << "PRINCE KUMAR SINGH" << endl;
cout << "22BCS11746" << endl;
cout << "Enter bottom-left and top-right viewport coordinates: ";
cin >> xmin >> ymin >> xmax >> ymax;
cout << "Enter line coordinates: ";
cin >> x1 >> y1 >> x2 >> y2;
clipLine(xmin, ymin, xmax, ymax, x1, y1, x2, y2);
closegraph();
return 0;
}

5. Output:

7
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

6. Learning Outcome:
a. Advanced Cohen-Sutherland Clipping: Learn how the algorithm handles multiple
intersection points.
b. Bitwise Operations: Understand & and | operations for efficient region code checking.
c. Multi-Intersection Handling: Learn to compute multiple clipping points dynamically.
d. Graphics Implementation in C++: Gain experience with graphics.h for visualization.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy