点列表的长度

时间:2014-01-19 12:37:56

标签: python python-2.7

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

[(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)

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

答案 2 :(得分: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()中定义Point方法,在length中定义LineString

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