Sierpinski Arrowhead

to SierpinskiArrowhead :angle :d
    if :d = 0 [forward :atom stop]
         
    SierpinskiArrowhead msgn :angle (- :d 1)
    right :angle
    SierpinskiArrowhead :angle (- :d 1)
    right :angle
    SierpinskiArrowhead msgn :angle (- :d 1)
end
         
to Sarrowhead :depth [:plane 300] 1
    (local
    "atom
    )
         
    make "atom :plane
    repeat :depth [make "atom (/ :atom 2)]
    penup
    setxy sentence msgn / :plane 2 msgn / * :plane 0.866 2
    ;pendown
    ;right 30
    ;repeat 3 [forward :plane right 120]
    ;left 30
    if (remainder :depth 2) = 0 then right 90 else right 30
    pendown
    SierpinskiArrowhead 60 :depth
end
         
;----------------------------------- Color version Sierpinski Arrowhead -----------------------------------
         
to cSierpinskiArrowhead :angle :d
    if :d = 0 [forward :atom stop]
         
    sethpc round * :hcdiv :counter
    make "counter sum :counter 1
    cSierpinskiArrowhead msgn :angle (- :d 1)
    right :angle
    cSierpinskiArrowhead :angle (- :d 1)
    right :angle
    cSierpinskiArrowhead msgn :angle (- :d 1)
end
         
to cSarrowhead :depth [:plane 300] 1
    (local
    "atom "counter "hcdiv
    )
         
    make "atom :plane
    make "counter 0
    make "hcdiv (/ 1535 precount :depth 3)
    repeat :depth [make "atom (/ :atom 2)]
    penup
    setxy sentence msgn / :plane 2 msgn / * :plane 0.866 2
    pendown
    right 30
    repeat 3 [forward :plane right 120]
    left 30
    if (remainder :depth 2) = 0 then right 90 else right 30
    pendown
    cSierpinskiArrowhead 60 :depth
end

cSarrowhead 5

cSarrowhead 10

 

Hilbert Arrowhead

to HilbertArrowhead :d
    if :d = 0 [stop]
         
    make "counter (+ :counter 1)
    revrse60
    right :stat.angle
    HilbertArrowhead - :d 1
    revrse60
    forward :atom
    right :stat.angle
    HilbertArrowhead - :d 1
    right :stat.angle
    forward :atom
    revrse60
    HilbertArrowhead - :d 1
    right :stat.angle
    revrse60 
end
         
to Harrowhead :depth [:plane 300] 1
    (local
    "stat.angle
    "atom "counter
    )
         
    make "stat.angle 60
    make "atom :plane
    make "counter 0
         
    penup
    setxy sentence msgn / :plane 2 msgn / * :plane 0.866 2
    pendown
    right 30
    repeat 3 [forward :plane right 120]
    left 30
    penup
    repeat :depth [make "atom (/ :atom 2)]
    right 90
    forward / :atom 2
    left 90
    forward / * :atom 0.866 2
    right 90
    pendown
    HilbertArrowhead :depth
    print sentence "counter :counter
end
         
;----------------------------------- Color version Hilbert Arrowhead -----------------------------------
         
to cHilbertArrowhead :d
    if :d = 0 [stop]
         
    sethpc round * :hcdiv :counter
    make "counter sum :counter 1
    revrse60
    right :stat.angle
    cHilbertArrowhead - :d 1
    revrse60
    forward :atom
    right :stat.angle
    cHilbertArrowhead - :d 1
    right :stat.angle
    forward :atom
    revrse60
    cHilbertArrowhead - :d 1
    right :stat.angle
    revrse60 
end
         
to cHarrowhead :depth [:plane 300] 1
    (local
    "stat.angle
    "atom "counter "hcdiv
    )
         
    make "stat.angle 60
    make "atom :plane
    make "counter 0
    make "hcdiv (/ 1535 precount :depth 3)
         
    penup
    setxy sentence msgn / :plane 2 msgn / * :plane 0.866 2
    pendown
    right 30
    setpc 0
    repeat 3 [forward :plane right 120]
    left 30
    penup
    repeat :depth [make "atom (/ :atom 2)]
    right 90
    forward / :atom 2
    left 90
    forward / * :atom 0.866 2
    right 90
    pendown
    cHilbertArrowhead :depth
    print sentence "counter :counter
end

cHarrowhead 5

cHarrowhead 10

共通ルーチン

to msgn :num
    output * -1 :num
end
         
to revrse60
    if :stat.angle = 60 [make "stat.angle -60] [
      make "stat.angle 60]
end
         
to precount :d :x
    local "out
         
    make "out 0
    repeat :d [make "out (+ * :out :x 1)]
    output :out
end
         
to sethpc :ct
    (local "max "divval "modval "difval)
         
    name 255 "max
    make "divval int quotient :ct 256
    make "modval remainder :ct 256
    make "difval difference :max :modval
         
    if :divval < 0 or :divval > 5 [setpc 0 stop]
    run item + :divval 1 [
      [setpc (list :max :modval 0)]
      [setpc (list :difval :max 0)]
      [setpc (list 0 :max :modval)]
      [setpc (list 0 :difval :max)]
      [setpc (list :modval 0 :max)]
      [setpc (list :max 0 :difval)]
    ]
end

 シェルピンスキとヒルベルトの矢じり図形です。
SierpinskiArrowheadの方は、素直で判りやすいプログラムだと思います。なにか問題有りますか?:-D
それに較べて、HilbertArrowheadの方は、ちょいとゴチャゴチャしていますね。
もう一度、Hilbert Curveを見て下さい。
はい、ほぼ同一の物ですね。ちょっとだけプログラムを変更しています。でも見た目が全然ちがう図形になりました。

しかし、シェルピンスキとヒルベルトの矢じり図形は、再帰レベルが深くなるに連れ見た目の違いが、ほとんど有りません。
と言うか、再帰レベル10以上の図形を較べて違いの判る人が、居るのでしょうか。;-)