hilbert/Python/hilbertgenerator.py

62 lines
1.5 KiB
Python
Raw Normal View History

from math import sqrt
class Vector:
def __init__(self, x, y):
self.x = x
self.y = y
def __bool__(self):
return bool(self.x) or bool(self.y)
def __truediv__(self, divisor):
return Vector(self.x / divisor, self.y / divisor)
def __floordiv__(self, divisor):
return Vector(int(self.x / divisor), int(self.y / divisor))
def __add__(self, other):
return Vector(self.x + other.x, self.y + other.y)
def __sub__(self, other):
return Vector(self.x - other.x, self.y - other.y)
def __neg__(self):
return Vector(-self.x, -self.y)
def __invert__(self):
return Vector(self.y, self.x)
def __str__(self):
return "(%d, %d)" % (self.x, self.y)
def __abs__(self):
return sqrt(self.x * self.x + self.y * self.y)
def hilbert(center, x, y):
if abs(x) < 1:
print(center)
else:
x /= 2
y /= 2
hilbert(center - x + y, -~x, -~y)
hilbert(center - x - y, x, y)
hilbert(center + x - y, x, y)
hilbert(center + x + y, ~x, ~y)
2018-12-29 21:02:44 +01:00
def hilbertgenerator(center, x, y):
if abs(x) < 1:
yield center
else:
x /= 2
y /= 2
for r in hilbertgenerator(center - x + y, -~x, -~y):
yield r
for r in hilbertgenerator(center - x - y, x, y):
yield r
for r in hilbertgenerator(center + x - y, x, y):
yield r
for r in hilbertgenerator(center + x + y, ~x, ~y):
yield r