/
point_set.py
135 lines (100 loc) · 3.07 KB
/
point_set.py
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
import point_pair
import mergesort
point1=(0,0)
point2=(0,0)
d = point_pair.point_distance(point1, point2)
l=100
def xsort(p):
mergesort.q = 0
mergesort.mergeSort(p)
return p
def ysort(p):
mergesort.q = 1
mergesort.mergeSort(p)
return p
def brute_force(r):
if len(r)==2:
point1= r[0]
point2= r[1]
return [point1,point2]
else:
x= point_pair.pair_distance([r[0],r[1]])
y= point_pair.pair_distance([r[0],r[2]])
z= point_pair.pair_distance([r[1],r[2]])
if x<=y:
if x<=z:
point1= r[0]
point2= r[1]
return [point1,point2]
else:
point1= r[1]
point2= r[2]
return [point1,point2]
else:
if y<=z:
point1= r[0]
point2= r[2]
return [point1,point2]
else:
point1= r[1]
point2= r[2]
return [point1,point2]
def closest_pair(p):
s=[]
w=[]
for i in p:
s.append(i)
w.append(i)
x=xsort(s)
y=ysort(w)
return cp (x)
def cp ( x):
if (len(x)<=3):
return brute_force(x)
else:
xL = xsort(x [:len(x)//2])
xR = xsort (x [len(x)//2:])
div = (x[len(x)//2-1][0]+ x[len(x)//2][0])/2
t= []
for i in x:
t.append(i)
y = ysort(t)
a = cp (xL)
f = point_pair.pair_distance(a)
b = cp (xR)
g = point_pair.pair_distance(b)
if point_pair.pair_distance(a)>=point_pair.pair_distance(b):
minlength = point_pair.pair_distance(b)
else:
minlength = point_pair.pair_distance(a)
return across_line (a,b, div,y, minlength)
def across_line (a,b, div,y, minlength):
f = point_pair.pair_distance(a)
g = point_pair.pair_distance(b)
if f <= g:
c = a
u = div+f
v = div -f
else:
c = b
u = div+g
v = div -g
for i in range (len(y)):
yprime = []
if y[i][0]>=v and y[i][0]<=u:
yprime.append(y[i])
for j in yprime:
if f <=g:
m = j[1]-f
n = j[1]+f
else:
m = j[1]-g
n = j[1]+g
for k in yprime:
if k != j:
if k[1]>=m and k[1]<n:
e = point_pair.point_distance(j,k)
if e < f and e<g and e<minlength:
minlength = e
c = (j,k)
return c