从Point类到Linestring类

时间:2018-09-30 23:10:28

标签: python line point

我创建了一个Point类,该类使用x,y坐标作为参数。我还想创建一个Linestring类,该类接受用户想要的任意数量的参数并将其存储为点。到目前为止:

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

  def move(self,movex,movey):
    self.x += movex
    self.y += movey

class LineString(object):
  def __init__(self, *args):
    self.points = [Point(*p) for p in args]

所以现在我在self.points中存储了一个点列表。 问题是如何在类线串中使用点的移动功能。 我尝试过类似的方法,但是它不起作用

def moveline(self,movex,movey):
    self.points.move(movex,movey)

1 个答案:

答案 0 :(得分:0)

要确切说明@MichaelButscher在评论中所说的内容,您的moveline函数的问题是self.pointsPoint对象的列表Point对象本身。因此,我们需要遍历此列表并为每个move对象调用Point函数。这可以通过for循环来完成。您更新后的moveline函数如下所示:

def moveline(self,movex,movey):
    for point in self.points:
        point.move(movex,movey)
相关问题