Sierpinski triangle: Difference between revisions
Content added Content deleted
(Added Hoon entry) |
|||
Line 3,510: | Line 3,510: | ||
* * * * * * * * |
* * * * * * * * |
||
* * * * * * * * * * * * * * * *</pre> |
* * * * * * * * * * * * * * * *</pre> |
||
=={{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`. |
|||
See [[Category_talk:Jq-turtle]] for the turtle.jq module used here. |
|||
Please note that the `include` directive below may need to be modified, |
|||
depending on the command-line options used |
|||
and the location of the included file. |
|||
<lang jq>include "turtle" {search: "."}; |
|||
# Compute the curve using a Lindenmayer system of rules |
|||
def rules: |
|||
# "H" signfies Horizontal motion |
|||
{X: "XX", |
|||
H: "H--X++H++X--H", |
|||
"": "H--X--X"}; |
|||
def sierpinski($count): |
|||
rules as $rules |
|||
| def repeat($count): |
|||
if $count == 0 then . |
|||
else gsub("X"; $rules["X"]) | gsub("H"; $rules["H"]) |
|||
| repeat($count-1) |
|||
end; |
|||
$rules[""] | repeat($count) ; |
|||
def interpret($x): |
|||
if $x == "+" then turtleRotate(-60) |
|||
elif $x == "-" then turtleRotate(60) |
|||
else turtleForward(20) |
|||
end; |
|||
def sierpinski_curve($n): |
|||
sierpinski($n) |
|||
| split("") |
|||
| reduce .[] as $action ( |
|||
turtle([200,-200]) | turtleDown; |
|||
interpret($action) ) ; |
|||
# viewBox = <min-x> <min-y> <width> <height> |
|||
# Input: {svg, minx, miny, maxx, maxy} |
|||
def svg: |
|||
"<svg viewBox='\(.minx|floor) \(.miny - 4 |floor) \(.maxx - .minx|ceil) \(6 + .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}}== |