From 7a46bc6faea4ba817c262ff89d7be080e888355f Mon Sep 17 00:00:00 2001 From: "Peter J. Holzer" Date: Sat, 29 Dec 2018 20:38:36 +0100 Subject: [PATCH] Print coordinates of hilbert curve (no generator yet) --- Python/hilbertgenerator.py | 45 ++++++++++++++++++++++++++++++++++++++ Python/test1.py | 5 +++++ 2 files changed, 50 insertions(+) create mode 100644 Python/hilbertgenerator.py create mode 100755 Python/test1.py diff --git a/Python/hilbertgenerator.py b/Python/hilbertgenerator.py new file mode 100644 index 0000000..b29b76b --- /dev/null +++ b/Python/hilbertgenerator.py @@ -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) + diff --git a/Python/test1.py b/Python/test1.py new file mode 100755 index 0000000..76a2982 --- /dev/null +++ b/Python/test1.py @@ -0,0 +1,5 @@ +#!/usr/bin/python3 +from hilbertgenerator import Vector, hilbert + +n = 1 +hilbert(Vector(n, n), Vector(n, 0), Vector(0,n))