""" Proof-of-concept lazy geometry Magnus Lie Hetland 2004-07 """ from __future__ import division from math import sin, cos __all__ = ['Point', 'LazyPoint', 'shifted', 'rotated', 'scaled'] __metaclass__ = type class Point: def __init__(self, x=0, y=0): self.x = x self.y = y def __add__(self, other): return LazyPoint(pointadd, (self, other)) def __sub__(self, other): return LazyPoint(pointsub, (self, other)) def __mul__(self, scale): return LazyPoint(scaled, (self, scale, scale)) __rmul__ = __mul__ def __div__(self, frac): return self * (1/frac) __rdiv__ = __div__ class LazyPoint(Point): def __init__(self, func, args): self._func = func self._args = args self._wrap = None def sync(self): if self._wrap is None: self._wrap = self._func(*self._args) def _getx(self): self.sync() return self._wrap.x def _setx(self, val): self.sync() self._wrap.x = val x = property(_getx, _setx) def _gety(self): self.sync() return self._wrap.y def _sety(self, val): self.sync() self._wrap.y = val y = property(_gety, _sety) def pointadd(pointa, pointb): return Point(pointa.x+pointb.x, pointa.y+pointb.y) def pointsub(pointa, pointb): return Point(pointa.x-pointb.x, pointa.y-pointb.y) def shifted(point, dx, dy): return Point(point.x+dx, point.y+dy) def rotated(point, a): return Point(point.x*cos(a)-point.y*sin(a), point.y*cos(a)+point.x*sin(a)) def scaled(point, sx, sy): return Point(point.x*sx, point.y*sy)