-
Notifications
You must be signed in to change notification settings - Fork 765
/
Copy pathquadilateral formula
144 lines (118 loc) · 3.15 KB
/
quadilateral formula
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
139
140
141
142
143
144
// C++ implementation of above approach
#include <iostream>
using namespace std;
struct Point // points
{
int x;
int y;
};
// determines the orientation of points
int orientation(Point p, Point q, Point r)
{
int val = (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);
if (val == 0)
return 0;
return (val > 0) ? 1 : 2;
}
// check whether the distinct line segments intersect
bool doIntersect(Point p1, Point q1, Point p2, Point q2)
{
int o1 = orientation(p1, q1, p2);
int o2 = orientation(p1, q1, q2);
int o3 = orientation(p2, q2, p1);
int o4 = orientation(p2, q2, q1);
if (o1 != o2 && o3 != o4)
return true;
return false;
}
// check if points overlap(similar)
bool similar(Point p1, Point p2)
{
// it is same, we are returning false because
// quadrilateral is not possible in this case
if (p1.x == p2.x && p1.y == p2.y)
return false;
// it is not same, So there is a
// possibility of a quadrilateral
return true;
}
// check for collinearity
bool collinear(Point p1, Point p2, Point p3)
{
int x1 = p1.x, y1 = p1.y;
int x2 = p2.x, y2 = p2.y;
int x3 = p3.x, y3 = p3.y;
// it is collinear, we are returning false
// because quadrilateral is not possible in this case
if ((y3 - y2) * (x2 - x1) == (y2 - y1) * (x3 - x2))
return false;
// it is not collinear, So there
// is a possibility of a quadrilateral
else
return true;
}
int no_of_quads(Point p1, Point p2, Point p3, Point p4)
{
// ** Checking for cases where no quadrilateral = 0 **
// check if any of the points are same
bool same = true;
same = same & similar(p1, p2);
same = same & similar(p1, p3);
same = same & similar(p1, p4);
same = same & similar(p2, p3);
same = same & similar(p2, p4);
same = same & similar(p3, p4);
// similar points exist
if (same == false)
return 0;
// check for collinearity
bool coll = true;
coll = coll & collinear(p1, p2, p3);
coll = coll & collinear(p1, p2, p4);
coll = coll & collinear(p1, p3, p4);
coll = coll & collinear(p2, p3, p4);
// points are collinear
if (coll == false)
return 0;
//** Checking for cases where no of quadrilaterals= 1 or 3 **
int check = 0;
if (doIntersect(p1, p2, p3, p4))
check = 1;
if (doIntersect(p1, p3, p2, p4))
check = 1;
if (doIntersect(p1, p2, p4, p3))
check = 1;
if (check == 0)
return 3;
return 1;
}
// Driver code
int main()
{
struct Point p1, p2, p3, p4;
// A =(0, 9), B = (-1, 0), C = (5, -1), D=(5, 9)
p1.x = 0, p1.y = 9;
p2.x = -1, p2.y = 0;
p3.x = 5, p3.y = -1;
p4.x = 5, p4.y = 9;
cout << no_of_quads(p1, p2, p3, p4) << endl;
// A=(0, 9), B=(-1, 0), C=(5, -1), D=(0, 3)
p1.x = 0, p1.y = 9;
p2.x = -1, p2.y = 0;
p3.x = 5, p3.y = -1;
p4.x = 0, p4.y = 3;
cout << no_of_quads(p1, p2, p3, p4) << endl;
// A=(0, 9), B=(0, 10), C=(0, 11), D=(0, 12)
p1.x = 0, p1.y = 9;
p2.x = 0, p2.y = 10;
p3.x = 0, p3.y = 11;
p4.x = 0, p4.y = 12;
cout << no_of_quads(p1, p2, p3, p4) << endl;
// A=(0, 9), B=(0, 9), C=(5, -1), D=(0, 3)
p1.x = 0, p1.y = 9;
p2.x = 0, p2.y = 9;
p3.x = 5, p3.y = -1;
p4.x = 0, p4.y = 3;
cout << no_of_quads(p1, p2, p3, p4) << endl;
return 0;
}