# Copyright 2013, 2014 Kevin Ryde
# This file 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.
#
# This file 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 this file. If not, see .
# Usage: gri -output tempfile.ps dragon-iterative.gri
# gv tempfile.ps
#
# This is an iterative implementation following the turn sequence bit
# above lowest zero.
#
`Bit above lowest zero &.n.'
Change .n. to 0 or 1 which is 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 \.word4. 2 remainder}
\.word4. = {rpn \.word4. 1 - 2 /}
end while
\.word4. = {rpn \.word4. 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.
# turn right or left according to bit above lowest 0 of .i.
.tmp. = .i.
Bit above lowest zero &.tmp.
if .tmp.
# (dx,dy) = (dy,-dx) turn right +90 degrees
.tmp. = .dy.
.dy. = {rpn 0 .dx. -}
.dx. = .tmp.
else
# (dx,dy) = (-dy,dx) turn left +90 degrees
.tmp. = .dx.
.dx. = {rpn 0 .dy. -}
.dy. = .tmp.
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 label "start" at -.5 {rpn 0 yusertocm ..fontsize.. pttocm - ycmtouser}
Draw Dragon length 256