-3
Martinのソリューションで更新されました(多くの感謝)。コンポジションからLineStringクラスでmoveメソッドを使用する方法を知りたいですか?コードを完成させるために渡す必要のあるアサーションを見てください。それ以上のガイダンスは高く評価されます。線ストリングを表すクラスを実装していますか?
class Point(object):
def __init__(self, x, y):
self.x = x
self.y = y
def move(self, dx, dy):
self.x += dx
self.y += dy
class LineString(object):
def __init__(self, *points):
self.points = [Point(*point) for point in points]
def length(self):
pairs = zip(self.points, self.points[1:])
return sum(starmap(distance, pairs))
def distance(p1, p2):
return math.sqrt((p1.x - p2.x)**2 + (p1.y - p2.y)**2)
if __name__ == '__main__':
# Tests for LineString
# ===================================
lin1 = LineString((1, 1), (0, 2))
assert lin1.length() == sqrt(2.0)
lin1.move(-1, -1) # Help with Composition needed
assert lin1[0].y == 0 # Inspect the y value of the start point.
lin2 = LineString((1, 1), (1, 2), (2, 2))
lin2.move(-1, -1) # Move by -1 and -1 for x and y respectively
assert lin2.length() == 2.0
assert lin2[-1].x == 1 # Inspect the x value of the end point.
あなたの質問は何ですか? –
あなたのピタゴラスは間違っています。あなたは 'math.sqrt((x1 - x0)** 2 +(y1 - y0)** 2)'を実行する必要があります。 –
私はあなたがhttp://stackoverflow.com/q/5434891/3001761のようなものを探していると思いますが、それを明確にするためには[編集]するべきです。 – jonrsharpe