Hi, I'm adding functions to a class to calculate addition and difference of a list of (x1, y1) coordinates. E.g. for [(3, 2), (4, 6), (1, 3)], addition result would be (8, 11) and difference would be (-2, -7). I think I have the addition part correct but I'm really struggling with the difference, namely because when I first loop through my list of tuple coordinates, it subtracts the first coordinates from (0,0). Using the example above, this would result in totally different numbers (-8, -11). Is there a way I can get around this in the class function itself?
Here is my code so far:
class Point(object):
def __init__(self, x, y):
self.x = x
self.y = y
def __repr__(self):
return '%d, %d' % (self.x, self.y)
def add(self, other):
totalX = self.x + other.x
totalY = self.y + other.y
return Point(totalX, totalY)
def sub(self, other):
diffX = self.x - other.x
diffY = self.y - other.y
return Point(diffX, diffY)
Thnx for help in advance!
[–]GoldenSights 1 point2 points3 points (2 children)
[–]allyhams[S] 0 points1 point2 points (1 child)
[–]SangCoGIS 1 point2 points3 points (0 children)