# Copyright 2013 Kevin Ryde
# This file is part of Math-PlanePath.
#
# Math-PlanePath is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 3, or (at your option) any later
# version.
#
# Math-PlanePath is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with Math-PlanePath. If not, see <http://www.gnu.org/licenses/>.
`bit_above_lowest_zero &.n.'
Change .n. to 0 or 1 being the bit above the least significant 0 bit of .n.
If .n. is 0 then the result is 0 since the low bit is 0 and above that also 0.
{
while {rpn \.word1. 2 remainder}
\.word1. = {rpn \.word1. 1 - 2 /}
end while
\.word1. = {rpn \.word1. 2 / 2 remainder}
}
`Draw Dragon length .length.'
Draw a dragon curve starting at 0,0 with .length. many segments, each
segment being 1 long.
{
new .length. .i. .tmp. .x. .y. .dx. .dy.
.length. = \.word3.
.x. = 0
.y. = 0
.dx. = 1
.dy. = 0
# loop i=0 to i=length inclusive
.i. = 0
while {rpn .i. .length. >=}
draw line from .x. .y. to {rpn .x. .dx. +} {rpn .y. .dy. +}
.x. += .dx.
.y. += .dy.
# (dx,dy) = (-dy,dx) turn left +90 degrees
.tmp. = .dx.
.dx. = {rpn 0 .dy. -}
.dy. = .tmp.
# if bit above lowest 0 then (dx,dy) = (-dx,-dy) rotate 180 so
# turn is right -90 degrees instead
.tmp. = .i.
bit_above_lowest_zero &.tmp.
if .tmp.
.dx. = {rpn 0 .dx. -}
.dy. = {rpn 0 .dy. -}
end if
.i. += 1
end while
delete .length. .i. .tmp. .x. .y. .dx. .dy.
}
set x axis -12 24 4
set y axis -18 18 4
Draw Dragon length 256
# show .i. " " .bit.