2007-06-04 22:30:58 -04:00
|
|
|
class Point(tuple):
|
2007-07-06 14:34:14 -04:00
|
|
|
'''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])
|
2007-07-02 20:05:58 -04:00
|
|
|
def __str__(self):
|
|
|
|
return '(%d,%d)' % (self[1], self[0])
|
2007-06-04 22:30:58 -04:00
|
|
|
|
|
|
|
def xy(self):
|
2007-07-06 14:34:14 -04:00
|
|
|
'''Returns a tuple (x,y)'''
|
2007-06-04 22:30:58 -04:00
|
|
|
return (self[1], self[0])
|
2007-07-06 14:34:14 -04:00
|
|
|
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:
|
2007-07-06 14:34:14 -04:00
|
|
|
return Point(self[1] + xdelta, self[0])
|