25 lines
683 B
Python
25 lines
683 B
Python
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 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])
|