Updated with the solution from Martin (Many thanks). Would like to know how to use the move method in the LineString class through a composition. Please have a look at the assertions which need to be passed to make the code complete. Any further guidance would be much appreciated.
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.