这应该适用于插值(比如潮汐线是距离岸上某个控制点的 x,y 距离):
import math
example_triangle = [[0,0], [5,5], [0,5], [0,0]]
high_tide_line = [[0, 0], [5., 1.5], [10., 3.2], [20., 1.], [30., 4.5], [80.,2.], [80,0]]
low_tide_line = [[0, 10.], [5., 11.5], [10., 13.2], [20., 11.], [30., 14.5], [80., 12.], [80, 10]]
def points_from_geom(geom):
idx = 0
line_lengths = []
unit_vectors = []
interpolated_points = []
while idx < (len(geom) - 1):
dy, dx = ((geom[idx+1][1] - geom[idx][1]), (geom[idx+1][0] - geom[idx][0]))
line_lengths.append(math.sqrt(dy**2 + dx**2))
try:
angle = math.atan(dy/dx)
unit_vectors.append([math.cos(angle)*cmp(dx, 0),
math.sin(angle)*cmp(dy, 0)])
except ZeroDivisionError:
if geom[idx+1][1] < geom[idx][1]:
direction = [0, -1]
else:
direction = [0, 1]
unit_vectors.append(direction)
idx += 1
for i, length in enumerate(line_lengths):
inter = 0
while inter <= length:
interpolated_points.append([geom[i][0] + unit_vectors[i][0]*inter,\
geom[i][1] + unit_vectors[i][1]*inter])
inter += .3048 # a ft in proper units ;)
return interpolated_points
ln1 = points_from_geom(example_triangle)
ln2 = points_from_geom(high_tide_line)
ln3 = points_from_geom(low_tide_line)
print ln1, ln2, ln3
有很多不同的方法来做多边形。我可能会尝试确定参考海岸线,然后以固定间隔或在每个线段的中间取垂直线,然后在相邻海岸线上的交点处制作多边形或边界框。顺便说一句,在实际应用程序中,您需要确保cmp
不在 (0,0) 上运行。