Dragon curve: Difference between revisions
Content deleted Content added
→{{header|Logo}}: add a bit-twiddling iterative version |
|||
Line 1,017: | Line 1,017: | ||
=={{header|Logo}}== |
=={{header|Logo}}== |
||
===Recursive=== |
|||
<lang logo>to dcr :step :length |
<lang logo>to dcr :step :length |
||
make "step :step - 1 |
make "step :step - 1 |
||
Line 1,060: | Line 1,061: | ||
if :step=1 [Rt 180 fd :length Lt 180] [O (:step - 1) (:length / 1.41421) S (:step - 1) (:length / 1.41421)] |
if :step=1 [Rt 180 fd :length Lt 180] [O (:step - 1) (:length / 1.41421) S (:step - 1) (:length / 1.41421)] |
||
end</lang> |
end</lang> |
||
===Iterative=== |
|||
Or drawing iteratively by making a turn left or right at each point calculated by bit-twiddling. This allows any length to be drawn, not just powers-of-2. Tested in ucblogo 5.5 and 6.0. |
|||
<lang logo>; Return the bit above the lowest 1-bit in :n. |
|||
; If :n = binary "...z100..00" then the return is "z000..00". |
|||
; Eg. n=22 is binary 10110 the lowest 1-bit is the "...1." and the return is |
|||
; bit above that "..1.," which is 4. |
|||
to bit.above.lowest.1bit :n |
|||
output bitand :n (1 + (bitxor :n (:n - 1))) |
|||
end |
|||
; Return angle +90 or -90 for dragon curve turn at point :n. |
|||
; The curve is reckoned as starting from n=0 so the first turn is at n=1. |
|||
to dragon.turn.angle :n |
|||
output ifelse (bit.above.lowest.1bit :n) = 0 [90] [-90] |
|||
end |
|||
; Draw :steps many segments of the dragon curve. |
|||
to dragon :steps |
|||
localmake "step.len 12 ; length of each step |
|||
repeat :steps [ |
|||
forward :step.len |
|||
left dragon.turn.angle repcount ; repcount = 1 to :steps inclusive |
|||
] |
|||
end |
|||
dragon 256</lang> |
|||
<lang logo>; Draw :steps many segments of the dragon curve, with corners chamfered |
|||
; off with little 45-degree diagonals. |
|||
; Done this way the vertices don't touch. |
|||
to dragon.chamfer :steps |
|||
localmake "step.len 12 ; length of each step |
|||
localmake "straight.frac 0.5 ; fraction of the step to go straight |
|||
localmake "straight.len :step.len * :straight.frac |
|||
localmake "diagonal.len (:step.len - :straight.len) * sqrt(1/2) |
|||
repeat :steps [ |
|||
localmake "turn (dragon.turn.angle repcount)/2 ; +45 or -45 |
|||
forward :straight.len |
|||
left :turn |
|||
forward :diagonal.len |
|||
left :turn |
|||
] |
|||
end |
|||
dragon.chamfer 256</lang> |
|||
=={{header|Lua}}== |
=={{header|Lua}}== |