2014-01-19 111 views
0

对于给定线条的给定列表,我想计算它的长度。例如:点的长度的长度

[(1,0),(5,6),(9,6),(5,2)] 

这些只是连接点的列表。我怎样才能有一个自动化的方法来计算它们? 创造的线串代码:

import math 
class Point(object): 
    def __init__(self, x, y): 
     self.x = x 
     self.y = y 

class LineString(Point): 
    def __init__(self, *points): 
     #~ self.points=points 
     self.points = [] 
     for point in points: 
      if not isinstance(point, Point): 
       point = Point(*point) 
      self.points.append(point) 




if __name__ == '__main__': 
    # Tests for LineString 
    # =================================== 
    lin1 = LineString((1, 1), (0, 2)) 


    assert lin1.length() == math.sqrt(2.0) 
+0

你能更多地讨论列表中实际包含什么? – justhalf

+0

请提供列表示例和相应的预期结果。 – user3146587

+0

好吧,我会添加代码在最小的 –

回答

1

如果你使用numpy的:

import numpy as np 

line = np.array([(1,0),(5,6),(9,6),(5,2)], float) 
print np.sqrt(np.sum((line[1:] - line[:-1])**2, -1)).sum() 

或列表解析:

line = [(1,0),(5,6),(9,6),(5,2)] 

sum(((x1-x2)**2 + (y1-y2)**2)**0.5 for (x1,y1),(x2,y2) in zip(line[:-1], line[1:])) 

编辑

通过使用你的类,你可以定义一个dist()方法PointlengthLineString

import math 
class Point(object): 
    def __init__(self, x, y): 
     self.x = x 
     self.y = y 

    def dist(self, point): 
     return math.hypot(self.x-point.x, self.y-point.y) 

class LineString(Point): 
    def __init__(self, *points): 
     #~ self.points=points 
     self.points = [] 
     for point in points: 
      if not isinstance(point, Point): 
       point = Point(*point) 
      self.points.append(point) 

    @property 
    def length(self): 
     return sum(p1.dist(p2) for p1, p2 in zip(self.points[1:], self.points[:-1])) 

line = LineString((1,0),(5,6),(9,6),(5,2)) 
print line.length 
+0

更简洁的NumPy解决方案:'np.hypot(* np.diff(line.T))。sum()' –

3

我想出了这个解决方案。我反覆对和使用math.hypot

l = [(1,0),(5,6),(9,6),(5,2)] 

from itertools import tee, izip 
from math import hypot 

def pairwise(iterable): 
    a, b = tee(iterable) 
    next(b, None) 
    return izip(a, b) 

distance = 0 
# iterate on pairs of points 
for prev_point, next_point in pairwise(l): 
    prev_x, prev_y = prev_point 
    next_x, next_y = next_point 
    distance += hypot(next_x - prev_x, next_y - prev_y) 

print distance 
2

使用summath.hypot

>>> from math import hypot 
>>> l = [(1, 0), (5, 6), (9, 6), (5, 2)] 
>>> sum(hypot(x1 - x2, y1 - y2) for (x1, y1), (x2, y2) in zip(l, l[1:])) 
16.86795680042036