Category talk:Jq-turtle
turtle.jq - turtle graphics with some ancillary SVG convenience functions.
To include these turtle graphics definitions, download the page contents (minus this preamble) into a file and include it using an appropriate `include` directive, e.g.
include "turtle" {search: "."};
# The turtle's state: {svg, up, angle} # Angle: # => = 0 degrees # ^ = 90 degrees # <= = 180 degrees # v = 270 degrees def pi: 4 * (1|atan); def a2radians: 2 * pi * ./360; def cosDegrees: a2radians|cos; def sinDegrees: a2radians|sin; # $start : [$x, $y] def turtle($start): $start | if type == "array" then "M \($start|join(","))" else "M 0,0" end | {svg: ., up:true, angle:0}; def turtleUp: .up=true; def turtleDown: .up=false; def turtleRotate($angle): .angle = (360 + (.angle + $angle)) % 360; def turtleArcRight($r): if .angle== 0 then .svg += " a\($r),\($r) 0 0 1 \($r),\($r)" # checked elif .angle== 90 then .svg += " a\($r),\($r) 0 0 1 \($r),-\($r)" # checked elif .angle==180 then .svg += " a\($r),\($r) 0 0 1 -\($r),-\($r)" # checked elif .angle==270 then .svg += " a\($r),\($r) 0 0 1 -\($r),\($r)" # checked else "turtleArcRight at \(.angle) not yet supported" | error end | turtleRotate(-90); def turtleArcLeft($r): if .angle== 0 then .svg += " a\($r),\($r) 0 0 0 \($r),-\($r)" # check elif .angle== 90 then .svg += " a\($r),\($r) 0 0 0 -\($r),-\($r)" # check elif .angle==180 then .svg += " a\($r),\($r) 0 0 0 -\($r),\($r)" # check elif .angle==270 then .svg += " a\($r),\($r) 0 0 0 \($r),\($r)" else "turtleArcRight at \(.angle) not yet supported" | error end | turtleRotate(90); def turtleForward($d): def rnd: 1000*.|round/1000; if .up then if .angle== 0 then .svg += " m \($d),0" elif .angle== 90 then .svg += " m 0,-\($d)" elif .angle==180 then .svg += " m -\($d),0" elif .angle==270 then .svg += " m 0,\($d)" else ($d * (.angle|cosDegrees)) as $dx | ($d * (.angle|sinDegrees) * -1) as $dy | .svg += " m\($dx),\($dy)" end else if .angle== 0 then .svg += " h \($d)" elif .angle== 90 then .svg += " v -\($d)" elif .angle==180 then .svg += " h -\($d)" elif .angle==270 then .svg += " v \($d)" else ($d * (.angle|cosDegrees|rnd)) as $dx | ($d * (.angle|sinDegrees|rnd) * -1) as $dy | .svg += " l\($dx),\($dy)" end end; def svg($size): "<svg viewBox=\"0 0 \($size) \($size)\" xmlns=\"http://www.w3.org/2000/svg\">", ., "</svg>"; def path($fill; $stroke; $width): "<path fill=\"\($fill)\" stroke=\"\($stroke)\" stroke-width=\"\($width)\" d=\"\(.svg)\" />"; def draw($size): path("none"; "red"; 1) | svg($size); ### def svg($size): "<svg viewBox=\"0 0 \($size) \($size)\" xmlns=\"http://www.w3.org/2000/svg\">", ., "</svg>"; def path($fill; $stroke; $width): "<path fill=\"\($fill)\" stroke=\"\($stroke)\" stroke-width=\"1\" d=\"\(.svg)\" />"; def draw($size): path("none"; "red"; 1) | svg($size) ;