pmacs3/point.py

69 lines
1.8 KiB
Python

LOGICAL = "logical"
PHYSICAL = "physical"
class Point:
def __init__(self, x=0, y=0, t=None):
raise Exception, "argh!"
assert t is None or \
t == LOGICAL or \
t == PHYSICAL
self.x = x
self.y = y
self.type = t
def offset(self, x, y, t=None):
return Point(self.x + x, self.y + y, t)
def copy(self):
return Point(self.x, self.y, self.type)
def __cmp__(self, other):
assert self.type == other.type or \
self.type is None or \
other.type is None
c = cmp(self.y, other.y)
if c == 0:
return cmp(self.x, other.x)
else:
return c
def __add__(self, other):
assert self.type == other.type or \
self.type is None or \
other.type is None
return Point(self.x + other.x,
self.y + other.y)
def __iadd__(self, other):
assert self.type == other.type or \
self.type is None or \
other.type is None
x = other.x
y = other.y
self.x += x
self.y += y
def __sub__(self, other):
assert self.type == other.type or \
self.type is None or \
other.type is None
return Point(self.x - other.x,
self.y - other.y)
def __isub__(self, other):
assert self.type == other.type or \
self.type is None or \
other.type is None
x = other.x
y = other.y
self.x -= x
self.y -= y
def __abs__(self):
return Point(abs(self.x), abs(self.y))
def __repr__(self):
if self.type is None:
return "Point(%d, %d)" % (self.x, self.y)
else:
return "Point(%s:%d, %d)" % (self.type[0], self.x, self.y)