-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_api.py
More file actions
103 lines (79 loc) · 3.4 KB
/
basic_api.py
File metadata and controls
103 lines (79 loc) · 3.4 KB
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
import scipy
def generate_parametrized_line(origin, angle, distance):
def parametrized_line(t):
x = origin['x'] + distance*scipy.cos(angle)*t
y = origin['y'] + distance*scipy.sin(angle)*t
return {'x': x, 'y': y}
return parametrized_line
def generate_parametrized_arc(center, angles, radius):
angle_delta = angles['stop'] - angles['start']
def parametrized_arc(t):
effective_angle = angle_delta*t + angles['start']
x = center['x'] + radius*scipy.cos(effective_angle)
y = center['y'] + radius*scipy.sin(effective_angle)
return {'x': x, 'y': y}
return parametrized_arc
#Given a point on the arc, the angle of the point relative to
#the center of the arc, and the radius of the arc,
#find the center of the arc.
def get_arc_center(point, radius, angle):
center_x = point['x'] - radius*scipy.cos(angle)
center_y = point['y'] - radius*scipy.sin(angle)
return {'x': center_x, 'y': center_y}
#The radial angles oriented away from the center of the arc is
#offset by 90 degrees from the direction of the tangent and
#is dependent on the direction of the arc.
def get_radial_angle_from_arc_tangent(tangent_angle, clockwise=False):
if clockwise:
angle = tangent_angle + scipy.pi/2.
else:
angle = tangent_angle - scipy.pi/2.
return angle
#The inverse function of get_radial_angle_from_arc_tangent
def get_arc_tangent_angle_from_radial_angle(radial_angle, clockwise=False):
if clockwise:
angle = radial_angle - scipy.pi/2.
else:
angle = radial_angle + scipy.pi/2.
return angle
def normalize_angle(angle):
return scipy.fmod(angle, scipy.pi*2.)
def determinant(point_a, point_b, point_c):
return ((point_b['x'] - point_a['x'])*(point_c['y'] - point_a['y'])
- (point_b['y'] - point_a['y'])*(point_c['x'] - point_a['x']))
#Does the line that passes through point_a and point_b intersect with
#the line that passes through point_c and point_d?
def is_intersection(point_a, point_b, point_c, point_d):
det_abc = determinant(point_a, point_b, point_c)
det_abd = determinant(point_a, point_b, point_d)
if det_abc*det_abd >= 0.:
return False
det_cda = determinant(point_c, point_d, point_a)
det_cdb = determinant(point_c, point_d, point_b)
if det_cda*det_cdb >= 0.:
return False
return True
def get_consecutive_points_from_sample(sample):
if len(sample) < 2:
raise ValueError
for i in range(len(sample) - 1):
yield sample[i], sample[i + 1]
def is_sample_acceptable(samples, candidate):
if len(samples) < 2:
return True
for point_c, point_d in get_consecutive_points_from_sample(candidate):
for sample in samples[:-1]:
for point_a, point_b in get_consecutive_points_from_sample(sample):
if is_intersection(point_a, point_b,
point_c, point_d):
return False
return True
def sampler(parametrized_function, start_t, stop_t, point_count):
return list(map(lambda t: parametrized_function(t),
scipy.linspace(start_t, stop_t, point_count)))
def write_samples_to_file(samples, filename):
with open(filename, 'w') as file_handle:
for sample in samples:
for point in sample:
file_handle.write('{x} {y}\n'.format(x=point['x'],
y=point['y']))