-3

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.
Simon Hanks
  • 55
  • 2
  • 8
  • 2
    And what is your question? – Daniel Roseman Jan 10 '17 at 23:15
  • 2
    Your Pythagoras is incorrect. You should be doing `math.sqrt((x1 - x0)**2 + (y1 - y0)**2)`. – Martin Valgur Jan 10 '17 at 23:15
  • I think you're looking for something like http://stackoverflow.com/q/5434891/3001761, but you should [edit] to make it clearer. – jonrsharpe Jan 10 '17 at 23:18
  • What is so complicated about calculating two pairs at a time and having a cumulative total? Write the pseudo-code using a loop that has a iteration skip of two indices. – OneCricketeer Jan 10 '17 at 23:22
  • It's not really appropriate to edit your question to ask something different (though I'm not really sure what you're asking at all). If you have a new question to ask (and it can't be made as a comment on an answer), please ask a completely new question (perhaps with a link back to this one for context). – Blckknght Jan 12 '17 at 01:52

1 Answers1

0

A more concise and working version of the code you posted

from itertools import starmap

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

    def move(self, dx, dy):
        self.x += dx
        self.y += dy

class LineString:
    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)

Testing

import numpy as np

points = np.arange(20).reshape((-1, 2))
ls = LineString(*points)
ls.length()

Returns 25.455 = sqrt(8) * 9, which is correct.

Martin Valgur
  • 5,793
  • 1
  • 33
  • 45
  • Thanks @Martin Valgur , I have another question about composition. I want to use the move method in LineString. Please see my edit, I am new to python and thanks again for your help. – Simon Hanks Jan 11 '17 at 20:18