Sierpinski triangle/Graphical: Difference between revisions

m
m (→‎{{header|Quackery}}: fast graphics)
 
(23 intermediate revisions by 8 users not shown)
Line 226:
 
}
</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 # Sierpinski Triangle 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 sierpinski triangle 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 := 0;
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 = ( "F-G-G"
, ( "F" -> "F-G+F+G-F"
, "G" -> "GG"
)
);
STRING curve = ssc EVAL order;
curve INTERPRET ( ( CHAR c )VOID:
IF c = "F" OR c = "G" 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 +:= 120 MODAB 360
ELIF c = "-" THEN
angle -:= 120 MODAB 360
FI
);
put( svg file, ( "'/>", newline, "</svg>", newline ) );
close( svg file )
FI # sierpinski square # ;
 
sierpinski triangle curve( "sierpinski_triangle.svg", 1200, 12, 5, 200, 400 )
 
END
</syntaxhighlight>
 
Line 787 ⟶ 854:
 
SierpinskyTriangle(7, w*0.05, h*0.05, w*0.9, h*0.9)</syntaxhighlight>
 
=={{header|Bruijn}}==
 
Rendered using [https://lambda-screen.marvinborner.de/ lambda screen].
 
<syntaxhighlight lang="bruijn">y [[1 (0 0)] [1 (0 0)]]
 
# infinite depth
triangle [y [[0 1 [[0]] 1 1]]]
 
:import std/Number .
 
# limited depth
triangle-n [y [[[[1 0 [[0]] 0 0] (=?1 [[1]] (2 --1))]]] (+7)]
</syntaxhighlight>
 
=={{header|C}}==
Line 1,049 ⟶ 1,131:
im.savePGM("sierpinski.pgm");
}</syntaxhighlight>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
[[File:DelphiSierpinskiTriangle.png|frame|none]]
 
<syntaxhighlight lang="Delphi">
 
 
const DepthColors24: array [0..23] of TColor =(
0 or (0 shl 8) or (0 shl 16),
255 or (0 shl 8) or (0 shl 16),
255 or (63 shl 8) or (0 shl 16),
255 or (127 shl 8) or (0 shl 16),
255 or (191 shl 8) or (0 shl 16),
255 or (255 shl 8) or (0 shl 16),
191 or (255 shl 8) or (0 shl 16),
127 or (255 shl 8) or (0 shl 16),
63 or (255 shl 8) or (0 shl 16),
0 or (255 shl 8) or (0 shl 16),
0 or (255 shl 8) or (63 shl 16),
0 or (255 shl 8) or (127 shl 16),
0 or (255 shl 8) or (191 shl 16),
0 or (255 shl 8) or (255 shl 16),
0 or (191 shl 8) or (255 shl 16),
0 or (127 shl 8) or (255 shl 16),
0 or (63 shl 8) or (255 shl 16),
0 or (0 shl 8) or (255 shl 16),
63 or (0 shl 8) or (255 shl 16),
127 or (0 shl 8) or (255 shl 16),
191 or (0 shl 8) or (255 shl 16),
255 or (0 shl 8) or (255 shl 16),
255 or (0 shl 8) or (191 shl 16),
255 or (0 shl 8) or (127 shl 16));
 
procedure DrawSerpTriangle(Image: TImage; StartX,StartY, Depth: integer);
var I,X,Y,Size,Inx: integer;
var C: TColor;
begin
Size:=1 shl Depth;
for Y:=0 to Size-1 do
for X:=0 to Size-1 do
begin
{Calculate new color index}
Inx:=MulDiv(Length(DepthColors24),X+Y,Size+Size)+1;
if (X and Y)=0 then
begin
Image.Canvas.Pixels[StartX+X,StartY+Y]:=DepthColors24[Inx];
end;
end;
end;
 
