-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy path7. Cohen Southerland line clipping algorithm.cpp
More file actions
85 lines (83 loc) · 1.33 KB
/
7. Cohen Southerland line clipping algorithm.cpp
File metadata and controls
85 lines (83 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
7. Write C++ program to implement Cohen Southerland line clipping algorithm.
*/
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void Window()
{
line (200,200,350,200);
line(350,200,350,350);
line(200,200,200,350);
line(200,350,350,350);
}
void Code(char c[4],float x,float y)
{ c[0]=(x<200)?'1':'0';
c[1]=(x>350)?'1':'0';
c[2]=(y<200)?'1':'0';
c[3]=(y>350)?'1':'0';
}
void Clipping (char c[],char d[],float &x,float &y,float m)
{
int flag=1,i=0;
for (i=0;i<4;i++)
{
if(c[i]!='0' && d[i]!='0')
{
flag=0;
break;
}
if(flag)
{
if(c[0]!='0')
{
y=m*(200-x)+y;
x=200;
}
else if(c[1]!='0')
{
y=m*(350-x)+y;
x=350;
}
else if(c[2]!='0')
{
x=((200-y)/m)+x;
y=200;
}
else if(c[3]!='0')
{
x=((350-y)/m)+x;
y=350;
}
}
if (flag==0)
cout<<"Line lying outside";
}
}
void main()
{
int gdriver = DETECT, gmode, errorcode;
float x1,y1,x2,y2;
float m;
char c[4],d[4];
clrscr();
initgraph(&gdriver, &gmode, "//Turboc3//bgi");
cout<<"Enter coordinates";
cin>>x1>>y1>>x2>>y2;
cout<<"Before clipping";
Window();
line(x1,y1,x2,y2);
getch();
cleardevice();
m=float((y2-y1)/(x2-x1));
Code(c,x1,y1);
Code(d,x2,y2) ;
Clipping(c,d,x1,y1,m);
Clipping(d,c,x2,y2,m);
cout<<"After Clipping";
Window();
line(x1,y1,x2,y2);
getch();
closegraph();
}