SlideShare a Scribd company logo
Experiment no. – 3.1
Student Name: Krishna Kumar UID: 19BCS2605
Branch: CSE - 11 Section/Group : ‘C’
Semester: 5th
Date of Performance: 21 Oct 2021
Subject Name: Computer Graphics Lab Subject Code: CSP - 305
1. Aim/Overview of the practical: a) To display 4-bit region code for end points of a line and
check whether line is completely on the screen or off the screen
b) To clip a line intersecting at one point with given window using Cohen
Sutherland Line Clipping algorithm.
c) To clip a line intersecting at Two or more points with given window using
Cohen Sutherland Line Clipping algorithm
2. Task to be done: a) To display 4-bit region code for end points of a line
b) To clip a line intersecting at one point with given window
c) To clip a line intersecting at Two or more points with given window
3. Algorithm :
a) algorithms for display 4-bit region code for end points of a line
Step 1: Start
Step 1.1: taking two point, starting point and end point of line for user
Step 1.2: taking two co-ordinate of rectangle, starting co-ordinate and end co-
ordinate of rectangle for user
Step 1. 4: Assign a region code for two endpoints of given line
Step 2 : If both endpoints have a region code 0000 then given line is completely
inside and we will keep this line
Step 3 : If step 2 fails, perform the logical AND operation for both region codes.
Step 3.1 : If the result is not 0000, then given line is completely outside.
Step 3.2 : Else line is partially inside.
Step 3.2.a : Choose an endpoint of the line that is outside the given rectangle.
Step 3.2.b : Find the intersection point of the rectangular boundary (based on
region code).
Step 3.2.c : Replace endpoint with the intersection point and update the region
code.
Step 3.2.d : Repeat step 2 until we find a clipped line either trivially accepted or
rejected.
Step 4 : Repeat step 1 for all lines
Step 5: using while for taking point of line again and again form user.
Step 6: Displaying window, line and line in side window at a green colour at
green colour
Step 7: End
b) algorithms for clip a line intersecting at one point with given window
Step 1: Start
Step 1.1: taking two point, starting point and end point of line for user
Step 1.2: taking two co-ordinate of rectangle, starting co-ordinate and end co-
ordinate of rectangle for user
Step 1.3 : Assign a region code for two endpoints of given line
Step 2 : If both endpoints have a region code 0000 then given line is completely
inside and we will keep this line
Step 3 : If step 2 fails, perform the logical AND operation for both region codes.
Step 3.1 : If the result is not 0000, then given line is completely outside.
Step 3.2 : Else line is partially inside.
Step 3.2.a : Choose an endpoint of the line that is outside the given rectangle.
Step 3.2.b : Find the intersection point of the rectangular boundary (based on
region code).
Step 3.2.c : Replace endpoint with the intersection point and update the region
code.
Step 3.2.d : Repeat step 2 until we find a clipped line either trivially accepted or
rejected.
Step 4 : Repeat step 1 for all lines
Step 5: Displaying window and line before line clipping and after line clipping
at green colour
Step 6: End
c) algorithms for clip a line intersecting at Two or more points with given window
Step 1: Start
Step 1.1: taking two point, starting point and end point of line for user
Step 1.2: taking two co-ordinate of rectangle, starting co-ordinate and end co-
ordinate of rectangle for user
Step 1.3 : Assign a region code for two endpoints of given line
Step 2 : If both endpoints have a region code 0000 then given line is completely
inside and we will keep this line
Step 3 : If step 2 fails, perform the logical AND operation for both region codes.
Step 3.1 : If the result is not 0000, then given line is completely outside.
Step 3.2 : Else line is partially inside.
Step 3.2.a : Choose an endpoint of the line that is outside the given rectangle.
Step 3.2.b : Find the intersection point of the rectangular boundary (based on
region code).
Step 3.2.c : Replace endpoint with the intersection point and update the region
code.
Step 3.2.d : Repeat step 2 until we find a clipped line either trivially accepted or
rejected.
Step 4 : Repeat step 1 for all lines
Step 5: Displaying window and line before line clipping and after line clipping
Step 6: End
4. Programming Code:
a) Code for display 4-bit region code for end points of a line
#include<iostream>
#include<graphics.h>
#include<math.h>
#include<conio.h>
using namespace std;
class CohenSutherLandAlgo
{
private:
double x1,y1,x2,y2;
double x_max,y_max,x_min,y_min;
const int INSIDE = 0;
const int LEFT = 1;
const int RIGHT = 2;
const int BOTTOM = 4;
const int TOP = 8;
public:
CohenSutherLandAlgo()
{
x1 = 0.0;
x2 = 0.0;
y1 = 0.0;
y2 = 0.0;
}
void getCoordinates();
void getClippingRectangle();
int generateCode(double x, double y);
void cohenSutherland();
};
void CohenSutherLandAlgo::getCoordinates()
{
std::cout << "Enter Co-ordinates of P1(X1,Y1) of Line Segment : ";
std::cin >> x1>>y1;
std::cout << "Enter Co-ordinates of P2(X2,Y2) of Line Segment :";
std::cin >> x2>>y2;
std::cout<<endl;
setcolor(WHITE);
line(x1,y1,x2,y2);
}
void CohenSutherLandAlgo::getClippingRectangle()
{
std::cout << "Enter the Co-ordinates of Interested Rectangle."<<endl;
std::cout << "Enter the X_MAX and Enter the Y_MAX : ";
std::cin >> x_max>>y_max;
std::cout << "Enter the X_MIN and Enter the Y_MIN : ";
std::cin >> x_min>>y_min;
rectangle(x_min,y_min,x_max,y_max);
}
int CohenSutherLandAlgo::generateCode(double x, double y)
{
int code = INSIDE;
if (x < x_min)
code |= LEFT;
else if (x > x_max)
code |= RIGHT;
if (y < y_min)
code |= BOTTOM;
else if (y > y_max)
code |= TOP;
return code;
}
void CohenSutherLandAlgo::cohenSutherland()
{
int code1 = generateCode(x1, y1);
int code2 = generateCode(x2, y2);
bool accept = false;
while (true)
{
if ((code1 == 0) && (code2 == 0))
{
accept = true;
break;
}
else if (code1 & code2)
{
break;
}
else
{
int code_out;
double x, y;
if (code1 != 0)
code_out = code1;
else
code_out = 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 = generateCode(x1, y1);
}
else
{
x2 = x;
y2 = y;
code2 = generateCode(x2, y2);
}
}
}
if (accept)
{ std::cout<<endl;
std::cout <<"Line accepted from " <<"("<< x1 << ", "
<< y1 << ")" << " to "<< "(" << x2 << ", " << y2 << ")" <<
std::endl;
setcolor(GREEN);
line(x1,y1,x2,y2);
}
else
std::cout << "Line rejected" << std::endl;
}
int main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:turboc3bgi");
int x;
outtextxy(5,5,"White color that part of line segment is not accept ");
setcolor(GREEN);
outtextxy(5,20,"Green color that part of line segment is accept ");
setcolor(WHITE);
CohenSutherLandAlgo c;
c.getClippingRectangle();
do{
c.getCoordinates();
c.cohenSutherland();
std::cout<<endl;
cout<<"Please press 1 (one) for continue :";
cin>>x;
}
while(x==1);
getch();
closegraph();
}
b) Code for clip a line intersecting at one point with given window
#include<iostream>
#include<graphics.h>
#include<math.h>
#include<conio.h>
using namespace std;
class CohenSutherLandAlgo
{
private:
double x1,y1,x2,y2;
double x_max,y_max,x_min,y_min;
const int INSIDE = 0;
const int LEFT = 1;
const int RIGHT = 2;
const int BOTTOM = 4;
const int TOP = 8;
public:
CohenSutherLandAlgo()
{
x1 = 0.0;
x2 = 0.0;
y1 = 0.0;
y2 = 0.0;
}
void getCoordinates();
void getClippingRectangle();
int generateCode(double x, double y);
void cohenSutherland();
};
void CohenSutherLandAlgo::getCoordinates()
{
std::cout << "Enter Co-ordinates of P1(X1,Y1) of Line Segment : ";
std::cin >> x1>>y1;
std::cout << "Enter Co-ordinates of P2(X2,Y2) of Line Segment :";
std::cin >> x2>>y2;
std::cout<<endl;
line(x1,y1,x2,y2);
}
void CohenSutherLandAlgo::getClippingRectangle()
{
std::cout << "Enter the Co-ordinates of Interested Rectangle."<<endl;
std::cout << "Enter the X_MAX and Enter the Y_MAX : ";
std::cin >> x_max>>y_max;
std::cout << "Enter the X_MIN and Enter the Y_MIN : ";
std::cin >> x_min>>y_min;
rectangle(x_min,y_min,x_max,y_max);
outtextxy(x_min,y1-20,"Before line Clipping");
setcolor(GREEN);
outtextxy(x_max+50,y1-20,"After line Clipping");
rectangle(x_max+50,y_min,x_max+50+x_max-x_min,y_max);
}
int CohenSutherLandAlgo::generateCode(double x, double y)
{
int code = INSIDE;
if (x < x_min)
code |= LEFT;
else if (x > x_max)
code |= RIGHT;
if (y < y_min)
code |= BOTTOM;
else if (y > y_max)
code |= TOP;
return code;
}
void CohenSutherLandAlgo::cohenSutherland()
{
int code1 = generateCode(x1, y1);
int code2 = generateCode(x2, y2);
bool accept = false;
while (true)
{
if ((code1 == 0) && (code2 == 0))
{
accept = true;
break;
}
else if (code1 & code2)
{
break;
}
else
{
int code_out;
double x, y;
if (code1 != 0)
code_out = code1;
else
code_out = 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 = generateCode(x1, y1);
}
else
{
x2 = x;
y2 = y;
code2 = generateCode(x2, y2);
}
}
}
if (accept)
{ std::cout<<endl;
std::cout <<"Line accepted from " <<"("<< x1 << ", "
<< y1 << ")" << " to "<< "(" << x2 << ", " << y2 << ")" <<
std::endl;
setcolor(GREEN);
line(x1+x_max-x_min+50,y1,x2+x_max-x_min+50,y2);
}
else
std::cout << "Line rejected" << std::endl;
}
int main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:turboc3bgi");
CohenSutherLandAlgo c;
c.getCoordinates();
c.getClippingRectangle();
c.cohenSutherland();
getch();
closegraph();
}
c) Code for clip a line intersecting at Two or more points with given window
#include<iostream>
#include<graphics.h>
#include<math.h>
#include<conio.h>
using namespace std;
class CohenSutherLandAlgo
{
private:
double x1,y1,x2,y2;
double x_max,y_max,x_min,y_min;
const int INSIDE = 0;
const int LEFT = 1;
const int RIGHT = 2;
const int BOTTOM = 4;
const int TOP = 8;
public:
CohenSutherLandAlgo()
{
x1 = 0.0;
x2 = 0.0;
y1 = 0.0;
y2 = 0.0;
}
void getCoordinates();
void getClippingRectangle();
int generateCode(double x, double y);
void cohenSutherland();
};
void CohenSutherLandAlgo::getCoordinates()
{
std::cout << "Enter Co-ordinates of P1(X1,Y1) of Line Segment : ";
std::cin >> x1>>y1;
std::cout << "Enter Co-ordinates of P2(X2,Y2) of Line Segment :";
std::cin >> x2>>y2;
std::cout<<endl;
line(x1,y1,x2,y2);
}
void CohenSutherLandAlgo::getClippingRectangle()
{
std::cout << "Enter the Co-ordinates of Interested Rectangle."<<endl;
std::cout << "Enter the X_MAX and Enter the Y_MAX : ";
std::cin >> x_max>>y_max;
std::cout << "Enter the X_MIN and Enter the Y_MIN : ";
std::cin >> x_min>>y_min;
rectangle(x_min,y_min,x_max,y_max);
outtextxy(x_min,y1-20,"Before line Clipping");
setcolor(GREEN);
outtextxy(x_max+50,y1-20,"After line Clipping");
rectangle(x_max+50,y_min,x_max+50+x_max-x_min,y_max);
}
int CohenSutherLandAlgo::generateCode(double x, double y)
{
int code = INSIDE;
if (x < x_min)
code |= LEFT;
else if (x > x_max)
code |= RIGHT;
if (y < y_min)
code |= BOTTOM;
else if (y > y_max)
code |= TOP;
return code;
}
void CohenSutherLandAlgo::cohenSutherland()
{
int code1 = generateCode(x1, y1);
int code2 = generateCode(x2, y2);
bool accept = false;
while (true)
{
if ((code1 == 0) && (code2 == 0))
{
accept = true;
break;
}
else if (code1 & code2)
{
break;
}
else
{
int code_out;
double x, y;
if (code1 != 0)
code_out = code1;
else
code_out = 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 = generateCode(x1, y1);
}
else
{
x2 = x;
y2 = y;
code2 = generateCode(x2, y2);
}
}
}
if (accept)
{ std::cout<<endl;
std::cout <<"Line accepted from " <<"("<< x1 << ", "
<< y1 << ")" << " to "<< "(" << x2 << ", " << y2 << ")" <<
std::endl;
setcolor(GREEN);
line(x1+x_max-x_min+50,y1,x2+x_max-x_min+50,y2);
}
else
std::cout << "Line rejected" << std::endl;
}
int main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:turboc3bgi");
CohenSutherLandAlgo c;
c.getCoordinates();
c.getClippingRectangle();
c.cohenSutherland();
getch();
closegraph();
}
5. Output:
a) Output for display 4-bit region code for end points of a line
b) Output for clip a line intersecting at one point with given window
c) Output for clip a line intersecting at Two or more points with given window
6. Learning outcomes (What I have learnt):
1. I have gathered detail knowledge about how to display 4-bit region code for end
points of a line and check whether line is completely on the screen or off the screen
2. I have gathered detail knowledge about how to clip a line intersecting at one point
with given window using Cohen Sutherland Line Clipping algorithm.
3. I have gathered detail knowledge about how to clip a line intersecting at Two or
more points with given window using Cohen Sutherland Line Clipping algorithm
4. I have learn how to display 4-bit region code for end points of a line and check
whether line is completely on the screen or off the screen
5. I have learn how to clip a line intersecting at one point and two or more point with
given window using Cohen Sutherland Line Clipping algorithm.
6. I get to know about proper logic & algorithm in computation of clip a line
intersecting at one point andTwo or more points with given window using Cohen
Sutherland Line Clipping algorithm
7. Evaluation Grid (To be created as per the SOP and Assessment guidelines by the faculty):
Sr. No. Parameters Marks Obtained Maximum Marks
1.
2.
3.