procedure ShowSierpinskiTriangle(Image: TImage);
begin
ClearImage(Image,clBlack);
DrawSerpTriangle(Image,50,32,8);
Image.Invalidate;
end;
</syntaxhighlight>
{{out}}
<pre>
Elapsed Time: 28.293 ms.
</pre>
 
 
=={{header|EasyLang}}==
[https://easylang.dev/show/#cod=fY3BCoMwEETv+Yq5l65RCOghH1PCKoG0kShS+/UmuiAe7O7pzczOjik6zMm/PgMCL/hixeR/DAIpAL7fZQtdKM87LlxSgs4nFxiaalMUDhOLU86eFrXgXlpZNMKXn4/DPl7/CVRockjgLnu2kCIlVguTt9NqAw== Run it]
 
<syntaxhighlight lang="easylang">
proc triang lev x y size . .
if lev = 0
move x y
circle 0.15
else
lev -= 1
size /= 2
triang lev x + size y size
triang lev x + size / 2 y + size size
triang lev x y size
.
.
triang 8 5 5 90
</syntaxhighlight>
 
=={{header|Erlang}}==
Line 1,102 ⟶ 1,267:
END PROGRAM
</syntaxhighlight>
 
=={{header|Evaldraw}}==
 
This makes use of sleep(millis); and refresh(); in the middle of a function to do the slow animation of triangles.
 
[[File:Evaldraw sierpinski.gif|thumb|alt=refresh allows for drawing outside the main () function|With sleep() we wait 1 milli before redraw any pixels with refresh()]]
 
<syntaxhighlight lang="c">
static calls=0;
() {
setcol(255,255,255);
if (numframes==0) {
cls(0);
calls = 0;
sierpinski(xres/2,yres*0.1,xres*.8,xres*.8);
}
moveto(0,0); printf("%g recursions", calls);
}
sierpinski(x,y,w,h) {
calls++;
triangle(x,y,w,h);
if(w < 10 || h < 10) return;
sleep(1); refresh();
halfH = h/2;
halfW = w/2;
sierpinski(x,y,halfW,halfH); // left
sierpinski(x+halfW/2,y+halfH,halfW,halfH);
sierpinski(x-halfW/2,y+halfH,halfW,halfH);
}
triangle(x,y,w,h) {
moveto(x,y);
lineto(x+w/2, y+h);
lineto(x-w/2, y+h);
lineto(x,y);
}</syntaxhighlight>
 
=={{header|Factor}}==
Line 1,164 ⟶ 1,364:
triangle s" triangle.ppm" save_image \ done, save the image</syntaxhighlight>
{{Out}}''Because Rosetta code doesn't allow file uploads, the output can't be shown.''
 
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
_window = 1
_width = 600
_height = 500
 
local fn SierpinskyTriangle( level as NSUInteger, x as NSUInteger, y as NSUInteger, w as NSUInteger, h as NSUInteger )
NSUInteger w2 = w/2, w4 = w/4, h2 = h/2
if ( level == 1 )
pen -1.0
line to x, y
pen 1.0, fn ColorYellow
line to x+w2, y+h
line to x+w, y
line to x, y
else
fn SierpinskyTriangle( level-1, x, y, w2, h2 )
fn SierpinskyTriangle( level-1, x+w4, y+h2, w2, h2 )
fn SierpinskyTriangle( level-1, x+w2, y, w2, h2 )
end if
end fn
 
window _window, @"Sierpinsky Triangle", ( 0, 0, _width, _height )
WindowSetBackgroundColor( 1, fn ColorBlack )
 
fn SierpinskyTriangle( 9, _width * 0.05, _height * 0.05, _width * 0.9, _height * 0.9 )
 
HandleEvents
</syntaxhighlight>
{{output}}
[[File:Sierpinski_triangle_in_FutureBasic.png]]
 
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/L-system}}
 
'''Solution'''
 
=== 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 script that creates a Sierpiński triangle is:
 
[[File:Fōrmulæ - L-system - Sierpiński triangle 01.png]]
 
[[File:Fōrmulæ - L-system - Sierpiński triangle 02.png]]
 
=== By chaos game ===
 
There is a function written in Fōrmulæ to generate fractals by chaos game in the page [[Chaos game#Fōrmulæ | chaos game]].
 
The script that creates a Sierpiński triangle is:
 
[[File:Fōrmulæ - Chaos game 05.png]]
 
[[File:Fōrmulæ - Chaos game 06.png]]
 
=== By Kronecker product ===
 
There is a function written in Fōrmulæ to create generic Kronecker product based fractal in the page [[Kronecker product based fractals#Fōrmulæ | Kronecker product based fractals]].
 
The script that creates a Sierpiński triangle is:
 
[[File:Fōrmulæ - Kronecker product based fractals 08.png]]
 
[[File:Fōrmulæ - Kronecker product based fractals 09.png]]
 
=== By elementary cellular automaton ===
 
There is a function written in Fōrmulæ to create images for the elementary cellular automaton in the page [[Elementary cellular automaton#Fōrmulæ | Elementary cellular automaton]].
 
All the rules 18, 22 , 23, 60, 82, 90, 102, 126, 129, 146, 153, 154, 161, 165, 167, 181, 182, 195, 210 and 218 produce Sierpiński triangles:
 
[[File:Fōrmulæ - Elementary cellular automaton - Sierpiński triangle 01.png]]
 
[[File:Fōrmulæ - Elementary cellular automaton - Sierpiński triangle 02.png]]
 
=={{header|gnuplot}}==
Generating X,Y coordinates by the ternary digits of parameter t.
Line 3,004 ⟶ 3,285:
{{trans|Kotlin}}
{{libheader|DOME}}
<syntaxhighlight lang="ecmascriptwren">import "graphics" for Canvas, Color
import "dome" for Window
 
Line 3,035 ⟶ 3,316:
}
}</syntaxhighlight>
 
{{out}}
[[File:Wren-Sierpinski_triangle_Graphical.png|400px]]
 
=={{header|XPL0}}==
2,120

edits