Repository navigation
Expand file tree
/
Copy pathliangbarsky.cpp
More file actions
138 lines (113 loc) · 3.01 KB
/
Copy pathliangbarsky.cpp
File metadata and controls
138 lines (113 loc) · 3.01 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Liang Barsky Line Clipping Algorithm
#include<iostream>
#include<graphics.h>
#include<math.h>
using namespace std;
// this function gives the maximum
float maxi(float arr[],int n)
{
float m = 0;
for(int i = 0;i<n;i++)
{
if(m<arr[i])
{
m = arr[i];
}
}
return m;
}
// this function gives the minimum
float mini(float arr[],int n)
{
float m = 1;
for(int i = 0;i<n;i++)
{
if(m>arr[i])
{
m = arr[i];
}
}
return m;
}
void liang_barsky_clipper(float xmin,float ymin, float xmax, float ymax, float x1,float y1, float x2, float y2)
{
// defining variables
float p1 = -(x2-x1);
float p2 = -p1;
float p3 = -(y2-y1);
float p4 = -p3;
float q1 = x1-xmin;
float q2 = xmax - x1;
float q3 = y1 - ymin;
float q4 = ymax - y1;
float posarr[5], negarr[5];
int posind = 1,negind = 1;
posarr[0] = 1;
negarr[0] = 0;
rectangle(xmin,467 - ymin,xmax,467 - ymax); // drawing the clipping window!
if(p1==0 || p3 ==0)
{
outtextxy(80,80,"Line is Parallel to clipping window!");
return;
}
if(p1!=0)
{
float r1 = q1/p1;
float r2 = q2/p2;
if(p1<0)
{
negarr[negind++] = r1;
posarr[posind++] = r2; // for negative p1, add it to negative array and add p2 to positive array
}
else
{
negarr[negind++] = r2;
posarr[posind++] = r1;
}
}
if(p3!=0)
{
float r3 = q3/p3;
float r4 = q4/r4;
if(p3<0)
{
negarr[negind++] = r3;
posarr[posind++] = r4;
}
else
{
negarr[negind++] = r4;
posarr[posind++] = r3;
}
}
float xn1,yn1,xn2,yn2;
float rn1,rn2;
rn1 = maxi(negarr,negind); // maximum of negative array
rn2 = mini(posarr,posind); // minimum of positive array
xn1 = x1 + p2*rn1;
yn1 = y1 + p4*rn1; // computing new points
xn2 = x1 + p2*rn2;
yn2 = y1 + p4*rn2;
setcolor(CYAN);
line(xn1,467 - yn1,xn2,467 - yn2); // the drawing the new line
setlinestyle(1,1,0);
line(x1,467 - y1,xn1,467 - yn1);
line(x2,467 - y2,xn2,467 - yn2);
}
int main()
{
cout<<"\nLIANG-BARSKY-LINE-CLIPPING";
cout<<"\nThe System Window Outlay is:(0,0) at bottom left and (631,467) at top right";
cout<<"\nEnter the co- ordinates of the window(wxmin,wxmax,wymin,wymax):";
float xmin,xmax,ymin,ymax;
cin>>xmin>>ymin>>xmax>>ymax;
cout<<"\nEnter the End Points of the Line (x1,y1) and (x2,y2):";
float x1, y1, x2, y2;
cin>>x1>>y1>>x2>>y2;
int gd = DETECT, gm;
// using the winbgim library for C++ , initializing the graphics mode
initgraph(&gd,&gm,"");
liang_barsky_clipper(xmin,ymin,xmax,ymax,x1,y1,x2,y2);
getch();
closegraph();
}