Peano curve: Difference between revisions

19,625 bytes added ,  29 days ago
→‎{{header|Fōrmulæ}}: added Peano-Meander variant by L-system
m (→‎{{header|Phix}}: added syntax colouring, made p2js compatible)
(→‎{{header|Fōrmulæ}}: added Peano-Meander variant by L-system)
(30 intermediate revisions by 16 users not shown)
Line 4:
 
Produce a graphical or ASCII-art representation of a [[wp:Peano curve|Peano curve]] of at least order 3.
 
=={{header|Action!}}==
Action! language does not support recursion. Therefore an iterative approach with a stack has been proposed.
<syntaxhighlight lang="action!">DEFINE MAXSIZE="12"
 
INT ARRAY
angleStack(MAXSIZE)
BYTE ARRAY
depthStack(MAXSIZE),stageStack(MAXSIZE)
BYTE stacksize=[0]
 
BYTE FUNC IsEmpty()
IF stacksize=0 THEN RETURN (1) FI
RETURN (0)
 
BYTE FUNC IsFull()
IF stacksize=MAXSIZE THEN RETURN (1) FI
RETURN (0)
 
PROC Push(INT angle BYTE depth,stage)
IF IsFull() THEN Break() FI
angleStack(stacksize)=angle
depthStack(stacksize)=depth
stageStack(stackSize)=stage
stacksize==+1
RETURN
 
PROC Pop(INT POINTER angle BYTE POINTER depth,stage)
IF IsEmpty() THEN Break() FI
stacksize==-1
angle^=angleStack(stacksize)
depth^=depthStack(stacksize)
stage^=stageStack(stacksize)
RETURN
 
INT FUNC Sin(INT a)
WHILE a<0 DO a==+360 OD
WHILE a>360 DO a==-360 OD
IF a=90 THEN
RETURN (1)
ELSEIF a=270 THEN
RETURN (-1)
FI
RETURN (0)
 
INT FUNC Cos(INT a)
RETURN (Sin(a-90))
 
PROC DrawPeano(INT x BYTE y,len BYTE depth)
BYTE stage
INT angle=[90],a
Plot(x,y)
Push(90,depth,0)
 
WHILE IsEmpty()=0
DO
Pop(@a,@depth,@stage)
IF stage<3 THEN
Push(a,depth,stage+1)
FI
IF stage=0 THEN
angle==+a
IF depth>1 THEN
Push(-a,depth-1,0)
FI
ELSEIF stage=1 THEN
x==+len*Cos(angle)
y==-len*Sin(angle)
DrawTo(x,y)
IF depth>1 THEN
Push(a,depth-1,0)
FI
ELSEIF stage=2 THEN
x==+len*Cos(angle)
y==-len*Sin(angle)
DrawTo(x,y)
IF depth>1 THEN
Push(-a,depth-1,0)
FI
ELSEIF stage=3 THEN
angle==-a
FI
OD
RETURN
 
PROC Main()
BYTE CH=$02FC,COLOR1=$02C5,COLOR2=$02C6
 
Graphics(8+16)
Color=1
COLOR1=$0C
COLOR2=$02
 
DrawPeano(69,186,7,6)
 
DO UNTIL CH#$FF OD
CH=$FF
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Peano_curve.png Screenshot from Atari 8-bit computer]
 
=={{header|Ada}}==
{{trans|C}}
{{libheader|APDF}}
<syntaxhighlight lang="ada">with PDF_Out; use PDF_Out;
 
procedure Peano_Curve is
 
Filename : constant String := "peano-curve.pdf";
Order : constant Positive := 4;
Scale : constant Real := 2.1;
Line_Width : constant Real := 2.5;
Corner : constant Point := (150.0, 50.0);
Background : constant Color_Type := (0.827, 0.816, 0.016);
Frame : constant Rectangle := (10.0, 10.0, 820.0, 575.0);
PDF : PDF_Out_File;
 
type Coord is record
X, Y : Natural;
end record;
 
function "+" (Left : Coord; Right : Coord) return Coord
is ((Left.X + Right.X, Left.Y + Right.Y));
 
function "*" (Left : Natural; Right : Coord) return Coord
is ((Left * Right.X, Left * Right.Y));
 
procedure Peano (Pos : Coord; Length : Positive; I1, I2 : Integer) is
Len : constant Integer := Length / 3;
begin
if Length = 1 then
PDF.Line (Corner + Scale * (Real (3 * Pos.X), Real (3 * Pos.Y)));
else
Peano (Pos + Len * (2 * I1, 2 * I1), Len, I1, I2);
Peano (Pos + Len * (I1 - I2 + 1, I1 + I2), Len, I1, 1 - I2);
Peano (Pos + Len * (1, 1), Len, I1, 1 - I2);
Peano (Pos + Len * (I1 + I2, I1 - I2 + 1), Len, 1 - I1, 1 - I2);
Peano (Pos + Len * (2 * I2, 2 - 2 * I2), Len, I1, I2);
Peano (Pos + Len * (1 + I2 - I1, 2 - I1 - I2), Len, I1, I2);
Peano (Pos + Len * (2 - 2 * I1, 2 - 2 * I1), Len, I1, I2);
Peano (Pos + Len * (2 - I1 - I2, 1 + I2 - I1), Len, 1 - I1, I2);
Peano (Pos + Len * (2 - 2 * I2, 2 * I2), Len, 1 - I1, I2);
end if;
end Peano;
 
