C curve
to Ccurve :d
if :d = 0 [
forward :atom
] [
make "d (- :d 1)
left 45
Ccurve :d
right 90
Ccurve :d
left 45
]
end
to CcurveDraw :depth [:plane 100] 1
(local "atom)
make "atom :plane
repeat :depth [make "atom (* :atom 0.7)]
penup
back / :plane 2
pendown
Ccurve :depth
end
1
|
2
|
|
|
1)(CcurveDraw 1 150)
2)(CcurveDraw 15 150)
C曲線あるいは、レヴィ曲線(Levy)と呼ばれる再帰図形です。
くの字を基本図形としています。プログラムの方も、単純な作りになっていますが、元の図形からは想像出来ない図形が描けますね。
Dragon Curve
;----------------------------------------------------------------------
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)
name 255 "max
make "divval int quotient :ct 256
make "modval remainder :ct 256
if :divval < 0 or :divval > 5 [setpc 0 stop]
run item + :divval 1 [
[setpc (list :max :modval 0)]
[setpc (list (- :max :modval) :max 0)]
[setpc (list 0 :max :modval)]
[setpc (list 0 (- :max :modval) :max)]
[setpc (list :modval 0 :max)]
[setpc (list :max 0 (- :max :modval))]
]
end
;----------------------------------- Dragon curve -----------------------------------
to dragon :sign :d
if :d = 0 [forward :atom stop]
make "counter sum :counter 1
make "d (- :d 1)
right * 45 :sign
dragon -1 :d
left * 90 :sign
dragon 1 :d
right * 45 :sign
end
to dragonDraw :depth [:plane 300] 1
(local "atom "counter)
make "atom :plane
make "counter 0
repeat :depth [make "atom (* :atom 0.7)]
penup
right 90
back * :plane 0.495
pendown
dragon 1 :depth
print sentence "counter :counter
end
;----------------------------------- Color version Dragon curve -----------------------------------
to cDragon :sign :d
if :d = 0 [forward :atom stop]
sethpc round * :hcdiv :counter
make "counter sum :counter 1
make "d (- :d 1)
right * 45 :sign
cDragon -1 :d
left * 90 :sign
cDragon 1 :d
right * 45 :sign
end
to cDragonDraw :depth [:plane 300] 1
(local
"atom "counter "hcdiv
)
make "atom :plane
make "counter 0
make "hcdiv (/ 1535 precount :depth 2)
repeat :depth [make "atom (* :atom 0.7)]
penup
right 90
back * :plane 0.495
pendown
cDragon 1 :depth
print sentence "counter :counter
end
1)dragonDraw 1
2)dragonDraw 11
3)(cDragonDraw 16 200)
このドラゴン曲線も、くの字を基本図形とした再帰図形です。
C曲線も、くの字を基本図形としているのでプログラムも、よく似ていますね。
違いと言えば、ドラゴン曲線は自分自身を反転する為に、sign
変数で指示しています。
![](../img/bret.gif)
|