pmacs3/point2.py

33 lines
1.1 KiB
Python
Raw Normal View History

2007-06-04 22:30:58 -04:00
class Point(tuple):
'''Represents an (x,y) coordinate'''
2007-06-04 22:30:58 -04:00
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])
2007-06-04 22:30:58 -04:00
def xy(self):
'''Returns a tuple (x,y)'''
2007-06-04 22:30:58 -04:00
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, applying 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)
2007-06-04 22:30:58 -04:00
else:
return Point(self[1] + xdelta, self[0])