More Related Content

PPTX
Computer Graphic - Clipping
PPTX
PDF
C++ in 10 Hours.pdf.pdf
PDF
Comparison of Various Line Clipping Algorithm for Improvement
PDF
0.my book draft chap 1
PDF
CD504 CGM_Lab Manual_004e08d3838702ed11fc6d03cc82f7be.pdf
PPTX
Unit2- line clipping.pptx
PDF
Unit-IV Windowing and Clipping.pdf
Computer Graphic - Clipping
C++ in 10 Hours.pdf.pdf
Comparison of Various Line Clipping Algorithm for Improvement
0.my book draft chap 1
CD504 CGM_Lab Manual_004e08d3838702ed11fc6d03cc82f7be.pdf
Unit2- line clipping.pptx
Unit-IV Windowing and Clipping.pdf

Similar to 19BCS2605_Krishna_Kumar_Computer_Graphics_exp_3.1.pdf (20)

PPTX
2D viewing & clipping
PPTX
CHAPTER - 4 for software engineering (1).pptx
PDF
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
DOC
oop Lecture 4
PDF
Computer graphics lab manual
PDF
Mid term sem 2 1415 sol
PDF
raster algorithm.pdf
PPT
Lcdf4 chap 03_p2
PPTX
Study on Fundamentals of Raster Scan Graphics
PPTX
Clipping ( Cohen-Sutherland Algorithm )
PDF
Chainer-Compiler 動かしてみた
DOC
C lab-programs
PPT
217456070-Chapter-3_eletrical engineering
PDF
openMP loop parallelization
PPTX
Digital Logic Design Lecture on Counters and
PPT
Chapter Eight(2)
PPTX
Best C++ Programming Homework Help
PDF
I am working on java programming that converts zipcode to barcode an.pdf
PDF
Computer graphics 2
2D viewing & clipping
CHAPTER - 4 for software engineering (1).pptx
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
oop Lecture 4
Computer graphics lab manual
Mid term sem 2 1415 sol
raster algorithm.pdf
Lcdf4 chap 03_p2
Study on Fundamentals of Raster Scan Graphics
Clipping ( Cohen-Sutherland Algorithm )
Chainer-Compiler 動かしてみた
C lab-programs
217456070-Chapter-3_eletrical engineering
openMP loop parallelization
Digital Logic Design Lecture on Counters and
Chapter Eight(2)
Best C++ Programming Homework Help
I am working on java programming that converts zipcode to barcode an.pdf
Computer graphics 2

