pmacs3/point.py

43 lines
1.6 KiB
Python
Raw Permalink Normal View History

class Point(tuple):
'''Represents an (x,y) coordinate'''
def __new__(cls, x, y):
return tuple.__new__(cls, (y, x))
def __getattr__(self, name):
if name == "x":
return self[1]
elif name == 'y':
return self[0]
else:
raise AttributeError
def __repr__(self):
return '<Point(%d, %d)>' % (self[1], self[0])
def __str__(self):
return '(%d,%d)' % (self[1], self[0])
def xy(self):
'''Returns a tuple (x,y)'''
return (self[1], self[0])
def yx(self):
'''Returns a tuple (x,y)'''
return tuple(self)
def add(self, xdelta, ydelta):
'''Returns a new point, adding xdelta and ydelta'''
return Point(self[1] + xdelta, self[0] + ydelta)
def vadd(self, xdelta, ydelta):
'''Returns a new point. If ydelta != 0, xdelta is absolute; otherwise, xdelta is relative'''
assert xdelta >= 0 and ydelta >= 0, str((xdelta, ydelta))
if ydelta != 0:
return Point(xdelta, self[0] + ydelta)
else:
return Point(self[1] + xdelta, self[0])
#def sub(self, xdelta, ydelta):
# '''Returns a new point, subtracing xdelta and ydelta'''
# return Point(self[1] - xdelta, self[0] - ydelta)
#def vsub(self, xdelta, ydelta):
# '''Returns a new point. If ydelta != 0, xdelta is absolute; otherwise, xdelta is relative'''
# assert xdelta >= 0 and ydelta >= 0, str((xdelta, ydelta))
# if ydelta != 0:
# return Point(self[1], self[0] - ydelta)
# else:
# return Point(self[1] - xdelta, self[0])