Peano curve: Difference between revisions
m (→{{header|Perl 6}}: added zkl header) |
(→{{header|zkl}}: added code) |
||
Line 40: | Line 40: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
Using a Lindenmayer system and turtle graphics & turned 90°: |
|||
⚫ | |||
<lang zkl> |
<lang zkl>lsystem("L", // axiom |
||
Dictionary("L","LFRFL-F-RFLFR+F+LFRFL", "R","RFLFR+F+LFRFL-F-RFLFR"), # rules |
|||
"+-F", 4) // constants, order |
|||
: turtle(_); |
|||
fcn lsystem(axiom,rules,consts,n){ // Lindenmayer system --> string |
|||
foreach k in (consts){ rules.add(k,k) } |
|||
buf1,buf2 := Data(Void,axiom).howza(3), Data().howza(3); // characters |
|||
do(n){ |
|||
buf1.pump(buf2.clear(), rules.get); |
|||
t:=buf1; buf1=buf2; buf2=t; // swap buffers |
|||
} |
|||
buf1.text // n=4 --> 16,401 characters |
|||
⚫ | |||
Using Image Magick and |
|||
the PPM class from http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#zkl |
|||
<lang zkl>fcn turtle(koch){ |
|||
const D=10.0; |
|||
dir,angle, x,y := 0.0, (90.0).toRad(), 20.0, 830.0; // turtle; x,y are float |
|||
img,color := PPM(850,850), 0x00ff00; |
|||
foreach c in (koch){ |
|||
switch(c){ |
|||
case("F"){ // draw forward |
|||
dx,dy := D.toRectangular(dir); |
|||
tx,ty := x,y; x,y = (x+dx),(y+dy); |
|||
img.line(tx.toInt(),ty.toInt(), x.toInt(),y.toInt(), color); |
|||
} |
|||
case("-"){ dir-=angle } // turn right |
|||
case("+"){ dir+=angle } // turn left |
|||
} |
|||
} |
|||
img.writeJPGFile("peanoCurve.zkl.jpg"); |
|||
}</lang> |
|||
{{out}} |
{{out}} |
||
Image at [http://www.zenkinetic.com/Images/RosettaCode/peanoCurve.zkl.jpg Peano curve] |
|||
<pre> |
|||
</pre> |
Revision as of 18:25, 19 July 2018
- Task
Produce a graphical or ASCII-art representation of a Peano curve of at least order 3.
Perl 6
<lang perl6>use SVG;
role Lindenmayer {
has %.rules; method succ { self.comb.map( { %!rules{$^c} // $c } ).join but Lindenmayer(%!rules) }
}
my $peano = 'L' but Lindenmayer( { 'L' => 'LFRFL-F-RFLFR+F+LFRFL', 'R' => 'RFLFR+F+LFRFL-F-RFLFR' } );
$peano++ xx 4; my @points = (10, 10);
for $peano.comb {
state ($x, $y) = @points[0,1]; state $d = 0 + 8i; when 'F' { @points.append: ($x += $d.re).round(1), ($y += $d.im).round(1) } when /< + - >/ { $d *= "{$_}1i" } default { }
}
say SVG.serialize(
svg => [ :660width, :660height, :style<stroke:lime>, :rect[:width<100%>, :height<100%>, :fill<black>], :polyline[ :points(@points.join: ','), :fill<black> ], ],
);</lang>
See: Peano curve (SVG image)
zkl
Using a Lindenmayer system and turtle graphics & turned 90°: <lang zkl>lsystem("L", // axiom
Dictionary("L","LFRFL-F-RFLFR+F+LFRFL", "R","RFLFR+F+LFRFL-F-RFLFR"), # rules "+-F", 4) // constants, order
- turtle(_);
fcn lsystem(axiom,rules,consts,n){ // Lindenmayer system --> string
foreach k in (consts){ rules.add(k,k) } buf1,buf2 := Data(Void,axiom).howza(3), Data().howza(3); // characters do(n){ buf1.pump(buf2.clear(), rules.get); t:=buf1; buf1=buf2; buf2=t; // swap buffers } buf1.text // n=4 --> 16,401 characters
}</lang> Using Image Magick and the PPM class from http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#zkl <lang zkl>fcn turtle(koch){
const D=10.0; dir,angle, x,y := 0.0, (90.0).toRad(), 20.0, 830.0; // turtle; x,y are float img,color := PPM(850,850), 0x00ff00; foreach c in (koch){ switch(c){
case("F"){ // draw forward dx,dy := D.toRectangular(dir); tx,ty := x,y; x,y = (x+dx),(y+dy); img.line(tx.toInt(),ty.toInt(), x.toInt(),y.toInt(), color); } case("-"){ dir-=angle } // turn right case("+"){ dir+=angle } // turn left
} } img.writeJPGFile("peanoCurve.zkl.jpg");
}</lang>
- Output:
Image at Peano curve