procedure Draw_Peano is
begin
PDF.Stroking_Color (Black);
PDF.Line_Width (Line_Width);
PDF.Move (Corner);
Peano ((0, 0), 3**Order, 0, 0);
PDF.Finish_Path (Close_Path => False,
Rendering => Stroke,
Rule => Nonzero_Winding_Number);
end Draw_Peano;
 
begin
PDF.Create (Filename);
PDF.Page_Setup (A4_Landscape);
 
PDF.Color (Background);
PDF.Draw (Frame, Fill);
 
Draw_Peano;
PDF.Close;
end Peano_Curve;</syntaxhighlight>
 
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-l-system}}
Generates an SVG file containing the curve using the L-System. Very similar to the Algol 68 Sierpinski square curve sample. Note the Algol 68 L-System library source code is on a separate page on Rosetta Code - follow the above link and then to the Talk page.<syntaxhighlight lang="algol68">
BEGIN # Peano Curve in SVG #
# uses the RC Algol 68 L-System library for the L-System evaluation & #
# interpretation #
 
PR read "lsystem.incl.a68" PR # include L-System utilities #
 
PROC peano curve = ( STRING fname, INT size, length, order, init x, init y )VOID:
IF FILE svg file;
BOOL open error := IF open( svg file, fname, stand out channel ) = 0
THEN
# opened OK - file already exists and #
# will be overwritten #
FALSE
ELSE
# failed to open the file #
# - try creating a new file #
establish( svg file, fname, stand out channel ) /= 0
FI;
open error
THEN # failed to open the file #
print( ( "Unable to open ", fname, newline ) );
stop
ELSE # file opened OK #
 
REAL x := init x;
REAL y := init y;
INT angle := 90;
put( svg file, ( "<svg xmlns='http://www.w3.org/2000/svg' width='"
, whole( size, 0 ), "' height='", whole( size, 0 ), "'>"
, newline, "<rect width='100%' height='100%' fill='white'/>"
, newline, "<path stroke-width='1' stroke='black' fill='none' d='"
, newline, "M", whole( x, 0 ), ",", whole( y, 0 ), newline
)
);
 
LSYSTEM ssc = ( "L"
, ( "L" -> "LFRFL-F-RFLFR+F+LFRFL"
, "R" -> "RFLFR+F+LFRFL-F-RFLFR"
)
);
STRING curve = ssc EVAL order;
curve INTERPRET ( ( CHAR c )VOID:
IF c = "F" THEN
x +:= length * cos( angle * pi / 180 );
y +:= length * sin( angle * pi / 180 );
put( svg file, ( " L", whole( x, 0 ), ",", whole( y, 0 ), newline ) )
ELIF c = "+" THEN
angle +:= 90 MODAB 360
ELIF c = "-" THEN
angle -:= 90 MODAB 360
FI
);
put( svg file, ( "'/>", newline, "</svg>", newline ) );
close( svg file )
FI # sierpinski square # ;
 
peano curve( "peano.svg", 1200, 12, 3, 50, 50 )
 
END
</syntaxhighlight>
 
=={{header|AutoHotkey}}==
{{trans|C}}
Requires [https://www.autohotkey.com/boards/viewtopic.php?t=6517 Gdip Library]
<langsyntaxhighlight AutoHotkeylang="autohotkey">gdip1()
PeanoX := A_ScreenWidth/2 - 100, PeanoY := A_ScreenHeight/2 - 100
Peano(PeanoX, PeanoY, 3**3, 5, 5, Arr:=[])
Line 80 ⟶ 312:
ExitApp
Return
</syntaxhighlight>
</lang>
 
=={{header|C}}==
Adaptation of the C program in the [https://www.researchgate.net/profile/Christoph_Schierz2/publication/228982573_A_recursive_algorithm_for_the_generation_of_space-filling_curves/links/0912f505c2f419782c000000/A-recursive-algorithm-for-the-generation-of-space-filling-curves.pdf Breinholt-Schierz paper] , requires the [http://www.cs.colorado.edu/~main/bgi/cs1300/ WinBGIm] library.
<syntaxhighlight lang="c">
<lang C>
/*Abhishek Ghosh, 14th September 2018*/
 
Line 120 ⟶ 352:
return 0;
}
</syntaxhighlight>
</lang>
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <cmath>
#include <fstream>
#include <iostream>
Line 208 ⟶ 440:
pc.write(out, 656, 8, 4);
return 0;
}</langsyntaxhighlight>
 
