Print coordinates of hilbert curve (no generator yet)

This commit is contained in:
Peter J. Holzer 2018-12-29 20:38:36 +01:00
commit 7a46bc6fae
2 changed files with 50 additions and 0 deletions

View File

@ -0,0 +1,45 @@
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)

5
Python/test1.py Executable file
View File

@ -0,0 +1,5 @@
#!/usr/bin/python3
from hilbertgenerator import Vector, hilbert
n = 1
hilbert(Vector(n, n), Vector(n, 0), Vector(0,n))