Sierpinski curve: Difference between revisions
Content added Content deleted
Line 694: | Line 694: | ||
See: [https://slack-files.com/T0CNUL56D-F016J6Q8W78-4a6e0291c9 sierpinski_curve.svg] (offsite SVG image) |
See: [https://slack-files.com/T0CNUL56D-F016J6Q8W78-4a6e0291c9 sierpinski_curve.svg] (offsite SVG image) |
||
=={{header|jq}}== |
|||
{{works with|jq}} |
|||
'''Works with gojq, the Go implementation of jq''' |
|||
This entry uses an L-system and turtle graphics to generate an SVG |
|||
file which can be viewed using a web browser, at least if the file type is `.svg`. The SVG viewBox is dynamically sized. |
|||
See [[Category_talk:Jq-turtle]] for the turtle.jq module used here. |
|||
Please note that the `include` directive may need to be modified |
|||
depending on the location of the included file, and the command-line |
|||
options used. |
|||
<lang jq>include "turtle" {search: "."}; |
|||
# Compute the curve using a Lindemayer system of rules |
|||
def rules: |
|||
{ X: "XF+G+XF--F--XF+G+X", |
|||
"": "F--XF--F--XF" }; |
|||
def sierpinski($count): |
|||
rules as $rules |
|||
| def p($count): |
|||
if $count == 0 then . |
|||
else gsub("X"; $rules["X"]) | p($count-1) |
|||
end; |
|||
$rules[""] | p($count) ; |
|||
def interpret($x): |
|||
if $x == "+" then turtleRotate(45) |
|||
elif $x == "-" then turtleRotate(-45) |
|||
elif $x == "F" or $x == "G" then turtleForward(5) |
|||
else . |
|||
end; |
|||
def sierpinski_curve($n): |
|||
sierpinski($n) |
|||
| split("") |
|||
| reduce .[] as $action ( |
|||
turtle([100,100]) | turtleDown; |
|||
interpret($action) ) ; |
|||
# viewBox = <min-x> <min-y> <width> <height> |
|||
# Input: {svg, minx, miny, maxx, maxy} |
|||
def svg: |
|||
"<svg viewBox='\(.minx|floor) \(.miny - 2 |floor) \(.maxx - .minx|ceil) \(2 + .maxy - .miny|ceil)'", |
|||
" preserveAspectRatio='xMinYmin meet'", |
|||
" xmlns='http://www.w3.org/2000/svg' >", |
|||
path("none"; "red"; 1), |
|||
"</svg>"; |
|||
sierpinski_curve(5) |
|||
| svg |
|||
</lang> |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
===Turtle procedural (lineto) version=== |
===Turtle procedural (lineto) version=== |