{{out}}
[[Media:Peano_curve_cpp.svg]]
See: [https://slack-files.com/T0CNUL56D-F01GKJNB79U-4cdfc7bd42 peano_curve.svg] (offsite SVG image)
 
=={{header|EasyLang}}==
[https://easylang.online/show/#cod=jZK9boMwFIV3P8WRhbogrKQ/QwavnphYIwYXnMaqY5BNArx95YAJoR06INnnfNzje3Vb11QwfvRqaGHUTRkwyEE3lwTuapRPjiUYAXBqHAy6ZqKCAkDaBByUztfAVAm0he9cdZbOz7Vmf0Y0OPbwnWrxOlW0S9iDBKBPUdclOKrkyQVCGF8QpNiXW+LTKfmN/ZPMyF/H0MvLKiRavpdtHIm0d5cRRto4udrJPvoDRkj7BTb9bbRVva67M3bsLQiX5qYCRP4xLH2a2qOCPh45IOWoGo9au4c6BtVr+6yG9BgGQJmlYraqWGuHjIdX/+bSDZeuORYnMffOQXNKlqXhOAYBNBeFyDORFSIXRSrS+52CFuFba5GhKElcyfftMpLtyD9w2OGwI+QH Run it]
 
<syntaxhighlight>
proc lsysexp level . axiom$ rules$[] .
for l to level
an$ = ""
for c$ in strchars axiom$
for i = 1 step 2 to len rules$[]
if rules$[i] = c$
c$ = rules$[i + 1]
break 1
.
.
an$ &= c$
.
swap axiom$ an$
.
.
proc lsysdraw axiom$ x y ang . .
linewidth 0.3
move x y
for c$ in strchars axiom$
if c$ = "F"
x += cos dir
y += sin dir
line x y
elif c$ = "-"
dir -= ang
elif c$ = "+"
dir += ang
.
.
.
axiom$ = "L"
rules$[] = [ "L" "LFRFL-F-RFLFR+F+LFRFL" "R" "RFLFR+F+LFRFL-F-RFLFR" ]
lsysexp 4 axiom$ rules$[]
lsysdraw axiom$ 5 90 90
</syntaxhighlight>
 
=={{header|Factor}}==
{{works with|Factor|0.99 2020-08-14}}
<langsyntaxhighlight lang="factor">USING: accessors L-system ui ;
 
: peano ( L-system -- L-system )
Line 226 ⟶ 498:
} >>rules ;
 
[ <L-system> peano "Peano curve" open-window ] with-ui</langsyntaxhighlight>
 
[[File:Peano.png|thumb]]
 
When using the L-system visualizer, the following controls apply:
Line 261 ⟶ 534:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Peano_curve}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
'''Solution'''
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
=== Peano-Meander variant, by recursion ===
In '''[https://formulae.org/?example=Peano_curve this]''' page you can see the program(s) related to this task and their results.
 
[[File:Fōrmulæ - Peano curve 01.png]]
 
[[File:Fōrmulæ - Peano curve 02.png]]
 
[[File:Fōrmulæ - Peano curve 03.png]]
 
'''Test cases'''
 
=== Peano-Meander variant, by L-system ===
 