Recently uploaded (20)

DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
Sustainable Sites - Green Building Construction
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PDF
composite construction of structures.pdf
PDF
Arduino robotics embedded978-1-4302-3184-4.pdf
PDF
Structs to JSON How Go Powers REST APIs.pdf
DOCX
573137875-Attendance-Management-System-original
PDF
Well-logging-methods_new................
PPT
Project quality management in manufacturing
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
Lecture Notes Electrical Wiring System Components
PDF
Digital Logic Computer Design lecture notes
PPTX
UNIT 4 Total Quality Management .pptx
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Sustainable Sites - Green Building Construction
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Operating System & Kernel Study Guide-1 - converted.pdf
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
composite construction of structures.pdf
Arduino robotics embedded978-1-4302-3184-4.pdf
Structs to JSON How Go Powers REST APIs.pdf
573137875-Attendance-Management-System-original
Well-logging-methods_new................
Project quality management in manufacturing
Foundation to blockchain - A guide to Blockchain Tech
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Lecture Notes Electrical Wiring System Components
Digital Logic Computer Design lecture notes
UNIT 4 Total Quality Management .pptx

19BCS2605_Krishna_Kumar_Computer_Graphics_exp_3.1.pdf

  • 1. Experiment no. – 3.1 Student Name: Krishna Kumar UID: 19BCS2605 Branch: CSE - 11 Section/Group : ‘C’ Semester: 5th Date of Performance: 21 Oct 2021 Subject Name: Computer Graphics Lab Subject Code: CSP - 305 1. Aim/Overview of the practical: a) To display 4-bit region code for end points of a line and check whether line is completely on the screen or off the screen b) To clip a line intersecting at one point with given window using Cohen Sutherland Line Clipping algorithm. c) To clip a line intersecting at Two or more points with given window using Cohen Sutherland Line Clipping algorithm 2. Task to be done: a) To display 4-bit region code for end points of a line b) To clip a line intersecting at one point with given window c) To clip a line intersecting at Two or more points with given window 3. Algorithm : a) algorithms for display 4-bit region code for end points of a line Step 1: Start Step 1.1: taking two point, starting point and end point of line for user Step 1.2: taking two co-ordinate of rectangle, starting co-ordinate and end co- ordinate of rectangle for user Step 1. 4: Assign a region code for two endpoints of given line Step 2 : If both endpoints have a region code 0000 then given line is completely inside and we will keep this line
  • 2. Step 3 : If step 2 fails, perform the logical AND operation for both region codes. Step 3.1 : If the result is not 0000, then given line is completely outside. Step 3.2 : Else line is partially inside. Step 3.2.a : Choose an endpoint of the line that is outside the given rectangle. Step 3.2.b : Find the intersection point of the rectangular boundary (based on region code). Step 3.2.c : Replace endpoint with the intersection point and update the region code. Step 3.2.d : Repeat step 2 until we find a clipped line either trivially accepted or rejected. Step 4 : Repeat step 1 for all lines Step 5: using while for taking point of line again and again form user. Step 6: Displaying window, line and line in side window at a green colour at green colour Step 7: End b) algorithms for clip a line intersecting at one point with given window Step 1: Start Step 1.1: taking two point, starting point and end point of line for user Step 1.2: taking two co-ordinate of rectangle, starting co-ordinate and end co- ordinate of rectangle for user Step 1.3 : Assign a region code for two endpoints of given line Step 2 : If both endpoints have a region code 0000 then given line is completely inside and we will keep this line Step 3 : If step 2 fails, perform the logical AND operation for both region codes. Step 3.1 : If the result is not 0000, then given line is completely outside. Step 3.2 : Else line is partially inside. Step 3.2.a : Choose an endpoint of the line that is outside the given rectangle. Step 3.2.b : Find the intersection point of the rectangular boundary (based on region code). Step 3.2.c : Replace endpoint with the intersection point and update the region code. Step 3.2.d : Repeat step 2 until we find a clipped line either trivially accepted or rejected.
  • 3. Step 4 : Repeat step 1 for all lines Step 5: Displaying window and line before line clipping and after line clipping at green colour Step 6: End c) algorithms for clip a line intersecting at Two or more points with given window Step 1: Start Step 1.1: taking two point, starting point and end point of line for user Step 1.2: taking two co-ordinate of rectangle, starting co-ordinate and end co- ordinate of rectangle for user Step 1.3 : Assign a region code for two endpoints of given line Step 2 : If both endpoints have a region code 0000 then given line is completely inside and we will keep this line Step 3 : If step 2 fails, perform the logical AND operation for both region codes. Step 3.1 : If the result is not 0000, then given line is completely outside. Step 3.2 : Else line is partially inside. Step 3.2.a : Choose an endpoint of the line that is outside the given rectangle. Step 3.2.b : Find the intersection point of the rectangular boundary (based on region code). Step 3.2.c : Replace endpoint with the intersection point and update the region code. Step 3.2.d : Repeat step 2 until we find a clipped line either trivially accepted or rejected. Step 4 : Repeat step 1 for all lines Step 5: Displaying window and line before line clipping and after line clipping Step 6: End 4. Programming Code: a) Code for display 4-bit region code for end points of a line #include<iostream> #include<graphics.h> #include<math.h>
  • 4. #include<conio.h> using namespace std; class CohenSutherLandAlgo { private: double x1,y1,x2,y2; double x_max,y_max,x_min,y_min; const int INSIDE = 0; const int LEFT = 1; const int RIGHT = 2; const int BOTTOM = 4; const int TOP = 8; public: CohenSutherLandAlgo() { x1 = 0.0; x2 = 0.0; y1 = 0.0; y2 = 0.0; } void getCoordinates(); void getClippingRectangle(); int generateCode(double x, double y); void cohenSutherland(); }; void CohenSutherLandAlgo::getCoordinates() { std::cout << "Enter Co-ordinates of P1(X1,Y1) of Line Segment : "; std::cin >> x1>>y1; std::cout << "Enter Co-ordinates of P2(X2,Y2) of Line Segment :"; std::cin >> x2>>y2; std::cout<<endl; setcolor(WHITE); line(x1,y1,x2,y2); } void CohenSutherLandAlgo::getClippingRectangle() { std::cout << "Enter the Co-ordinates of Interested Rectangle."<<endl; std::cout << "Enter the X_MAX and Enter the Y_MAX : "; std::cin >> x_max>>y_max; std::cout << "Enter the X_MIN and Enter the Y_MIN : ";
  • 5. std::cin >> x_min>>y_min; rectangle(x_min,y_min,x_max,y_max); } int CohenSutherLandAlgo::generateCode(double x, double y) { int code = INSIDE; if (x < x_min) code |= LEFT; else if (x > x_max) code |= RIGHT; if (y < y_min) code |= BOTTOM; else if (y > y_max) code |= TOP; return code; } void CohenSutherLandAlgo::cohenSutherland() { int code1 = generateCode(x1, y1); int code2 = generateCode(x2, y2); bool accept = false; while (true) { if ((code1 == 0) && (code2 == 0)) { accept = true; break; } else if (code1 & code2) { break; } else { int code_out; double x, y; if (code1 != 0) code_out = code1; else code_out = code2; if (code_out & TOP)
  • 6. { 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 = generateCode(x1, y1); } else { x2 = x; y2 = y; code2 = generateCode(x2, y2); } } } if (accept) { std::cout<<endl; std::cout <<"Line accepted from " <<"("<< x1 << ", " << y1 << ")" << " to "<< "(" << x2 << ", " << y2 << ")" << std::endl; setcolor(GREEN); line(x1,y1,x2,y2); }
  • 7. else std::cout << "Line rejected" << std::endl; } int main() { int gd=DETECT,gm; initgraph(&gd,&gm,"c:turboc3bgi"); int x; outtextxy(5,5,"White color that part of line segment is not accept "); setcolor(GREEN); outtextxy(5,20,"Green color that part of line segment is accept "); setcolor(WHITE); CohenSutherLandAlgo c; c.getClippingRectangle(); do{ c.getCoordinates(); c.cohenSutherland(); std::cout<<endl; cout<<"Please press 1 (one) for continue :"; cin>>x; } while(x==1); getch(); closegraph(); } b) Code for clip a line intersecting at one point with given window #include<iostream> #include<graphics.h> #include<math.h> #include<conio.h> using namespace std; class CohenSutherLandAlgo { private:
  • 8. double x1,y1,x2,y2; double x_max,y_max,x_min,y_min; const int INSIDE = 0; const int LEFT = 1; const int RIGHT = 2; const int BOTTOM = 4; const int TOP = 8; public: CohenSutherLandAlgo() { x1 = 0.0; x2 = 0.0; y1 = 0.0; y2 = 0.0; } void getCoordinates(); void getClippingRectangle(); int generateCode(double x, double y); void cohenSutherland(); }; void CohenSutherLandAlgo::getCoordinates() { std::cout << "Enter Co-ordinates of P1(X1,Y1) of Line Segment : "; std::cin >> x1>>y1; std::cout << "Enter Co-ordinates of P2(X2,Y2) of Line Segment :"; std::cin >> x2>>y2; std::cout<<endl; line(x1,y1,x2,y2); } void CohenSutherLandAlgo::getClippingRectangle() { std::cout << "Enter the Co-ordinates of Interested Rectangle."<<endl; std::cout << "Enter the X_MAX and Enter the Y_MAX : ";
  • 9. std::cin >> x_max>>y_max; std::cout << "Enter the X_MIN and Enter the Y_MIN : "; std::cin >> x_min>>y_min; rectangle(x_min,y_min,x_max,y_max); outtextxy(x_min,y1-20,"Before line Clipping"); setcolor(GREEN); outtextxy(x_max+50,y1-20,"After line Clipping"); rectangle(x_max+50,y_min,x_max+50+x_max-x_min,y_max); } int CohenSutherLandAlgo::generateCode(double x, double y) { int code = INSIDE; if (x < x_min) code |= LEFT; else if (x > x_max) code |= RIGHT; if (y < y_min) code |= BOTTOM; else if (y > y_max) code |= TOP; return code; } void CohenSutherLandAlgo::cohenSutherland() { int code1 = generateCode(x1, y1); int code2 = generateCode(x2, y2); bool accept = false; while (true) { if ((code1 == 0) && (code2 == 0)) { accept = true; break; } else if (code1 & code2) {
  • 10. break; } else { int code_out; double x, y; if (code1 != 0) code_out = code1; else code_out = 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 = generateCode(x1, y1); }
  • 11. else { x2 = x; y2 = y; code2 = generateCode(x2, y2); } } } if (accept) { std::cout<<endl; std::cout <<"Line accepted from " <<"("<< x1 << ", " << y1 << ")" << " to "<< "(" << x2 << ", " << y2 << ")" << std::endl; setcolor(GREEN); line(x1+x_max-x_min+50,y1,x2+x_max-x_min+50,y2); } else std::cout << "Line rejected" << std::endl; } int main() { int gd=DETECT,gm; initgraph(&gd,&gm,"c:turboc3bgi"); CohenSutherLandAlgo c; c.getCoordinates(); c.getClippingRectangle(); c.cohenSutherland(); getch(); closegraph(); } c) Code for clip a line intersecting at Two or more points with given window #include<iostream> #include<graphics.h>
  • 12. #include<math.h> #include<conio.h> using namespace std; class CohenSutherLandAlgo { private: double x1,y1,x2,y2; double x_max,y_max,x_min,y_min; const int INSIDE = 0; const int LEFT = 1; const int RIGHT = 2; const int BOTTOM = 4; const int TOP = 8; public: CohenSutherLandAlgo() { x1 = 0.0; x2 = 0.0; y1 = 0.0; y2 = 0.0; } void getCoordinates(); void getClippingRectangle(); int generateCode(double x, double y); void cohenSutherland(); }; void CohenSutherLandAlgo::getCoordinates() { std::cout << "Enter Co-ordinates of P1(X1,Y1) of Line Segment : "; std::cin >> x1>>y1; std::cout << "Enter Co-ordinates of P2(X2,Y2) of Line Segment :"; std::cin >> x2>>y2; std::cout<<endl; line(x1,y1,x2,y2);
  • 13. } void CohenSutherLandAlgo::getClippingRectangle() { std::cout << "Enter the Co-ordinates of Interested Rectangle."<<endl; std::cout << "Enter the X_MAX and Enter the Y_MAX : "; std::cin >> x_max>>y_max; std::cout << "Enter the X_MIN and Enter the Y_MIN : "; std::cin >> x_min>>y_min; rectangle(x_min,y_min,x_max,y_max); outtextxy(x_min,y1-20,"Before line Clipping"); setcolor(GREEN); outtextxy(x_max+50,y1-20,"After line Clipping"); rectangle(x_max+50,y_min,x_max+50+x_max-x_min,y_max); } int CohenSutherLandAlgo::generateCode(double x, double y) { int code = INSIDE; if (x < x_min) code |= LEFT; else if (x > x_max) code |= RIGHT; if (y < y_min) code |= BOTTOM; else if (y > y_max) code |= TOP; return code; } void CohenSutherLandAlgo::cohenSutherland() { int code1 = generateCode(x1, y1); int code2 = generateCode(x2, y2); bool accept = false; while (true)
  • 14. { if ((code1 == 0) && (code2 == 0)) { accept = true; break; } else if (code1 & code2) { break; } else { int code_out; double x, y; if (code1 != 0) code_out = code1; else code_out = 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);
  • 15. x = x_min; } if (code_out == code1) { x1 = x; y1 = y; code1 = generateCode(x1, y1); } else { x2 = x; y2 = y; code2 = generateCode(x2, y2); } } } if (accept) { std::cout<<endl; std::cout <<"Line accepted from " <<"("<< x1 << ", " << y1 << ")" << " to "<< "(" << x2 << ", " << y2 << ")" << std::endl; setcolor(GREEN); line(x1+x_max-x_min+50,y1,x2+x_max-x_min+50,y2); } else std::cout << "Line rejected" << std::endl; } int main() { int gd=DETECT,gm; initgraph(&gd,&gm,"c:turboc3bgi"); CohenSutherLandAlgo c; c.getCoordinates(); c.getClippingRectangle(); c.cohenSutherland();
  • 16. getch(); closegraph(); } 5. Output: a) Output for display 4-bit region code for end points of a line
  • 17. b) Output for clip a line intersecting at one point with given window c) Output for clip a line intersecting at Two or more points with given window
  • 18. 6. Learning outcomes (What I have learnt): 1. I have gathered detail knowledge about how to display 4-bit region code for end points of a line and check whether line is completely on the screen or off the screen 2. I have gathered detail knowledge about how to clip a line intersecting at one point with given window using Cohen Sutherland Line Clipping algorithm. 3. I have gathered detail knowledge about how to clip a line intersecting at Two or more points with given window using Cohen Sutherland Line Clipping algorithm 4. I have learn how to display 4-bit region code for end points of a line and check whether line is completely on the screen or off the screen 5. I have learn how to clip a line intersecting at one point and two or more point with given window using Cohen Sutherland Line Clipping algorithm. 6. I get to know about proper logic & algorithm in computation of clip a line intersecting at one point andTwo or more points with given window using Cohen Sutherland Line Clipping algorithm 7. Evaluation Grid (To be created as per the SOP and Assessment guidelines by the faculty): Sr. No. Parameters Marks Obtained Maximum Marks 1. 2. 3.