-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathspline_util.py
More file actions
executable file
·147 lines (121 loc) · 3.93 KB
/
spline_util.py
File metadata and controls
executable file
·147 lines (121 loc) · 3.93 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
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
#!/usr/bin/env python3
#encoding=utf-8
# distance between two points
from math import hypot
def slide_percent(x1,y1,x2,y2,x3,y3):
percent = -1
distance_offset=200.0
previous_x,previous_y=two_point_extend(x1,y1,x2,y2,-1 * distance_offset)
next_x,next_y=two_point_extend(x3,y3,x2,y2,-1 * distance_offset)
d3 = get_distance(previous_x,previous_y,next_x,next_y)
if d3 > 0:
percent = d3/distance_offset
if percent > 2.0:
percent = 2.0
return percent
# PS: 呼叫函數前,請先確定 distance_offset 不為 0,因為無法判斷要放在 from 還是 end.
def two_point_extend(x1,y1,x2,y2,distance_offset):
distance = get_distance(x1,y1,x2,y2)
distance_percent = 1
if distance_offset != 0 and distance != 0:
distance_percent = (distance_offset / distance)
x_offset = int((x2-x1) * distance_percent)
y_offset = int((y2-y1) * distance_percent)
# 斜線上的「內縮」的新坐標。
new_x=x2+x_offset
new_y=y2+y_offset
return new_x,new_y
def is_xyz_on_line(x1,y1,x2,y2,x3,y3,accuracy=0.01):
ret=False
dist_1 = get_distance(x1,y1,x3,y3)
dist_2 = get_distance(x2,y2,x3,y3)
dist_compare = dist_1 + dist_2
dist_full = get_distance(x2,y2,x1,y1)
dist_diff = abs(dist_full-dist_compare)
compare_accuracy = dist_full * accuracy
if compare_accuracy <= 2:
compare_accuracy = 2
#distance too short, need more accuracy.
if dist_full <= 100:
compare_accuracy = 1
#print("dist_full:", dist_full)
#print("dist_compare:", dist_compare)
#print("dist_diff:",dist_diff)
#print("compare_accuracy:", compare_accuracy)
if dist_diff <= compare_accuracy:
ret = True
return ret
def get_distance(x1,y1,x2,y2):
dist = int(hypot(x2 - x1, y2 - y1))
return dist
def average(lst):
return sum(lst) / len(lst)
def is_same_direction_list(args,deviation=0):
ret = True
args_average=average(args)
#print("args_average:", args_average)
direction = -1
if args[0] <= args_average:
direction = 1
idx=0
args_count = len(args)
for item in args:
idx+=1
if idx == args_count:
break
if direction==1:
if (args[idx]+deviation)<item and (args[idx]-deviation)<item:
ret = False
break
else:
if (args[idx]+deviation)>item and (args[idx]-deviation)>item:
ret = False
break
return ret
def is_same_direction(*args,deviation=0):
return self.is_same_direction_list(args,deviation=deviation)
# common functions.
def find_between(s, first, last ):
try:
start = s.index( first ) + len( first )
end = s.index( last, start )
return s[start:end]
except ValueError:
return ""
def field_right(s, first, is_include_symbol=False):
try:
start = s.index(first )
if not is_include_symbol:
start += len(first )
return s[start:]
except ValueError:
return ""
def field_left(s, first, is_include_symbol=False):
try:
start = s.index(first )
if is_include_symbol:
start += len(first )
return s[:start]
except ValueError:
return ""
def two_point_extend_next(x1,y1,x2,y2):
new_x, new_y = x2,y2
for distance_offset in range(2,12):
new_x,new_y=two_point_extend(x1,y1,x2,y2,distance_offset)
if not new_x == x2:
break
if not new_y == y2:
break
return new_x, new_y
# for test functions.
if __name__ == '__main__':
x1,y1=1,1
x2,y2=10,10
distance_offset = 15
distance = get_distance(x1,y1,x2,y2)
print("distance:",distance)
#distance_offset = distance
new_x,new_y=two_point_extend(x2,y2,x1,y1,-1*distance_offset)
print("new x,y:",new_x,new_y)
#new_x,new_y=two_point_extend_next(x1,y1,x2,y2)
#print("new x,y:",new_x,new_y)