#Include .H: Cppcopy Code
#Include .H: Cppcopy Code
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.
cppCopy code
#include<conio.h>
<conio.h>: This includes functions used for console input and output in Turbo
C++.
cppCopy code
#include<graphics.h>
<graphics.h>: This includes the graphics library for Turbo C++, allowing you to
create graphical applications.
cppCopy code
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: Declares integer constants.
INSIDE, LEFT, RIGHT , BOTTOM, TOP: Constants representing the region codes for
the Cohen-Sutherland line clipping algorithm.
cppCopy code
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.
cppCopy code
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.
cppCopy code
int code = INSIDE;
int code: Declares an integer variable named code.
= INSIDE: Initializes code with the value of the constant INSIDE.
cppCopy code
if (x < x_min)
if: Conditional statement.
x < x_min: Condition that checks if x is less than x_min.
cppCopy code
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.
cppCopy 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.
cppCopy code
code |= RIGHT;
Similar to the previous line, updates code to include the right region code.
cppCopy code
if (y < y_min)
Checks if y is less than y_min.
cppCopy code
code |= BOTTOM;
Updates code to include the bottom region code.
cppCopy code
else if (y > y_max)
Checks if y is greater than y_max.
cppCopy code
code |= TOP;
Updates code to include the top region code.
cppCopy code
return code;
Returns the computed region code.
cppCopy 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.
cppCopy code
int code1 = computeCode (x1, y1);
Declares an integer variable code1 and initializes it with the region code of point
(x1, y1).
cppCopy code
int code2 = computeCode (x2, y2);
Similar to the previous line, initializes code2 with the region code of point (x2,
y2).
cppCopy code
int accept = 0 ; // 0 represents false
Declares an integer variable accept and initializes it with 0, representing false.
cppCopy code
while ( 1 ) { // Using 1 instead of true
while (1): Infinite loop. The condition 1 is always true.
cppCopy code
accept = 1 ; // 1 represents true
Sets accept to 1, representing true.
cppCopy code
if ((code1 == 0 ) && (code2 == 0 )) {
Checks if both code1 and code2 are 0.
cppCopy code
break ;
Breaks out of the loop if the condition is met.
cppCopy code
else if (code1 & code2) {
Checks if there is a common bit set between code1 and code2.
cppCopy code
else {
Executes if none of the above conditions are met.
cppCopy code
double x, y;
Declares variables x and y to store the clipped coordinates.
cppCopy code
int code_out = (code1 != 0 ) ? code1 : code2;
Determines which endpoint to clip based on which one lies outside the clipping
window.
cppCopy code
if (code_out & TOP) {
Checks if the TOP region code is set for the current endpoint.
cppCopy code
if (accept) {
Checks if accept is true.
cppCopy code
cout << "Line accepted from (" << x1 << ", " << y1 << ") to (" << x2 << ", " << y2 << ")" << endl;
Outputs the accepted line segment.
cppCopy code
rectangle (x_min, y_min, x_max, y_max);
Draws the clipping window as a rectangle.
cppCopy code
cin >> x1 >> y1 >> x2 >> y2;
Reads the coordinates of the line segment from the user.
cppCopy code
initgraph (&gd, &gm, "C:\\TC\\BGI" );
Initializes the graphics mode with the specified graphics driver, mode, and path
to the BGI folder.
cppCopy code
cohenSutherlandClip (x1, y1, x2, y2);