Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Parth1906
GitHub Repository: Parth1906/SPPU-2019-Pattern-SE-COMP-Computer-Graphics-Practicals
Path: blob/main/7. Cohen Southerland line clipping algorithm.cpp
724 views
1
/*
2
7. Write C++ program to implement Cohen Southerland line clipping algorithm.
3
*/
4
5
6
#include<iostream.h>
7
#include<conio.h>
8
#include<graphics.h>
9
#include<math.h>
10
void Window()
11
{
12
line (200,200,350,200);
13
line(350,200,350,350);
14
line(200,200,200,350);
15
line(200,350,350,350);
16
}
17
void Code(char c[4],float x,float y)
18
{ c[0]=(x<200)?'1':'0';
19
c[1]=(x>350)?'1':'0';
20
c[2]=(y<200)?'1':'0';
21
c[3]=(y>350)?'1':'0';
22
}
23
void Clipping (char c[],char d[],float &x,float &y,float m)
24
{
25
int flag=1,i=0;
26
for (i=0;i<4;i++)
27
{
28
if(c[i]!='0' && d[i]!='0')
29
{
30
flag=0;
31
break;
32
}
33
if(flag)
34
{
35
if(c[0]!='0')
36
{
37
y=m*(200-x)+y;
38
x=200;
39
}
40
else if(c[1]!='0')
41
{
42
y=m*(350-x)+y;
43
x=350;
44
}
45
else if(c[2]!='0')
46
{
47
x=((200-y)/m)+x;
48
y=200;
49
}
50
else if(c[3]!='0')
51
{
52
x=((350-y)/m)+x;
53
y=350;
54
}
55
}
56
if (flag==0)
57
cout<<"Line lying outside";
58
}
59
}
60
void main()
61
{
62
int gdriver = DETECT, gmode, errorcode;
63
float x1,y1,x2,y2;
64
float m;
65
char c[4],d[4];
66
clrscr();
67
initgraph(&gdriver, &gmode, "//Turboc3//bgi");
68
cout<<"Enter coordinates";
69
cin>>x1>>y1>>x2>>y2;
70
cout<<"Before clipping";
71
Window();
72
line(x1,y1,x2,y2);
73
getch();
74
cleardevice();
75
m=float((y2-y1)/(x2-x1));
76
Code(c,x1,y1);
77
Code(d,x2,y2) ;
78
Clipping(c,d,x1,y1,m);
79
Clipping(d,c,x2,y2,m);
80
cout<<"After Clipping";
81
Window();
82
line(x1,y1,x2,y2);
83
getch();
84
closegraph();
85
}
86
87