Hilbert Curves In Python

Hi, I have been learning python and I have recently written a little program that creates Hilbert curves which I submit to this list for critique and review: # Hilbert Curve # David Zuccaro # 18/09/2014 import sys import pygame import math class cursor: def __init__(self, a): # a should be a list of 2 numbers ie a point. self.a = a def line(self, a): # draw a line from he current position from the new position; update current position pygame.draw.lines(window, (255, 255, 255), True, [[self.a[0], self.a[1]], [self.a[0] + a[0], self.a[1] + a[1]]], 1) self.a[0] = self.a[0] + a[0] self.a[1] = self.a[1] + a[1] def hilbert(self, kind, order): row = ((1,0,0,2), (0,1,1,3), (3,2,2,0), (2,3,3,1)) # hilbert curve definition table 0 = A 1 = B ... dir = ((0,3,2), (3,0,1), (1,2,3), (2,1,0)) # definition of A, B, C, D orient = ((0,-1), (-1,0), (0,1), (1,0)) # definition of up, left, down, right dist = 30 # length of segment if order > 0: for j in range(4): self.hilbert(row[kind][j], order - 1) # keep on hilberting until order = 0 if j < 3: self.line([x * dist for x in orient[dir[kind][j]]]) #this was the previous code but I thought it would be cleaner if # more abstruse to do this all in one line ^ # if dir[kind][j] == 0: # self.line(0, -dist) #up # if dir[kind][j] == 1: # self.line(-dist, 0) #left # if dir[kind][j] == 2: # self.line(0,dist) #down # if dir[kind][j] == 3: # self.line(dist, 0) #right pygame.init() #create the screen window = pygame.display.set_mode((1900, 1000)) david = cursor([10,970]) # starting point increasing y moves down screen david.hilbert(0, 5) # create a fifth order hilbert curve #draw it to the screen pygame.display.flip() #input handling (somewhat boilerplate code): while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit(0) #end Output is here: http://members.optuszoo.com.au/~david.zuccaro/hilbert2.png https://en.wikipedia.org/wiki/Hilbert_curve

David Zuccaro wrote:
Hi,
I have been learning python and I have recently written a little program that creates Hilbert curves which I submit to this list for critique and review:
# Hilbert Curve
# David Zuccaro # 18/09/2014
import sys import pygame import math
class cursor:
def __init__(self, a): # a should be a list of 2 numbers ie a point. self.a = a
def line(self, a): # draw a line from he current position from the new position; update current position pygame.draw.lines(window, (255, 255, 255), True, [[self.a[0], self.a[1]], [self.a[0] + a[0], self.a[1] + a[1]]], 1) self.a[0] = self.a[0] + a[0] self.a[1] = self.a[1] + a[1]
def hilbert(self, kind, order): row = ((1,0,0,2), (0,1,1,3), (3,2,2,0), (2,3,3,1)) # hilbert curve definition table 0 = A 1 = B ... dir = ((0,3,2), (3,0,1), (1,2,3), (2,1,0)) # definition of A, B, C, D orient = ((0,-1), (-1,0), (0,1), (1,0)) # definition of up, left, down, right dist = 30 # length of segment if order > 0: for j in range(4):
self.hilbert(row[kind][j], order - 1) # keep on hilberting until order = 0
if j < 3: self.line([x * dist for x in orient[dir[kind][j]]])
#this was the previous code but I thought it would be cleaner if # more abstruse to do this all in one line ^
# if dir[kind][j] == 0: # self.line(0, -dist) #up
# if dir[kind][j] == 1: # self.line(-dist, 0) #left
# if dir[kind][j] == 2: # self.line(0,dist) #down
# if dir[kind][j] == 3: # self.line(dist, 0) #right
pygame.init()
#create the screen window = pygame.display.set_mode((1900, 1000))
david = cursor([10,970]) # starting point increasing y moves down screen
david.hilbert(0, 5) # create a fifth order hilbert curve
#draw it to the screen pygame.display.flip()
#input handling (somewhat boilerplate code): while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit(0)
#end
Output is here: http://members.optuszoo.com.au/~david.zuccaro/hilbert2.png
https://en.wikipedia.org/wiki/Hilbert_curve
_______________________________________________ luv-main mailing list luv-main@luv.asn.au http://lists.luv.asn.au/listinfo/luv-main Which by it's rarity reminds us why luv-prog ?; is sadly no more !; are people taking their programming problems to specialist language lists or are their no programming problems ? Just curious regards Rohan McLeod

On 19 September 2014 11:12, David Zuccaro <david.zuccaro@optusnet.com.au> wrote:
I have been learning python and I have recently written a little program that creates Hilbert curves which I submit to this list for critique and review:
Hi David I suggest the Python Tutor mailing list https://mail.python.org/mailman/listinfo/tutor which has an excellent community that usually responds thoroughly to review requests of this nature. You also need to state which Python version you are using. Ideally it should be 3.4 for new code, or 2.7 if you wish to use version 2 for some reason, perhaps for compatibility with your imported modules. And make sure to send text-only message (not html) because html does not preserve whitespace reliably. I suggest renaming your local variable to something other than 'dir' because in the scope where that name is rebound it will prevent access to the 'dir' Python builtin function, see help(dir). Not that this affects your program but it is something to avoid generally. Cheers David

On Fri, 19 Sep 2014, David wrote:
On 19 September 2014 11:12, David Zuccaro <david.zuccaro@optusnet.com.au> wrote:
I have been learning python and I have recently written a little program that creates Hilbert curves which I submit to this list for critique and review:
Hi David
I suggest the Python Tutor mailing list https://mail.python.org/mailman/listinfo/tutor which has an excellent community that usually responds thoroughly to review requests of this nature.
You also need to state which Python version you are using. Ideally it should be 3.4 for new code, or 2.7 if you wish to use version 2 for some reason, perhaps for compatibility with your imported modules.
And make sure to send text-only message (not html) because html does not preserve whitespace reliably.
Someone should invent a language for this web oriented world where slight whitespace variations that inevitably crop up during web communication doesn't affect the runnability of a program. And where if you decide you need to run an autoindenter because of mentioned whitespace problems, the autoindent doesn't have to read your mind in order not to screw up the program logic. (wasn't whitespace rigidity stupidity why most people gave up on Fortran? Ironically, Fortran is now far more relaxed about whitespace). -- Tim Connors

Tim Connors wrote:
(wasn't whitespace rigidity stupidity why most people gave up on Fortran? Ironically, Fortran is now far more relaxed about whitespace).
Well I must confess some little frustration with the "FORMAT' statement; which on at least one occasion resulted in all the out put from my incredibly interesting program (really !); being confined to a small black square ! regards Rohan McLeod

On 22/09/14 12:29, Tim Connors wrote:
Someone should invent a language for this web oriented world where slight whitespace variations that inevitably crop up during web communication doesn't affect the runnability of a program.

Tim Connors writes:
Someone should invent a language for this web oriented world where slight whitespace variations that inevitably crop up during web communication doesn't affect the runnability of a program.
Back in the 90s, it was called perl. Not to worry, this web fad will blow over RSN, when everyone remembers why they switched from mainframes & thin terminals to personal computers in the first place.
participants (6)
-
David
-
David Zuccaro
-
Paul Dwerryhouse
-
Rohan McLeod
-
Tim Connors
-
trentbuck@gmail.com