Move points and check specific start value, using classes python

3k views Asked by At

I need to be able to move points and check a specific point value. This is the code:

class Point(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y
    def move(self)
        #Here I want to move my points

Next class is a linestring. It must be able to handle x set of points

class LineString(Point):
    def __init__(self, *points):
        self.points = []
        for point in points:
            if not isinstance(point, Point):
                point = Point(*point)
            self.points.append(point)
    def __getitem__(self):
         #Here I want to inspect the value of the specific
         # e.g. y value for the start point after it has been moved

I'm a bit unsure of how to get the __getitem__ to work and whether it's in the right position. Should it be under class Point? Could this be done in another way?

Edited code;

from numpy import sqrt
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)

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

class LineString(Point):
    def __init__(self, *points):
        self.points = []
        for point in points:
            if not isinstance(point, Point):
                point = Point(*point)
            self.points.append(point)

    def length(self):
        return sum(p1.dist(p2) for p1, p2 in zip(self.points[1:], self.points[:-1]))

    def move (self, x, y):
        for p in self.points:
            p.move(x, y)

    def __getitem__(self, key):
        return self.points[key]
1

There are 1 answers

1
Tom Dalton On

I think this is roughly what you want:

You don't seem to actually need a dictionary (for a line, I think a list makes more sense anyway). So the Line class is just a list of Points, and it provides a move_all_points function to translate them all. Because Line subclasses a list, you get all the standard behaviour of lists for free:

class Point(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y
    def __repr__(self):
        return "<Point({},{})>".format(self.x, self.y)
    def __str__(self):
        return(repr(self))
    def move(self, dx, dy):
        """Move the point by (dx, dy)."""
        self.x += dx
        self.y += dy

class Line(list):
    """A list of points that make up a line."""
    def move_all_points(self, dx, dy):
        for p in self:
            p.move(dx, dy)

So then you can use them as follows:

>>> p1, p2, p3 = Point(0, 0), Point(5, 0), Point(10, 10)

>>> my_line = Line((p1, p2, ))
>>> print my_line
[<Point(0,0)>, <Point(5,0)>]

>>> my_line.append(p3)
>>> print my_line
[<Point(0,0)>, <Point(5,0)>, <Point(10,10)>]

>>> p4 = Point(100,100)
>>> my_line.move_all_points(1, 1)
>>> print my_line
[<Point(1,1)>, <Point(6,1)>, <Point(11,11)>]

>>> my_line.append(p4)
>>> print my_line
[<Point(1,1)>, <Point(6,1)>, <Point(11,11)>, <Point(100,100)>]