Peano curve: Difference between revisions
Content deleted Content added
(22 intermediate revisions by 11 users not shown) | |||
Line 7:
=={{header|Action!}}==
Action! language does not support recursion. Therefore an iterative approach with a stack has been proposed.
<
INT ARRAY
Line 102:
DO UNTIL CH#$FF OD
CH=$FF
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Peano_curve.png Screenshot from Atari 8-bit computer]
Line 109:
{{trans|C}}
{{libheader|APDF}}
<
procedure Peano_Curve is
Line 170:
Draw_Peano;
PDF.Close;
end Peano_Curve;</
=={{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]
<
PeanoX := A_ScreenWidth/2 - 100, PeanoY := A_ScreenHeight/2 - 100
Peano(PeanoX, PeanoY, 3**3, 5, 5, Arr:=[])
Line 247 ⟶ 312:
ExitApp
Return
</syntaxhighlight>
=={{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">
/*Abhishek Ghosh, 14th September 2018*/
Line 287 ⟶ 352:
return 0;
}
</syntaxhighlight>
=={{header|C++}}==
<
#include <fstream>
#include <iostream>
Line 375 ⟶ 440:
pc.write(out, 656, 8, 4);
return 0;
}</
{{out}}
[[Media:Peano_curve_cpp.svg]]
=={{header|EasyLang}}==
[https://easylang.online/show/#cod=jZK9boMwFIV3P8WRhbogrKQ/QwavnphYIwYXnMaqY5BNArx95YAJoR06INnnfNzje3Vb11QwfvRqaGHUTRkwyEE3lwTuapRPjiUYAXBqHAy6ZqKCAkDaBByUztfAVAm0he9cdZbOz7Vmf0Y0OPbwnWrxOlW0S9iDBKBPUdclOKrkyQVCGF8QpNiXW+LTKfmN/ZPMyF/H0MvLKiRavpdtHIm0d5cRRto4udrJPvoDRkj7BTb9bbRVva67M3bsLQiX5qYCRP4xLH2a2qOCPh45IOWoGo9au4c6BtVr+6yG9BgGQJmlYraqWGuHjIdX/+bSDZeuORYnMffOQXNKlqXhOAYBNBeFyDORFSIXRSrS+52CFuFba5GhKElcyfftMpLtyD9w2OGwI+QH Run it]
<syntaxhighlight lang=text>
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}}
<
: peano ( L-system -- L-system )
Line 393 ⟶ 498:
} >>rules ;
[ <L-system> peano "Peano curve" open-window ] with-ui</
[[File:Peano.png|thumb]]
When using the L-system visualizer, the following controls apply:
Line 428 ⟶ 534:
=={{header|Fōrmulæ}}==
{{FormulaeEntry|page=https://formulae.org/?script=examples/Peano_curve}}
'''Solution'''
=== Peano-Meander variant, by recursion ===
[[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}}
<
Screenres 700,700
Line 459 ⟶ 591:
Peano(0, 0, anchura, 0, 0)
Sleep</
=={{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 466 ⟶ 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]).
<
import "github.com/fogleman/gg"
Line 505 ⟶ 687:
dc.Stroke()
dc.SavePNG("peano.png")
}</
=={{header|IS-BASIC}}==
<
110 OPTION ANGLE DEGREES
120 SET VIDEO MODE 5:SET VIDEO COLOR 0:SET VIDEO X 40:SET VIDEO Y 27
Line 524 ⟶ 706:
240 CALL PEANO(D,-A,LEV-1)
250 PLOT LEFT A;
260 END DEF</
=={{header|J}}==
This Hilbert variant is taken directly from the j lab "viewmat".
<syntaxhighlight lang="j">
load'viewmat'
hp=: 3 : '(|.,]) 1 (0 _2 _2 ,&.> _2 _1 0 + #y) } (,.|:) y'
Line 534 ⟶ 716:
WR=: 16777215 16711680
viewrgb WR {~ hp ^:7 HP
</syntaxhighlight>
Or, a smaller [[j:File:Hp1.png|example]] (6 iterations instead of 7) and a different color selection:<
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,</
=={{header|Java}}==
Output is a file in SVG format.
<
public class PeanoCurve {
Line 632 ⟶ 814:
private int currentAngle;
private static final int ANGLE = 90;
}</
{{out}}
[[Media:Peano_curve_java.svg]]
=={{header|jq}}==
Line 651 ⟶ 833:
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
Line 693 ⟶ 875:
def draw:
path("none"; "red"; "0.1") | svg(100) ;</
====Peano Curve====
<
def rules:
{ L: "LFRFL-F-RFLFR+F+LFRFL",
Line 723 ⟶ 905:
peano_curve(4)
| draw</
{{out}}
See https://imgur.com/gallery/4QbUN7I
Line 736 ⟶ 918:
</pre>
<
# Output: the array augmented with another x,y pair
def peano($x; $y; $lg; $i1; $i2):
Line 772 ⟶ 954:
svg,
( path("none"; "red") + peanoCurve + endpath)
</syntaxhighlight>
{{out}}
https://imgur.com/a/RGQr17J
Line 778 ⟶ 960:
=={{header|Julia}}==
The peano function is from the C version.
<
function peano(ctx, x, y, lg, i1, i2)
Line 816 ⟶ 998:
signal_connect(endit, win, :destroy)
wait(cond)
</syntaxhighlight>
=={{header|Lua}}==
Using the Bitmap class [[Bitmap#Lua|here]], with an ASCII pixel representation, then extending..
<
axiom = "L",
rules = {
Line 870 ⟶ 1,052:
bitmap:clear(" ")
bitmap:drawPath(PeanoLSystem:eval(3), 0, 0, 0, 1)
bitmap:render()</
{{out}}
<pre style="font-size:50%;">@ +---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+
Line 928 ⟶ 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">
Module Peano_curve {
Cls 1, 0
Line 961 ⟶ 1,143:
}
Peano_curve
</syntaxhighlight>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang
{{out}}
Outputs a graphical representation of a 4th order PeanoCurve.
Line 971 ⟶ 1,153:
{{trans|Go}}
{{libheader|imageman}}
<
const Width = 81
Line 999 ⟶ 1,181:
for i in 1..points.high:
image.drawLine(points[i - 1], points[i], color)
image.savePNG("peano.png", compression = 9)</
=={{header|Perl}}==
<
use List::Util qw(max min);
Line 1,041 ⟶ 1,223:
open $fh, '>', 'peano_curve.svg';
print $fh $svg->xmlify(-namespace=>'svg');
close $fh;</
[https://github.com/SqrtNegInf/Rosettacode-Perl5-Smoke/blob/master/ref/peano_curve.svg Peano curve] (offsite image)
Line 1,049 ⟶ 1,231:
You can run this online [http://phix.x10.mx/p2js/peano.htm here].
Space key toggles between switchback and meander curves.
<!--<
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\peano_curve.exw
Line 1,181 ⟶ 1,363:
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</
=={{header|Processing}}==
{{trans|C}}
<
//Abhishek Ghosh, 28th June 2022
Line 1,211 ⟶ 1,393:
Peano(0, 0, 1000, 0, 0);
}
</syntaxhighlight>
=={{header|Prolog}}==
{{works with|SWI Prolog}}
<
write_peano_curve('peano_curve.svg', 656, 4).
Line 1,270 ⟶ 1,452:
execute(Stream, X, Y, Length, Angle1, Chars).
execute(Stream, X, Y, Length, Angle, [_|Chars]):-
execute(Stream, X, Y, Length, Angle, Chars).</
{{out}}
[[Media:Peano_curve_prolog.svg]]
=={{header|Python}}==
Line 1,280 ⟶ 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.
<
import turtle as tt
import inspect
Line 1,358 ⟶ 1,540:
P.plot(stack)
P.show()
</syntaxhighlight>
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,364 ⟶ 1,546:
=={{header|Quackery}}==
(Method is described at [[L-system#Quackery]].)
<syntaxhighlight lang="quackery"> [ $ "turtleduck.qky" loadfile ] now!
[ stack ] is switch.arg ( --> [ )
Line 1,399 ⟶ 1,583:
char L case [ -1 4 turn ]
char R case [ 1 4 turn ]
otherwise ( ignore ) ] ]</
{{output}}
[[File:Quackery Peano curve.png]]
=={{header|R}}==
Line 1,409 ⟶ 1,593:
<
#to install hilbercurve library, biocmanager needs to be installed first
install.packages("BiocManager")
Line 1,422 ⟶ 1,606:
peano = HilbertCurve(1, 512, level = 4, reference = TRUE, arrow = FALSE)
hc_points(peano, x1 = i, np = NULL, pch = 16, size = unit(3, "mm"))
}</
=={{header|Racket}}==
Line 1,430 ⟶ 1,614:
See also https://pdfs.semanticscholar.org/fee6/187cc2dd1679d4976db9522b06a49f63be46.pdf
<syntaxhighlight lang="racket">
/* Jens Axel Søgaard, 27th December 2018*/
#lang racket
Line 1,463 ⟶ 1,647:
(peano 6 90 3)
(draw* c))
</syntaxhighlight>
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2018.06}}
<syntaxhighlight lang="raku"
role Lindenmayer {
Line 1,496 ⟶ 1,680:
:polyline[ :points(@points.join: ','), :fill<black> ],
],
);</
See: [https://github.com/thundergnat/rc/blob/master/img/peano-perl6.svg Peano curve] (SVG image)
Line 1,506 ⟶ 1,690:
<br/>
Implemented as a Lindenmayer System, depends on JRuby or JRubyComplete see Hilbert for grammar
<
load_library :grammar
Line 1,591 ⟶ 1,775:
size(800, 800)
end
</syntaxhighlight>
=={{header|Rust}}==
<
// svg = "0.8.0"
Line 1,674 ⟶ 1,858:
fn main() {
PeanoCurve::save("peano_curve.svg", 656, 4).unwrap();
}</
{{out}}
[[Media:Peano_curve_rust.svg]]
=={{header|Sidef}}==
Uses the LSystem class defined at [https://rosettacode.org/wiki/Hilbert_curve#Sidef Hilbert curve].
<
l => 'lFrFl-F-rFlFr+F+lFrFl',
r => 'rFlFr+F+lFrFl-F-rFlFr',
Line 1,698 ⟶ 1,882:
)
lsys.execute('l', 4, "peano_curve.png", rules)</
Output image: [https://github.com/trizen/rc/blob/master/img/peano_curve-sidef.png Peano curve]
=={{header|VBA}}==
{{trans|C}}
<
Dim n As Long
Dim points() As Single
Line 1,750 ⟶ 1,934:
Call Peano(0, 0, WIDTH, 0, 0) 'Start Peano recursion to generate and store points
ActiveSheet.Shapes.AddPolyline points 'Excel assumed
End Sub</
=={{header|VBScript}}==
VBSCript does'nt have access to Windows graphics so I write
<syntaxhighlight lang="vb">
option explicit
Line 1,857 ⟶ 2,041:
peano 7,1
set x=nothing 'show image in browser
</syntaxhighlight>
=={{header|Wren}}==
{{trans|Go}}
{{libheader|DOME}}
<
import "dome" for Window
Line 1,905 ⟶ 2,089:
static draw(dt) {}
}</
=={{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}}
<
open window 700, 700
Line 1,930 ⟶ 2,140:
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</
=={{header|zkl}}==
Using a Lindenmayer system and turtle graphics & turned 90°:
<
Dictionary("L","LFRFL-F-RFLFR+F+LFRFL", "R","RFLFR+F+LFRFL-F-RFLFR"), # rules
"+-F", 4) // constants, order
Line 1,947 ⟶ 2,157:
}
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
<
const D=10.0;
dir,angle, x,y := 0.0, (90.0).toRad(), 20.0, 830.0; // turtle; x,y are float
Line 1,966 ⟶ 2,176:
}
img.writeJPGFile("peanoCurve.zkl.jpg");
}</
{{out}}
Image at [http://www.zenkinetic.com/Images/RosettaCode/peanoCurve.zkl.jpg Peano curve]
|