0% found this document useful (0 votes)
11 views

203 Computer Graphics

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

203 Computer Graphics

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 36

Cohen southerland line

clipping algorithm
Implementation
#include<iostream.h>
#include: Preprocessor directive used to include header files.
• <iostream.h>: This includes the input/output stream library for C++.
The .h extension indicates it’s a header file
#include<conio.h>

• <conio.h>: This includes functions used for console input and output in Turbo C++.

• #include<graphics.h>

• <graphics.h>: This includes the graphics library for Turbo C++, allowing you to create graphical
applications.
Const int INSIDE = 0; // 0000
const int LEFT = 1; // 0001
const int RIGHT = 2; // 0010
const int BOTTOM = 4; // 0100
• const int TOP = 8;

Const int: Declares integer constants.


• INSIDE, LEFT, RIGHT, BOTTOM, TOP: Constants representing the region
codes for the Cohen-Sutherland line clipping algorithm.
Const int x_max = 200;
const int y_max = 200;
const int x_min = 100;
• const int y_min = 100;

Const int: Declares integer constants.


• X_max, y_max, x_min, y_min: Constants representing the minimum
and maximum coordinates of the clipping window.
Int computeCode(double x, double y) {

Int: Specifies that the function computeCode returns an integer.


computeCode: Name of the function.
• (double x, double y): Parameters of the function, representing the
coordinates of a point.
Int code = INSIDE;

Int code: Declares an integer variable named code.


• = INSIDE: Initializes code with the value of the constant INSIDE.
If (x < x_min)

If: Conditional statement.


• X < x_min: Conditio
• in that checks if x is less than x_min.
Code |= LEFT;

|=: Bitwise OR assignment operator.


LEFT: Constant representing the left region code.
• This line updates the code variable to include the left region code.
Else if (x > x_max)

Else if: Conditional statement that executes if the previous condition is


false.
• X > x_max: Condition that checks if x is greater than x_max.
Code |= RIGHT;

• Similar to the previous line, updates code to include the right region
code
If (y < y_min)

• Checks if y is less than y_min


Code |= BOTTOM;

• Updates code to include the bottom region code.


Else if (y > y_max)

• Checks if y is greater than y_max


Code |= TOP;

• Updates code to include the top region code.


Return code;

• Returns the computed region code.


Void cohenSutherlandClip(double x1, double y1, double x2, double
y2) {

Void: Specifies that the function cohenSutherlandClip doesn’t return


any value.
cohenSutherlandClip: Name of the function.
• (double x1, double y1, double x2, double y2): Parameters of the
function, representing the endpoints of a line segment.
Int code1 = computeCode(x1, y1);

• Declares an integer variable code1 and initializes it with the region


code of point (x1, y1).
Int code2 = computeCode(x2, y2);

• Similar to the previous line, initializes code2 with the region code of
point (x2, y2).
Int accept = 0; // 0 represents false

• Declares an integer variable accept and initializes it with 0,


representing false
While (1) { // Using 1 instead of true

• While (1): Infinite loop. The condition 1 is always true.


Accept = 1; // 1 represents true

• Sets accept to 1, representing true.


If ((code1 == 0) && (code2 == 0)) {

• Checks if both code1 and code2 are 0.


Break;
Breaks out of the loop if the condition is met.

Else if (code1 & code2) {


• Checks if there is a common bit set between code1 and code2.

• else {
• Executes if none of the above conditions are met.
Double x, y;
Declares variables x and y to store the clipped coordinates.
Int code_out = (code1 != 0) ? Code1 : code2;
• Determines which endpoint to clip based on which one lies outside
the clipping window.

• if (code_out & TOP) {


• Checks if the TOP region code is set for the current endpoint.
If (accept) {
Checks if accept is true.
Cout << “Line accepted from (“ << x1 << “, “ << y1 << “) to (“ << x2 << “,
“ << y2 << “)” << endl;
• Outputs the accepted line segment.

• rectangle(x_min, y_min, x_max, y_max);


• Draws the clipping window as a rectangle
Cin >> x1 >> y1 >> x2 >> y2;
Reads the coordinates of the line segment from the user.

Initgraph(&gd, &gm, “C:\\TC\\BGI”);


Initializes the graphics mode with the specified graphics driver, mode,
and path to the BGI folder.
Complete program
#include<iostream.h>
#include<conio.h>
#include<graphics.h>

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
);
getch(); // Wait for user input before closing
}
#include<iostream.h>
#include<conio.h>
#include<graphics.h>

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

const int x_max = 200;


const int y_max = 200;
const int x_min = 100;
const int y_min = 100;
Int computeCode(double x, double y) {
int code = INSIDE;

if (x < x_min) // to the left of rectangle


code |= LEFT;
else if (x > x_max) // to the right of rectangle
code |= RIGHT;
if (y < y_min) // below the rectangle
code |= BOTTOM;
else if (y > y_max) // above the rectangle
code |= TOP;

return code;
}
Void cohenSutherlandClip(double x1, double y1, double x2, double y2) {
int code1 = computeCode(x1, y1);
int code2 = computeCode(x2, y2);
int accept = 0; // 0 represents false

while (1) { // Using 1 instead of true


if ((code1 == 0) && (code2 == 0)) {
accept = 1; // 1 represents true
break;
} else if (code1 & code2) {
break;
} else {
double x, y;
int code_out = (code1 != 0) ? Code1 : code2;

if (code_out & TOP) {


x = x1 + (x2 – x1) * (y_max – y1) / (y2 – y1);
y = y_max;
}
Else if (code_out & BOTTOM) {
x = x1 + (x2 – x1) * (y_min – y1) / (y2 – y1);
y = y_min;
} else if (code_out & RIGHT) {
y = y1 + (y2 – y1) * (x_max – x1) / (x2 – x1);
x = x_max;
} else if (code_out & LEFT) {
y = y1 + (y2 – y1) * (x_min – x1) / (x2 – x1);
x = x_min;
}
If (code_out == code1) {
x1 = x;
y1 = y;
code1 = computeCode(x1, y1);
} else {
x2 = x;
y2 = y;
code2 = computeCode(x2, y2);
}
}
}
If (accept) {
// Draw the accepted line using graphics
int gd = DETECT, gm;
initgraph(&gd, &gm, “C:\\TC\\BGI”); // Modify the path accordingly
line(x1, y1, x2, y2);
closegraph();
cout << “Line accepted from (“ << x1 << “, “ << y1 << “) to (“ << x2 << “, “ << y2 << “)” << endl;
} else {
cout << “Line rejected” << endl;
}
}
Void main() {
double x1, y1, x2, y2;
cout << “Enter the coordinates of the line (x1, y1, x2, y2): “;
cin >> x1 >> y1 >> x2 >> y2;

// Call Cohen-Sutherland algorithm


cohenSutherlandClip(x1, y1, x2, y2);
getch(); // Wait for user input before closing
}
Thank U

You might also like