File: hilbert.py

package info (click to toggle)
minetest-mod-pycraft 0.22-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,744 kB
  • sloc: python: 79,282; makefile: 10
file content (35 lines) | stat: -rwxr-xr-x 773 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#
# Code under the MIT license by Alexander Pruss
#
import lsystem
from mcturtle import *
from sys import argv
t = Turtle()
t.pendelay(0)
t.turtle(None)
t.gridalign()

# Hilbert curve axiom and production rule by Stan Wagon, Mathematica in Action (chapter 6), W. H. Freeman and Co., 1991
rules = { 'X': '^<XF^<XFX+F^>>XFX&F->>XFX+F>X+>' }

count = 0

def go():
  global count
  # seven segments per basic unit
  if count % 7 == 0:
      t.penblock(Block(WOOL.id, (count/7)%16))
  count += 1
  t.go(4)

dictionary = {
  'F': go,
  '+': lambda: t.yaw(90),
  '-': lambda: t.yaw(-90),
  '^': lambda: t.pitch(90),
  '&': lambda: t.pitch(-90),
  '>': lambda: t.roll(90),
  '<': lambda: t.roll(-90)
  }

lsystem.lsystem('X', rules, dictionary, 3 if len(argv)<2 else int(argv[1]))