pmacs3/point2.py

27 lines
752 B
Python
Raw Normal View History

2007-06-04 22:30:58 -04:00
class Point(tuple):
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):
return (self[1], self[0])
def add(self, x, y):
assert x >= 0, y >= 0
return Point(self[1] + x, self[0] + y)
def vadd(self, x, y):
assert x >= 0, y >= 0
if y > 0:
return Point(x, self[0] + y)
else:
return Point(self[1] + x, self[0])