There are generic functions written in Fōrmulæ to compute an L-system in the page [[L-system#Fōrmulæ | L-system]].
 
The program that creates a Peano-Meander curve is:
 
[[File:Fōrmulæ - L-system - Peano-Meander curve 01.png]]
 
[[File:Fōrmulæ - L-system - Peano-Meander curve 02.png]]
 
=== Switch-back variant, by L-system ===
 
The program that creates a Switch-back Peano curve is:
 
[[File:Fōrmulæ - L-system - Peano Curve 01.png]]
 
[[File:Fōrmulæ - L-system - Peano Curve 02.png]]
 
=={{header|FreeBASIC}}==
{{trans|Yabasic}}
<langsyntaxhighlight lang="freebasic">Const anchura = 243 'una potencia de 3 para una curva uniformemente espaciada
 
Screenres 700,700
Line 292 ⟶ 591:
Peano(0, 0, anchura, 0, 0)
 
Sleep</langsyntaxhighlight>
 
=={{header|FutureBasic}}==
Couldn't help adding a little bling.
<syntaxhighlight lang="futurebasic">
_window = 1
_curveSize = 7
_curveOrder = 3 * 3 * 3 * 3 // 3^4
 
void local fn BuildWindow
CGRect r = fn CGRectMake( 0, 0, 565, 570 )
window _window, @"Peano Curve In FutureBasic", r, NSWindowStyleMaskTitled
WindowSetBackgroundColor( _window, fn ColorBlack )
end fn
 
local fn Peano( x as long, y as long, lg as long, i1 as long, i2 as long )
ColorRef color = fn ColorRed
if ( lg == 1 ) then line to x * _curveSize, y * _curveSize : exit fn
lg /= 3
select ( rnd(8) )
case 1 : color = fn ColorBrown
case 2 : color = fn ColorRed
case 3 : color = fn ColorOrange
case 4 : color = fn ColorYellow
case 5 : color = fn ColorGreen
case 6 : color = fn ColorBlue
case 7 : color = fn ColorPurple
case 8 : color = fn ColorWhite
end select
pen 2, color
fn Peano( x + 2*i1* lg, y + 2*i1* lg, lg, i1, i2 )
fn Peano( x + (i1-i2+1)*lg, y + (i1+i2)* lg, lg, i1, 1-i2 )
fn Peano( x + lg, y + lg, lg, i1, 1-i2 )
fn Peano( x + (i1+i2)* lg, y + (i1-i2+1)*lg, lg, 1-i1, 1-i2 )
fn Peano( x + 2*i2* lg, y + 2*(1-i2)* lg, lg, i1, i2 )
fn Peano( x + (1+i2-i1)*lg, y + (2-i1-i2)*lg, lg, i1, i2 )
fn Peano( x + 2*(1-i1)* lg, y + 2*(1-i1)* lg, lg, i1, i2 )
fn Peano( x + (2-i1-i2)*lg, y + (1+i2-i1)*lg, lg, 1-i1, i2 )
fn Peano( x + 2*(1-i2)* lg, y + 2*i2* lg, lg, 1-i1, i2 )
end fn
 
randomize
fn BuildWindow
fn Peano( 0, 0, _curveOrder, 0, 0 )
 
HandleEvents
</syntaxhighlight>
{{output}}
[[File:Rosetta_Code_Peano_Curve_rev.png]]
 
=={{header|Go}}==
Line 299 ⟶ 648:
<br>
The following is based on the recursive algorithm and C code in [https://www.researchgate.net/profile/Christoph_Schierz2/publication/228982573_A_recursive_algorithm_for_the_generation_of_space-filling_curves/links/0912f505c2f419782c000000/A-recursive-algorithm-for-the-generation-of-space-filling-curves.pdf this paper] scaled up to 81 x 81 points. The image produced is a variant known as a Peano-Meander curve (see Figure 1(b) [https://www5.in.tum.de/lehre/vorlesungen/asc/ss17/blatt10/ws10.pdf here]).
<langsyntaxhighlight lang="go">package main
 
import "github.com/fogleman/gg"
Line 338 ⟶ 687:
dc.Stroke()
dc.SavePNG("peano.png")
}</langsyntaxhighlight>
 
=={{header|IS-BASIC}}==
<langsyntaxhighlight ISlang="is-BASICbasic">100 PROGRAM "PeanoC.bas"
110 OPTION ANGLE DEGREES
120 SET VIDEO MODE 5:SET VIDEO COLOR 0:SET VIDEO X 40:SET VIDEO Y 27
Line 357 ⟶ 706:
240 CALL PEANO(D,-A,LEV-1)
250 PLOT LEFT A;
260 END DEF</langsyntaxhighlight>
 
=={{header|J}}==
This Hilbert variant is taken directly from the j lab "viewmat".
<syntaxhighlight lang="j">
<pre>
load'viewmat'
hp=: 3 : '(|.,]) 1 (0 _2 _2 ,&.> _2 _1 0 + #y) } (,.|:) y'
Line 367 ⟶ 716:
WR=: 16777215 16711680
viewrgb WR {~ hp ^:7 HP
</syntaxhighlight>
</pre>
 
Or, a smaller [[j:File:Hp1.png|example]] (6 iterations instead of 7) and a different color selection:<syntaxhighlight lang="j"> require 'viewmat'
hp=: 3 : '(|.,]) 1 (0 _2 _2 ,&.> _2 _1 0 + #y) } (,.|:) y'
MG=: 256 #. 128 0 128,:0 192 0
viewrgb 2 ([ # #"1) MG {~ hp ^:6 [ 0, 0 1 0 ,: 0,</syntaxhighlight>
 
=={{header|Java}}==
Output is a file in SVG format.
<langsyntaxhighlight lang="java">import java.io.*;
 
public class PeanoCurve {
Line 460 ⟶ 814:
private int currentAngle;
private static final int ANGLE = 90;
}</langsyntaxhighlight>
 
{{out}}
[[Media:Peano_curve_java.svg]]
See: [https://slack-files.com/T0CNUL56D-F017FM1JKM2-dc8a4b1575 peano_curve.svg] (offsite SVG image)
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
 
This entry includes two distinct solutions. In both cases, the jq program generates
a SVG document, which can be viewed directly in the browser,
at least if the file suffix is ".svg".
 
===Using a Lindenmayer System===
In this section, a Lindenmayer system of rules is used with turtle
graphics.
 
The output converted to a .png file can be viewed at https://imgur.com/gallery/4QbUN7I
====Simple Turtle Graphics====
<syntaxhighlight lang="jq">
# => = 0 degrees
# ^ = 90 degrees
# <= = 180 degrees
# v = 270 degrees
 
# $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 turtleForward($d):
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 "unsupported angle \(.angle)" | error
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 "unsupported angle \(.angle)" | error
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:
path("none"; "red"; "0.1") | svg(100) ;</syntaxhighlight>
 
====Peano Curve====
<syntaxhighlight lang="jq"># Compute the curve using a Lindenmayer system of rules
def rules:
{ L: "LFRFL-F-RFLFR+F+LFRFL",
R: "RFLFR+F+LFRFL-F-RFLFR" } ;
 
def peano($count):
rules as $rules
| def p($count):
if $count <= 0 then .
else gsub("L"; "l") | gsub("R"; $rules["R"]) | gsub("l"; $rules["L"]) | p($count-1)
end;
"L" | p($count) ;
 
def interpret($x):
if $x == "+" then turtleRotate(90)
elif $x == "-" then turtleRotate(-90)
elif $x == "F" then turtleForward(1)
else .
end;
 
def peano_curve($n):
peano($n)
| split("")
| reduce .[] as $action (turtle([1,1]) | turtleDown;
interpret($action) ) ;
 
peano_curve(4)
| draw</syntaxhighlight>
{{out}}
See https://imgur.com/gallery/4QbUN7I
 
===Peano-Meander curve===
'''Adapted from [[#Go]]'''
 
A .png version of the SVG image generated using an invocation
such as the following can be viewed at https://imgur.com/a/RGQr17J
<pre>
jq -nr -f peano-curve.jq > peano-curve.svg
</pre>
 
<syntaxhighlight lang="jq"># Input: an array
# Output: the array augmented with another x,y pair
def peano($x; $y; $lg; $i1; $i2):
$lg as $width
| def p($x; $y; $lg; $i1; $i2):
if $lg == 1
then (($width - $x) * 10) as $px
| (($width - $y) * 10) as $py
| . + [[$px,$py]]
else (($lg/3) | floor) as $lg
| p($x+2*$i1*$lg; $y+2*$i1*$lg; $lg; $i1; $i2)
| p($x+($i1-$i2+1)*$lg; $y+($i1+$i2)*$lg; $lg; $i1; 1-$i2)
| p($x+$lg; $y+$lg; $lg; $i1; 1-$i2)
| p($x+($i1+$i2)*$lg; $y+($i1-$i2+1)*$lg; $lg; 1-$i1; 1-$i2)
| p($x+2*$i2*$lg; $y+2*(1-$i2)*$lg; $lg; $i1; $i2)
| p($x+(1+$i2-$i1)*$lg; $y+(2-$i1-$i2)*$lg; $lg; $i1; $i2)
| p($x+2*(1-$i1)*$lg; $y+2*(1-$i1)*$lg; $lg; $i1; $i2)
| p($x+(2-$i1-$i2)*$lg; $y+(1+$i2-$i1)*$lg; $lg; 1-$i1; $i2)
| p($x+2*(1-$i2)*$lg; $y+2*$i2*$lg; $lg; 1-$i1; $i2)
end;
p($x; $y; $lg; $i1; $i2);
 
def svg:
"<svg viewBox=\"0 0 820 820\" xmlns=\"http://www.w3.org/2000/svg\">" ;
 
def path($fill; $stroke):
"<path fill=\"\($fill)\" stroke=\"\($stroke)\" d=\"M ";
 
def endpath:
" \" /> </svg>";
def peanoCurve:
null | peano(0; 0; 81; 0; 0) | map(join(",")) | join(" ");
 
svg,
( path("none"; "red") + peanoCurve + endpath)
</syntaxhighlight>
{{out}}
https://imgur.com/a/RGQr17J
 
=={{header|Julia}}==
The peano function is from the C version.
<langsyntaxhighlight lang="julia">using Gtk, Graphics, Colors
 
function peano(ctx, x, y, lg, i1, i2)
Line 505 ⟶ 998:
signal_connect(endit, win, :destroy)
wait(cond)
</syntaxhighlight>
</lang>
 
=={{header|Lua}}==
Using the Bitmap class [[Bitmap#Lua|here]], with an ASCII pixel representation, then extending..
<langsyntaxhighlight lang="lua">local PeanoLSystem = {
axiom = "L",
rules = {
Line 558 ⟶ 1,052:
bitmap:clear(" ")
bitmap:drawPath(PeanoLSystem:eval(3), 0, 0, 0, 1)
bitmap:render()</langsyntaxhighlight>
{{out}}
<pre style="font-size:50%;">@ +---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+
Line 616 ⟶ 1,110:
Draw to M2000 console (which has graphic capabilities). Sub is a copy from freebasic, changed *Line* statement to *Draw to*
 
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module Peano_curve {
Cls 1, 0
Line 649 ⟶ 1,143:
}
Peano_curve
</syntaxhighlight>
</lang>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang Mathematica="mathematica">Graphics[PeanoCurve[4]]</langsyntaxhighlight>
{{out}}
Outputs a graphical representation of a 4th order PeanoCurve.
Line 659 ⟶ 1,153:
{{trans|Go}}
{{libheader|imageman}}
<langsyntaxhighlight Nimlang="nim">import imageman
 
const Width = 81
Line 687 ⟶ 1,181:
for i in 1..points.high:
image.drawLine(points[i - 1], points[i], color)
image.savePNG("peano.png", compression = 9)</langsyntaxhighlight>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use SVG;
use List::Util qw(max min);
 
Line 729 ⟶ 1,223:
open $fh, '>', 'peano_curve.svg';
print $fh $svg->xmlify(-namespace=>'svg');
close $fh;</langsyntaxhighlight>
[https://github.com/SqrtNegInf/Rosettacode-Perl5-Smoke/blob/master/ref/peano_curve.svg Peano curve] (offsite image)
 
Line 737 ⟶ 1,231:
You can run this online [http://phix.x10.mx/p2js/peano.htm here].
Space key toggles between switchback and meander curves.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\peano_curve.exw
Line 869 ⟶ 1,363:
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</langsyntaxhighlight>-->
 
=={{header|Processing}}==
{{trans|C}}
<syntaxhighlight lang="java">
//Abhishek Ghosh, 28th June 2022
 
void Peano(int x, int y, int lg, int i1, int i2) {
if (lg == 1) {
ellipse(x,y,1,1);
return;
}
lg = lg/3;
Peano(x+(2*i1*lg), y+(2*i1*lg), lg, i1, i2);
Peano(x+((i1-i2+1)*lg), y+((i1+i2)*lg), lg, i1, 1-i2);
Peano(x+lg, y+lg, lg, i1, 1-i2);
Peano(x+((i1+i2)*lg), y+((i1-i2+1)*lg), lg, 1-i1, 1-i2);
Peano(x+(2*i2*lg), y+(2*(1-i2)*lg), lg, i1, i2);
Peano(x+((1+i2-i1)*lg), y+((2-i1-i2)*lg), lg, i1, i2);
Peano(x+(2*(1-i1)*lg), y+(2*(1-i1)*lg), lg, i1, i2);
Peano(x+((2-i1-i2)*lg), y+((1+i2-i1)*lg), lg, 1-i1, i2);
Peano(x+(2*(1-i2)*lg), y+(2*i2*lg), lg, 1-i1, i2);
}
 
void setup(){
size(1000,1000);
Peano(0, 0, 1000, 0, 0);
}
</syntaxhighlight>
 
=={{header|Prolog}}==
{{works with|SWI Prolog}}
<langsyntaxhighlight lang="prolog">main:-
write_peano_curve('peano_curve.svg', 656, 4).
 
Line 928 ⟶ 1,452:
execute(Stream, X, Y, Length, Angle1, Chars).
execute(Stream, X, Y, Length, Angle, [_|Chars]):-
execute(Stream, X, Y, Length, Angle, Chars).</langsyntaxhighlight>
 
{{out}}
[[Media:Peano_curve_prolog.svg]]
See: [https://slack-files.com/T0CNUL56D-F0179PCAUSF-addac3a9f7 peano_curve.svg] (offsite SVG image)
 
=={{header|Python}}==
Line 938 ⟶ 1,462:
This implementation is, as I think, a variant of the Peano curve (just because it's different in the images). And it's supposed to be like a fullscreen app. And because of scale, the "steps" of the curve will be different in horizontal and vertical (it'll be quite nice if your screen is square :D). If <code>peano(4)</code> (in the last line of code) is runned with the input higher than 8, the void between the steps will be filled with steps, and the hole screen will fill (of course, this depends of your screen size). It's also produces a graphic with the current stack pilled, timed by the current function runned.
 
<langsyntaxhighlight lang="python">
import turtle as tt
import inspect
Line 1,016 ⟶ 1,540:
P.plot(stack)
P.show()
</syntaxhighlight>
</lang>
 
You can see the output image [https://github.com/jlfcosta/Projeto_2019_v2/blob/master/Exerc%C3%ADcios/Curva%20de%20Peano/opahehe1.png <ins>here</ins>] and the output stack graphic [https://github.com/jlfcosta/Projeto_2019_v2/blob/master/Exerc%C3%ADcios/Curva%20de%20Peano/opahehestacks1.png <ins>here</ins>].
Line 1,022 ⟶ 1,546:
=={{header|Quackery}}==
 
<langsyntaxhighlight Quackerylang="quackery"> [ $ "turtleduck.qky" loadfile ] now!
[ stack ] is switch.arg ( --> [ )
Line 1,057 ⟶ 1,581:
char L case [ -1 4 turn ]
char R case [ 1 4 turn ]
otherwise ( ignore ) ] ]</langsyntaxhighlight>
 
{{output}}
 
[[File:Quackery Peano curve.png]]
https://imgur.com/PCi2cSZ
 
=={{header|R}}==
Line 1,067 ⟶ 1,591:
 
 
<langsyntaxhighlight lang="rsplus">
#to install hilbercurve library, biocmanager needs to be installed first
install.packages("BiocManager")
Line 1,080 ⟶ 1,604:
peano = HilbertCurve(1, 512, level = 4, reference = TRUE, arrow = FALSE)
hc_points(peano, x1 = i, np = NULL, pch = 16, size = unit(3, "mm"))
}</langsyntaxhighlight>
 
=={{header|Racket}}==
Line 1,088 ⟶ 1,612:
 
See also https://pdfs.semanticscholar.org/fee6/187cc2dd1679d4976db9522b06a49f63be46.pdf
<syntaxhighlight lang="racket">
<lang Racket>
/* Jens Axel Søgaard, 27th December 2018*/
#lang racket
Line 1,121 ⟶ 1,645:
(peano 6 90 3)
(draw* c))
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2018.06}}
<syntaxhighlight lang="raku" perl6line>use SVG;
 
role Lindenmayer {
Line 1,154 ⟶ 1,678:
:polyline[ :points(@points.join: ','), :fill<black> ],
],
);</langsyntaxhighlight>
 
See: [https://github.com/thundergnat/rc/blob/master/img/peano-perl6.svg Peano curve] (SVG image)
Line 1,164 ⟶ 1,688:
<br/>
Implemented as a Lindenmayer System, depends on JRuby or JRubyComplete see Hilbert for grammar
<langsyntaxhighlight lang="ruby">
load_library :grammar
 
Line 1,249 ⟶ 1,773:
size(800, 800)
end
</syntaxhighlight>
</lang>
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">// [dependencies]
// svg = "0.8.0"
 
Line 1,332 ⟶ 1,856:
fn main() {
PeanoCurve::save("peano_curve.svg", 656, 4).unwrap();
}</langsyntaxhighlight>
 
{{out}}
[[Media:Peano_curve_rust.svg]]
See: [https://slack-files.com/T0CNUL56D-F01749MF7TM-70dda622c5 peano_curve.svg] (offsite SVG image)
 
=={{header|Sidef}}==
Uses the LSystem class defined at [https://rosettacode.org/wiki/Hilbert_curve#Sidef Hilbert curve].
<langsyntaxhighlight lang="ruby">var rules = Hash(
l => 'lFrFl-F-rFlFr+F+lFrFl',
r => 'rFlFr+F+lFrFl-F-rFlFr',
Line 1,356 ⟶ 1,880:
)
 
lsys.execute('l', 4, "peano_curve.png", rules)</langsyntaxhighlight>
Output image: [https://github.com/trizen/rc/blob/master/img/peano_curve-sidef.png Peano curve]
 
=={{header|VBA}}==
{{trans|C}}
<langsyntaxhighlight lang="vb">Const WIDTH = 243 'a power of 3 for a evenly spaced curve
Dim n As Long
Dim points() As Single
Line 1,408 ⟶ 1,932:
Call Peano(0, 0, WIDTH, 0, 0) 'Start Peano recursion to generate and store points
ActiveSheet.Shapes.AddPolyline points 'Excel assumed
End Sub</langsyntaxhighlight>
 
=={{header|VBScript}}==
VBSCript does'nt have access to Windows graphics so I write SVG commands into an HML file and display it using the default browser. A turtle graphics class makes the recursive definition of the curve easy.
<syntaxhighlight lang="vb">
 
option explicit
'outputs turtle graphics to svg file and opens it
 
const pi180= 0.01745329251994329576923690768489 ' pi/180
const pi=3.1415926535897932384626433832795 'pi
class turtle
dim fso
dim fn
dim svg
dim iang 'radians
dim ori 'radians
dim incr
dim pdown
dim clr
dim x
dim y
 
public property let orient(n):ori = n*pi180 :end property
public property let iangle(n):iang= n*pi180 :end property
public sub pd() : pdown=true: end sub
public sub pu() :pdown=FALSE :end sub
public sub rt(i)
ori=ori - i*iang:
if ori<0 then ori = ori+pi*2
end sub
public sub lt(i):
ori=(ori + i*iang)
if ori>(pi*2) then ori=ori-pi*2
end sub
public sub bw(l)
x= x+ cos(ori+pi)*l*incr
y= y+ sin(ori+pi)*l*incr
end sub
public sub fw(l)
dim x1,y1
x1=x + cos(ori)*l*incr
y1=y + sin(ori)*l*incr
if pdown then line x,y,x1,y1
x=x1:y=y1
end sub
Private Sub Class_Initialize()
setlocale "us"
initsvg
pdown=true
end sub
Private Sub Class_Terminate()
disply
end sub
private sub line (x,y,x1,y1)
svg.WriteLine "<line x1=""" & x & """ y1= """& y & """ x2=""" & x1& """ y2=""" & y1 & """/>"
end sub
 
private sub disply()
dim shell
svg.WriteLine "</svg></body></html>"
svg.close
Set shell = CreateObject("Shell.Application")
shell.ShellExecute fn,1,False
end sub
 
private sub initsvg()
dim scriptpath
Set fso = CreateObject ("Scripting.Filesystemobject")
ScriptPath= Left(WScript.ScriptFullName, InStrRev(WScript.ScriptFullName, "\"))
fn=Scriptpath & "SIERP.HTML"
Set svg = fso.CreateTextFile(fn,True)
if SVG IS nothing then wscript.echo "Can't create svg file" :vscript.quit
svg.WriteLine "<!DOCTYPE html>" &vbcrlf & "<html>" &vbcrlf & "<head>"
svg.writeline "<style>" & vbcrlf & "line {stroke:rgb(255,0,0);stroke-width:.5}" &vbcrlf &"</style>"
svg.writeline "</head>"&vbcrlf & "<body>"
svg.WriteLine "<svg xmlns=""http://www.w3.org/2000/svg"" width=""800"" height=""800"" viewBox=""0 0 800 800"">"
end sub
end class
 
sub peano (n,a)
if n=0 then exit sub
x.rt a
peano n-1, -a
x.fw 1
peano n-1, a
x.fw 1
peano n-1, -a
x.lt a
end sub
 
dim x,i
set x=new turtle
x.iangle=90
x.orient=0
x.incr=7
x.x=100:x.y=500
peano 7,1
set x=nothing 'show image in browser
</syntaxhighlight>
 
=={{header|Wren}}==
{{trans|Go}}
{{libheader|DOME}}
<langsyntaxhighlight ecmascriptlang="wren">import "graphics" for Canvas, Color, Point
import "dome" for Window
 
Line 1,456 ⟶ 2,087:
 
static draw(dt) {}
}</langsyntaxhighlight>
 
=={{header|XPL0}}==
{{trans|C}}
<syntaxhighlight lang "XPL0">proc Peano(X, Y, Lg, I1, I2);
int X, Y, Lg, I1, I2;
[if Lg = 1 then
[Line(3*X, 3*Y, 11 \cyan\);
return;
];
Lg:= Lg/3;
Peano(X + 2*I1* Lg, Y + 2*I1* Lg, Lg, I1, I2);
Peano(X + (I1-I2+1)*Lg, Y + (I1+I2)* Lg, Lg, I1, 1-I2);
Peano(X + Lg, Y + Lg, Lg, I1, 1-I2);
Peano(X + (I1+I2)* Lg, Y + (I1-I2+1)*Lg, Lg, 1-I1, 1-I2);
Peano(X + 2*I2* Lg, Y + 2*(1-I2)* Lg, Lg, I1, I2);
Peano(X + (1+I2-I1)*Lg, Y + (2-I1-I2)*Lg, Lg, I1, I2);
Peano(X + 2*(1-I1)* Lg, Y + 2*(1-I1)* Lg, Lg, I1, I2);
Peano(X + (2-I1-I2)*Lg, Y + (1+I2-I1)*Lg, Lg, 1-I1, I2);
Peano(X + 2*(1-I2)* Lg, Y + 2*I2* Lg, Lg, 1-I1, I2);
];
 
[SetVid($13);
Peano(0, 0, 3*3*3*3, 0, 0); \start Peano recursion
]</syntaxhighlight>
{{out}}
[[File:PianoXPL0.gif]]
 
=={{header|Yabasic}}==
{{trans|VBA}}
<langsyntaxhighlight Yabasiclang="yabasic">WIDTH = 243 //a power of 3 for a evenly spaced curve
 
open window 700, 700
Line 1,481 ⟶ 2,138:
Peano(x + ((2 - i1 - i2) * lg), y + ((1 + i2 - i1) * lg), lg, 1 - i1, i2)
Peano(x + (2 * (1 - i2) * lg), y + (2 * i2 * lg), lg, 1 - i1, i2)
End Sub</langsyntaxhighlight>
 
=={{header|zkl}}==
Using a Lindenmayer system and turtle graphics & turned 90°:
<langsyntaxhighlight lang="zkl">lsystem("L", // axiom
Dictionary("L","LFRFL-F-RFLFR+F+LFRFL", "R","RFLFR+F+LFRFL-F-RFLFR"), # rules
"+-F", 4) // constants, order
Line 1,498 ⟶ 2,155:
}
buf1.text // n=4 --> 16,401 characters
}</langsyntaxhighlight>
Using Image Magick and
the PPM class from http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#zkl
<langsyntaxhighlight 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
Line 1,517 ⟶ 2,174:
}
img.writeJPGFile("peanoCurve.zkl.jpg");
}</langsyntaxhighlight>
{{out}}
Image at [http://www.zenkinetic.com/Images/RosettaCode/peanoCurve.zkl.jpg Peano curve]
2,120

edits