curves similarity

 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
#!/usr/bin/env python
import math
# **2 is to avoid math.sqrt()
THRESHOLD = 15**2
MAX_TRIES = 5
def distance(p1, p2):
return (p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2
def calculate(base_points, check_points):
starting_point, mins = 0, []
for base in base_points:
success, tries, dist_min = False, 0, THRESHOLD
for i, check in enumerate(check_points[starting_point:]):
dist = distance(check, base)
if dist > THRESHOLD:
tries += 1
if tries >= MAX_TRIES:
break
else:
success = True
if dist < dist_min:
dist_min = dist
if tries:
tries = 0
starting_point += i
mins.append(dist_min)
if not success:
return 0
return 1 - (float(sum(mins)) / len(mins)) / THRESHOLD