forked from mandli/swc-2013-07-washington-testing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
close_line.py
69 lines (53 loc) · 1.67 KB
/
close_line.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
import numpy as np
from scipy.optimize import fmin
#
# Attempt 1
#
def point_on_line1(x, p1, p2):
y = p1[1] + (x - p1[0])*(p2[1] - p1[1]) / (p2[0] - p1[0])
return np.array([x, y])
def dist_from_line1(x, pdata, p1, p2):
pline = point_on_line1(x, p1, p2)
return np.sqrt(np.sum((pline - pdata)**2))
def closest_data_to_line1(data, p1, p2):
dists = np.empty(len(data), dtype=float)
for i, pdata in enumerate(data):
x = fmin(dist_from_line1, p1[0], (pdata, p1, p2), disp=False)[0]
dists[i] = dist_from_line1(x, pdata, p1, p2)
imin = np.argmin(dists)
return imin, data[imin]
#
# Attempt 2
#
def dist_from_line2(pdata, p1, p2):
a = np.sqrt(np.sum((p1 - pdata)**2))
b = np.sqrt(np.sum((p2 - pdata)**2))
c = np.sqrt(np.sum((p2 - p1)**2))
h = a * np.sqrt(1.0 - ((a**2 + c**2 - b**2) / (2.0 * a * c))**2)
return h
def closest_data_to_line2(data, p1, p2):
dists = np.empty(len(data), dtype=float)
for i, pdata in enumerate(data):
dists[i] = dist_from_line2(pdata, p1, p2)
imin = np.argmin(dists)
return imin, data[imin]
#
# Attempt 3
#
def perimeter3(pdata, p1, p2):
a = np.sqrt(np.sum((p1 - pdata)**2))
b = np.sqrt(np.sum((p2 - pdata)**2))
c = np.sqrt(np.sum((p2 - p1)**2))
return (a + b + c)
def closest_data_to_line3(data, p1, p2):
peris = np.empty(len(data), dtype=float)
for i, pdata in enumerate(data):
peris[i] = perimeter3(pdata, p1, p2)
imin = np.argmin(peris)
return imin, data[imin]
#
# Attempt 4
#
def closest_data_to_line4(data, p1, p2):
return data[np.argmin(np.sqrt(np.sum((p1 - data)**2, axis=1)) + \
np.sqrt(np.sum((p2 - data)**2, axis=1)))]