這應該插值工作(說潮線爲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)上運行。