-
Notifications
You must be signed in to change notification settings - Fork 1
/
minimum_enclosing_circle.cpp
153 lines (143 loc) · 3.56 KB
/
minimum_enclosing_circle.cpp
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
145
146
147
148
149
150
151
152
153
#include <LEDA/geo/point.h>
#include <LEDA/geo/random_point.h>
#include <vector>
#include <LEDA/geo/circle.h>
#include <LEDA/core/array.h>
#include <LEDA/geo/segment.h>
#include <assert.h>
#include <math.h>
#include <fstream>
#include <climits>
#if defined(LEDA_STD_IO_HEADERS)
using std::cout;
using std::cin;
using std::flush;
using std::endl;
#endif
using namespace leda;
bool is_acute(point a, point b, point c)
{
float angle;
segment s1, s2, s3;
s1 = segment(a, c);
s2 = segment(a, b);
angle = s1.angle(s2);
//cout << angle*180/LEDA_PI<< endl;
if (angle >= LEDA_PI/2 && angle <= LEDA_PI * 3/2)
return false;
s1 = segment(b, a);
s2 = segment(b, c);
angle = s1.angle(s2);
//cout << angle*180/LEDA_PI<< endl;
//if (angle >= LEDA_PI/2)
if (angle >= LEDA_PI/2 && angle <= LEDA_PI * 3/2)
return false;
s1 = segment(c, a);
s2 = segment(c, b);
angle = s1.angle(s2);
//cout << angle*180/LEDA_PI<< endl;
//if (angle >= LEDA_PI/2)
if (angle >= LEDA_PI/2 && angle <= LEDA_PI * 3/2)
return false;
return true;
}
circle dia_circle(point a, point b)
{
point centre = midpoint(a,b);
circle C(centre, a);
return C;
}
circle min_enclose2(array <point> &L, point def_pt1)
{
// last element of L1 is the difining point
point def_pt2 = L[L.high()];
//L.permute(); // not required
//cout << L << endl;
circle C;
C = dia_circle(def_pt1, def_pt2);
for (int i = 0; i < L.high(); i++)
{
point new_p = L[i];
if (C.outside(new_p))
{
if (is_acute(def_pt1, def_pt2, new_p))
C = circle(def_pt1, def_pt2, new_p);
}
}
return C;
}
circle min_enclose1(array <point> &L)
{
// last element of L1 is the difining point
point def_pt = L[L.high()];
//L.permute(0, L.high()-1); //TODO
//cout << L << endl;
circle C;
point pt1 = L[0];
C = dia_circle(def_pt, pt1);
// last elemnt to be excluded
for (int i = 1; i < L.high(); i++)
{
point new_p = L[i];
if (C.outside(new_p))
{
array <point> al2(i+1);
for (int j = 0; j <= i; j++)
al2[j] = L[j];
C = min_enclose2(al2, def_pt);
}
}
return C;
}
int main()
{
list <point> L;
int n;
cout << "Enter Number of Points: ";
cin >> n;
int sz;
//cin >> sz;
sz = 10000;
// Random points in sz * sz square
random_points_in_square(n,sz, L);
array <point> al(n);
list_item it = L.first();
int cnt = 0;
for (it = L.first(); it != NULL; it = L.succ(it))
{
al[cnt] = L[it];
cnt++;
}
std::ofstream myfile;
myfile.open("points.txt");
for (int i = 0; i < L.length(); i++)
{
point p = al[i];
myfile << p.xcoord() << " " << p.ycoord() << endl;
}
myfile.close();
al.permute();//TODO
point p1 = al[0];
point p2 = al[1];
circle C = dia_circle(p1, p2);
//cout << C.center() << endl;
int outside_pts = 0;
for (int i = 2; i < n; i++)
{
if (i % 10000 == 0)
{
cout << "Completed: " << i << " Points Outside: " << outside_pts << endl;
}
point new_p = al[i];
if (C.outside(new_p))
{
outside_pts++;
array <point> al2(i+1);
for (int j = 0; j <= i; j++)
al2[j] = al[j];
C = min_enclose1(al2);
}
}
myfile.open("circle.txt");
myfile << C.center().xcoord() << " " << C.center().ycoord() <<" " << C.radius();
}