Sierpinski triangle/Graphical: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
Line 10: Line 10:
It uses 320x200 mode, so the maximum order is 7.
It uses 320x200 mode, so the maximum order is 7.


<lang asm> ;;; Display a Sierpinski triangle on a CGA screen
<syntaxhighlight lang="asm"> ;;; Display a Sierpinski triangle on a CGA screen
;;; (order 7 is the maximum that fits in 200 lines)
;;; (order 7 is the maximum that fits in 200 lines)
mode: equ 0Fh ; INT 10H call to get current video mode
mode: equ 0Fh ; INT 10H call to get current video mode
Line 83: Line 83:
section .bss
section .bss
order: resb 1 ; Order of Sierpinski triangle
order: resb 1 ; Order of Sierpinski triangle
vmode: resb 1 ; Store old video mode (to restore later)</lang>
vmode: resb 1 ; Store old video mode (to restore later)</syntaxhighlight>


=={{header|Action!}}==
=={{header|Action!}}==
<lang Action!>PROC Draw(INT x0 BYTE y0,depth)
<syntaxhighlight lang="action!">PROC Draw(INT x0 BYTE y0,depth)
BYTE i,x,y,size
BYTE i,x,y,size


Line 114: Line 114:
DO UNTIL CH#$FF OD
DO UNTIL CH#$FF OD
CH=$FF
CH=$FF
RETURN</lang>
RETURN</syntaxhighlight>
{{out}}
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Sierpinski_triangle_graphical.png Screenshot from Atari 8-bit computer]
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Sierpinski_triangle_graphical.png Screenshot from Atari 8-bit computer]
Line 120: Line 120:
=={{header|ActionScript}}==
=={{header|ActionScript}}==
SierpinskiTriangle class:
SierpinskiTriangle class:
<syntaxhighlight lang="actionscript3">
<lang ActionScript3>
package {
package {
Line 200: Line 200:


}
}
</syntaxhighlight>
</lang>


Document class:
Document class:
<syntaxhighlight lang="actionscript3">
<lang ActionScript3>
package {
package {
Line 226: Line 226:


}
}
</syntaxhighlight>
</lang>


=={{header|Asymptote}}==
=={{header|Asymptote}}==
This simple-minded recursive apporach doesn't scale well to large orders, but neither would your PostScript viewer, so there's nothing to gain from a more efficient algorithm. Thus are the perils of vector graphics.
This simple-minded recursive apporach doesn't scale well to large orders, but neither would your PostScript viewer, so there's nothing to gain from a more efficient algorithm. Thus are the perils of vector graphics.


<lang asymptote>path subtriangle(path p, real node) {
<syntaxhighlight lang="asymptote">path subtriangle(path p, real node) {
return
return
point(p, node) --
point(p, node) --
Line 249: Line 249:
}
}
sierpinski((0, 0) -- (5 inch, 1 inch) -- (2 inch, 6 inch) -- cycle, 10);</lang>
sierpinski((0, 0) -- (5 inch, 1 inch) -- (2 inch, 6 inch) -- cycle, 10);</syntaxhighlight>


Una versión mas corta:
Una versión mas corta:
<lang asymptote>pair A = (0, 0), B = (1, 0), C = (.5, 1);
<syntaxhighlight lang="asymptote">pair A = (0, 0), B = (1, 0), C = (.5, 1);


void sierpinski(pair p, int d) {
void sierpinski(pair p, int d) {
Line 265: Line 265:
}
}


sierpinski((0, 0), 0);</lang>
sierpinski((0, 0), 0);</syntaxhighlight>


=={{header|ATS}}==
=={{header|ATS}}==
{{libheader|SDL}}
{{libheader|SDL}}
<lang ATS>// patscc -O2 -flto -D_GNU_SOURCE -DATS_MEMALLOC_LIBC sierpinski.dats -o sierpinski -latslib -lSDL2
<syntaxhighlight lang="ats">// patscc -O2 -flto -D_GNU_SOURCE -DATS_MEMALLOC_LIBC sierpinski.dats -o sierpinski -latslib -lSDL2
#include "share/atspre_staload.hats"
#include "share/atspre_staload.hats"


Line 371: Line 371:
SDL_RenderDrawLine(sdlren, x1, y1, x2, y2);
SDL_RenderDrawLine(sdlren, x1, y1, x2, y2);
}
}
%}</lang>
%}</syntaxhighlight>


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
{{libheader|GDIP}}
{{libheader|GDIP}}
<lang AutoHotkey>#NoEnv
<syntaxhighlight lang="autohotkey">#NoEnv
#SingleInstance, Force
#SingleInstance, Force
SetBatchLines, -1
SetBatchLines, -1
Line 473: Line 473:
If (A_Gui = 1)
If (A_Gui = 1)
PostMessage, 0xA1, 2
PostMessage, 0xA1, 2
}</lang>
}</syntaxhighlight>




Line 479: Line 479:
{{works with|QBasic}}
{{works with|QBasic}}
<!-- {{works with|QBasic}} codificado por: Kibbee, 12 junio 2012 -->
<!-- {{works with|QBasic}} codificado por: Kibbee, 12 junio 2012 -->
<lang basic>
<syntaxhighlight lang="basic">
SCREEN 9
SCREEN 9
H=.5
H=.5
Line 494: Line 494:
PSET(P-X*P,P-Y*P)
PSET(P-X*P,P-Y*P)
NEXT
NEXT
</syntaxhighlight>
</lang>
[https://www.dropbox.com/s/c3g1ae1i771ox7g/Sierpinski_triangle_QBasic.png?dl=0 Sierpinski triangle QBasic image]
[https://www.dropbox.com/s/c3g1ae1i771ox7g/Sierpinski_triangle_QBasic.png?dl=0 Sierpinski triangle QBasic image]


==={{header|BBC BASIC}}===
==={{header|BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
{{works with|BBC BASIC for Windows}}
<lang bbcbasic> order% = 8
<syntaxhighlight lang="bbcbasic"> order% = 8
size% = 2^order%
size% = 2^order%
VDU 23,22,size%;size%;8,8,16,128
VDU 23,22,size%;size%;8,8,16,128
Line 507: Line 507:
NEXT
NEXT
NEXT Y%
NEXT Y%
</syntaxhighlight>
</lang>
[[File:sierpinski_triangle_bbc.gif]]
[[File:sierpinski_triangle_bbc.gif]]


==={{header|FreeBASIC}}===
==={{header|FreeBASIC}}===
<lang FreeBASIC>' version 06-07-2015
<syntaxhighlight lang="freebasic">' version 06-07-2015
' compile with: fbc -s console or with: fbc -s gui
' compile with: fbc -s console or with: fbc -s gui


Line 534: Line 534:
WindowTitle "Hit any key to end program"
WindowTitle "Hit any key to end program"
Sleep
Sleep
End</lang>
End</syntaxhighlight>


==={{header|IS-BASIC}}===
==={{header|IS-BASIC}}===
<lang IS-BASIC>100 PROGRAM "Triangle.bas"
<syntaxhighlight lang="is-basic">100 PROGRAM "Triangle.bas"
110 SET VIDEO MODE 1:SET VIDEO COLOR 0:SET VIDEO X 40:SET VIDEO Y 27
110 SET VIDEO MODE 1:SET VIDEO COLOR 0:SET VIDEO X 40:SET VIDEO Y 27
120 OPEN #101:"video:"
120 OPEN #101:"video:"
Line 550: Line 550:
210 PLOT X,Y;X+W/2,Y+W;X+W,Y;X,Y
210 PLOT X,Y;X+W/2,Y+W;X+W,Y;X,Y
220 END IF
220 END IF
230 END DEF</lang>
230 END DEF</syntaxhighlight>


==={{header|Liberty BASIC}}===
==={{header|Liberty BASIC}}===
The ability of LB to handle very large integers makes the Pascal triangle method very attractive. If you alter the rem'd line you can ask it to print the last, central term...
The ability of LB to handle very large integers makes the Pascal triangle method very attractive. If you alter the rem'd line you can ask it to print the last, central term...
<syntaxhighlight lang="lb">
<lang lb>
nomainwin
nomainwin


Line 594: Line 594:
end
end
end sub
end sub
</syntaxhighlight>
</lang>
Up to order 10 displays on a 1080 vertical pixel screen.
Up to order 10 displays on a 1080 vertical pixel screen.


==={{header|Run BASIC}}===
==={{header|Run BASIC}}===
[[File : SierpinskiRunBasic.png|thumb|right]]
[[File : SierpinskiRunBasic.png|thumb|right]]
<lang runbasic>graphic #g, 300,300
<syntaxhighlight lang="runbasic">graphic #g, 300,300
order = 8
order = 8
width = 100
width = 100
Line 616: Line 616:
render #g
render #g
#g "flush"
#g "flush"
wait</lang>
wait</syntaxhighlight>


==={{header|SmileBASIC}}===
==={{header|SmileBASIC}}===
{{Trans|Action!}}
{{Trans|Action!}}
<lang basic>OPTION STRICT
<syntaxhighlight lang="basic">OPTION STRICT
OPTION DEFINT
OPTION DEFINT
DEF DRAW X0, Y0, DEPTH
DEF DRAW X0, Y0, DEPTH
Line 634: Line 634:
END
END
CALL "DRAW", 96, 32, 7
CALL "DRAW", 96, 32, 7
END</lang>
END</syntaxhighlight>


==={{header|TI-83 BASIC}}===
==={{header|TI-83 BASIC}}===
<lang ti83b>:1→X:1→Y
<syntaxhighlight lang="ti83b">:1→X:1→Y
:Zdecimal
:Zdecimal
:Horizontal 3.1
:Horizontal 3.1
Line 650: Line 650:
:If pxl-Test(Y-1,X) xor (pxl-Test(Y,X-1
:If pxl-Test(Y-1,X) xor (pxl-Test(Y,X-1
:PxlOn(Y,X
:PxlOn(Y,X
:End</lang>
:End</syntaxhighlight>
This could be made faster, but I just wanted to use the DS<( command
This could be made faster, but I just wanted to use the DS<( command


Line 657: Line 657:


3D version.
3D version.
<lang Yabasic>// Adpated from non recursive sierpinsky.bas for SmallBASIC 0.12.6 [B+=MGA] 2016-05-19 with demo mod 2016-05-29
<syntaxhighlight lang="yabasic">// Adpated from non recursive sierpinsky.bas for SmallBASIC 0.12.6 [B+=MGA] 2016-05-19 with demo mod 2016-05-29


//Sierpinski triangle gasket drawn with lines from any 3 given points
//Sierpinski triangle gasket drawn with lines from any 3 given points
Line 762: Line 762:
SierLineTri(cx, cy, cx+r*cos(2*pi/N*i), cy +r*sin(2*pi/N*i), cx + r*cos(2*pi/N*(i+1)), cy + r*sin(2*pi/N*(i+1)), 5)
SierLineTri(cx, cy, cx+r*cos(2*pi/N*i), cy +r*sin(2*pi/N*i), cx + r*cos(2*pi/N*(i+1)), cy + r*sin(2*pi/N*(i+1)), 5)
next i
next i
</syntaxhighlight>
</lang>


Simple recursive version
Simple recursive version
<lang Yabasic>w = 800 : h = 600
<syntaxhighlight lang="yabasic">w = 800 : h = 600
open window w, h
open window w, h
window origin "lb"
window origin "lb"
Line 786: Line 786:
end sub
end sub


SierpinskyTriangle(7, w*0.05, h*0.05, w*0.9, h*0.9)</lang>
SierpinskyTriangle(7, w*0.05, h*0.05, w*0.9, h*0.9)</syntaxhighlight>


=={{header|C}}==
=={{header|C}}==
[[file:sierp-tri-c.png|thumb|center|128px]]Code lifted from [[Dragon curve]]. Given a depth n, draws a triangle of size 2^n in a PNM file to the standard output. Usage: <code>gcc -lm stuff.c -o sierp; ./sierp 9 > triangle.pnm</code>. Sample image generated with depth 9. Generated image's size depends on the depth: it plots dots, but does not draw lines, so a large size with low depth is not possible.
[[file:sierp-tri-c.png|thumb|center|128px]]Code lifted from [[Dragon curve]]. Given a depth n, draws a triangle of size 2^n in a PNM file to the standard output. Usage: <code>gcc -lm stuff.c -o sierp; ./sierp 9 > triangle.pnm</code>. Sample image generated with depth 9. Generated image's size depends on the depth: it plots dots, but does not draw lines, so a large size with low depth is not possible.


<lang C>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <string.h>
#include <string.h>
Line 903: Line 903:
return 0;
return 0;
}</lang>
}</syntaxhighlight>


=={{header|C++}}==
=={{header|C++}}==
[[file:STriCpp.png|thumb|right|200px]]
[[file:STriCpp.png|thumb|right|200px]]
<lang cpp>
<syntaxhighlight lang="cpp">
#include <windows.h>
#include <windows.h>
#include <string>
#include <string>
Line 1,028: Line 1,028:
return 0;
return 0;
}
}
</syntaxhighlight>
</lang>


=={{header|D}}==
=={{header|D}}==
The output image is the same as the Go version. This requires the module from the Grayscale image Task.
The output image is the same as the Go version. This requires the module from the Grayscale image Task.
{{trans|Go}}
{{trans|Go}}
<lang d>void main() {
<syntaxhighlight lang="d">void main() {
import grayscale_image;
import grayscale_image;


Line 1,048: Line 1,048:
im[x + margin, y + margin] = Gray.black;
im[x + margin, y + margin] = Gray.black;
im.savePGM("sierpinski.pgm");
im.savePGM("sierpinski.pgm");
}</lang>
}</syntaxhighlight>


=={{header|Erlang}}==
=={{header|Erlang}}==
<syntaxhighlight lang="erlang">
<lang Erlang>
-module(sierpinski).
-module(sierpinski).
-author("zduchac").
-author("zduchac").
Line 1,081: Line 1,081:
}]),
}]),
wxFrame:show(Frame).
wxFrame:show(Frame).
</syntaxhighlight>
</lang>


=={{header|ERRE}}==
=={{header|ERRE}}==
<syntaxhighlight lang="erre">
<lang ERRE>
PROGRAM SIERPINSKY
PROGRAM SIERPINSKY


Line 1,101: Line 1,101:
GET(K$)
GET(K$)
END PROGRAM
END PROGRAM
</syntaxhighlight>
</lang>


=={{header|Factor}}==
=={{header|Factor}}==
<lang factor>USING: accessors images images.loader kernel literals math
<syntaxhighlight lang="factor">USING: accessors images images.loader kernel literals math
math.bits math.functions make sequences ;
math.bits math.functions make sequences ;
IN: rosetta-code.sierpinski-triangle-graphical
IN: rosetta-code.sierpinski-triangle-graphical
Line 1,144: Line 1,144:
"sierpinski-triangle.png" save-graphic-image ;
"sierpinski-triangle.png" save-graphic-image ;


MAIN: main</lang>
MAIN: main</syntaxhighlight>
{{out}}
{{out}}
[https://i.imgur.com/wjwCrvL.png]
[https://i.imgur.com/wjwCrvL.png]
=={{header|Forth}}==
=={{header|Forth}}==
{{works with|4tH v3.62}}
{{works with|4tH v3.62}}
<lang forth>include lib/graphics.4th \ graphics support is needed
<syntaxhighlight lang="forth">include lib/graphics.4th \ graphics support is needed


520 pic_width ! \ width of the image
520 pic_width ! \ width of the image
Line 1,162: Line 1,162:
: triangle 1 order lshift dup 0 do dup 0 do i j ?pixel loop loop drop ;
: triangle 1 order lshift dup 0 do dup 0 do i j ?pixel loop loop drop ;


triangle s" triangle.ppm" save_image \ done, save the image</lang>
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.''
{{Out}}''Because Rosetta code doesn't allow file uploads, the output can't be shown.''
=={{header|gnuplot}}==
=={{header|gnuplot}}==
Generating X,Y coordinates by the ternary digits of parameter t.
Generating X,Y coordinates by the ternary digits of parameter t.


<lang gnuplot># triangle_x(n) and triangle_y(n) return X,Y coordinates for the
<syntaxhighlight lang="gnuplot"># triangle_x(n) and triangle_y(n) return X,Y coordinates for the
# Sierpinski triangle point number n, for integer n.
# Sierpinski triangle point number n, for integer n.
triangle_x(n) = (n > 0 ? 2*triangle_x(int(n/3)) + digit_to_x(int(n)%3) : 0)
triangle_x(n) = (n > 0 ? 2*triangle_x(int(n/3)) + digit_to_x(int(n)%3) : 0)
Line 1,183: Line 1,183:
set parametric
set parametric
set key off
set key off
plot triangle_x(t), triangle_y(t) with points</lang>
plot triangle_x(t), triangle_y(t) with points</syntaxhighlight>


=={{header|Go}}==
=={{header|Go}}==
[[file:GoSierpinski.png|right|thumb|Output png]]
[[file:GoSierpinski.png|right|thumb|Output png]]
{{trans|Icon and Unicon}}
{{trans|Icon and Unicon}}
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 1,227: Line 1,227:
fmt.Println(err)
fmt.Println(err)
}
}
}</lang>
}</syntaxhighlight>


=={{header|Haskell}}==
=={{header|Haskell}}==
Line 1,237: Line 1,237:


[[File:Sierpinski-Haskell.svg|thumb|Sierpinski Triangle]]
[[File:Sierpinski-Haskell.svg|thumb|Sierpinski Triangle]]
<lang haskell>import Diagrams.Prelude
<syntaxhighlight lang="haskell">import Diagrams.Prelude
import Diagrams.Backend.Cairo.CmdLine
import Diagrams.Backend.Cairo.CmdLine


Line 1,249: Line 1,249:


main = defaultMain $ sierpinski !! 7
main = defaultMain $ sierpinski !! 7
</syntaxhighlight>
</lang>


=={{header|Icon}} and {{header|Unicon}}==
=={{header|Icon}} and {{header|Unicon}}==
Line 1,255: Line 1,255:


[[File:Sierpinski_Triangle_Unicon.PNG|thumb|Sample Output for order=8]]
[[File:Sierpinski_Triangle_Unicon.PNG|thumb|Sample Output for order=8]]
<lang Icon>link wopen
<syntaxhighlight lang="icon">link wopen


procedure main(A)
procedure main(A)
Line 1,270: Line 1,270:


Event()
Event()
end</lang>
end</syntaxhighlight>
{{libheader|Icon Programming Library}}
{{libheader|Icon Programming Library}}
[http://www.cs.arizona.edu/icon/library/src/gprogs/sier1.icn Original source IPL Graphics/sier1.]
[http://www.cs.arizona.edu/icon/library/src/gprogs/sier1.icn Original source IPL Graphics/sier1.]
Line 1,276: Line 1,276:
=={{header|J}}==
=={{header|J}}==
'''Solution:'''
'''Solution:'''
<lang j> load 'viewmat'
<syntaxhighlight lang="j"> load 'viewmat'
'rgb'viewmat--. |. (~:_1&|.)^:(<@#) (2^8){.1</lang>
'rgb'viewmat--. |. (~:_1&|.)^:(<@#) (2^8){.1</syntaxhighlight>


This looks almost exactly (except for OS specific decorations) like the [[:File:Sierpinski_Triangle_Unicon.PNG|task example image]]
This looks almost exactly (except for OS specific decorations) like the [[:File:Sierpinski_Triangle_Unicon.PNG|task example image]]
Line 1,283: Line 1,283:
Other approaches are possible
Other approaches are possible


<lang j>load'viewmat'
<syntaxhighlight lang="j">load'viewmat'
viewmat(,~,.~)^:4,1</lang> generates a [[j:File:Sier1.jpg|"smaller" image]] and is white on black instead of black on white.
viewmat(,~,.~)^:4,1</syntaxhighlight> generates a [[j:File:Sier1.jpg|"smaller" image]] and is white on black instead of black on white.


Similarly, <lang J>viewmat #:(~:/&.#:@, +:)^:(<32) 1</lang> presents the [[j:File:Sierpinksi.png|image]] in a different orientation.
Similarly, <syntaxhighlight lang="j">viewmat #:(~:/&.#:@, +:)^:(<32) 1</syntaxhighlight> presents the [[j:File:Sierpinksi.png|image]] in a different orientation.


And, of course, other approaches are [[j:File:Triangle.png|viable]].
And, of course, other approaches are [[j:File:Triangle.png|viable]].
Line 1,292: Line 1,292:
=={{header|Java}}==
=={{header|Java}}==
'''Solution:'''
'''Solution:'''
<lang java>import javax.swing.*;
<syntaxhighlight lang="java">import javax.swing.*;
import java.awt.*;
import java.awt.*;


Line 1,349: Line 1,349:
drawSierpinskyTriangle(level-1, x, y+size/2, size/2, g);
drawSierpinskyTriangle(level-1, x, y+size/2, size/2, g);
}
}
}</lang>
}</syntaxhighlight>


===Animated version===
===Animated version===
{{works with|Java|8}}
{{works with|Java|8}}
<lang java>import java.awt.*;
<syntaxhighlight lang="java">import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionEvent;
import java.awt.geom.Path2D;
import java.awt.geom.Path2D;
Line 1,414: Line 1,414:
});
});
}
}
}</lang>
}</syntaxhighlight>


=={{header|JavaScript}}==
=={{header|JavaScript}}==
Line 1,425: Line 1,425:
{{Works with|Chrome}}
{{Works with|Chrome}}
[[File:SierpTRjs.png|200px|right|thumb|Output SierpTRjs.png]]
[[File:SierpTRjs.png|200px|right|thumb|Output SierpTRjs.png]]
<lang html>
<syntaxhighlight lang="html">
<!-- SierpinskiTriangle.html -->
<!-- SierpinskiTriangle.html -->
<html>
<html>
Line 1,483: Line 1,483:
<!--canvas id="cvsId" width="1280" height="1280" style="border: 2px inset;"></canvas-->
<!--canvas id="cvsId" width="1280" height="1280" style="border: 2px inset;"></canvas-->
</body></html>
</body></html>
</syntaxhighlight>
</lang>
{{Output}}
{{Output}}
<pre>
<pre>
Line 1,501: Line 1,501:
depending on the location of the included file, and the command-line
depending on the location of the included file, and the command-line
options used.
options used.
<lang jq>include "turtle" {search: "."};
<syntaxhighlight lang="jq">include "turtle" {search: "."};


# Compute the curve using a Lindenmayer system of rules
# Compute the curve using a Lindenmayer system of rules
Line 1,543: Line 1,543:
sierpinski_curve(5)
sierpinski_curve(5)
| svg
| svg
</syntaxhighlight>
</lang>


=={{header|Julia}}==
=={{header|Julia}}==
Produces a png graphic on a transparent background. The brushstroke used for fill might need to be modified for a white background.
Produces a png graphic on a transparent background. The brushstroke used for fill might need to be modified for a white background.
<lang julia>
<syntaxhighlight lang="julia">
using Luxor
using Luxor


Line 1,569: Line 1,569:
finish()
finish()
preview()
preview()
</syntaxhighlight>
</lang>


=={{header|Kotlin}}==
=={{header|Kotlin}}==
'''From Java code:'''
'''From Java code:'''
<lang scala>import java.awt.*
<syntaxhighlight lang="scala">import java.awt.*
import javax.swing.JFrame
import javax.swing.JFrame
import javax.swing.JPanel
import javax.swing.JPanel
Line 1,625: Line 1,625:
drawSierpinskyTriangle(level - 1, x, y + size / 2, size / 2)
drawSierpinskyTriangle(level - 1, x, y + size / 2, size / 2)
}
}
}</lang>
}</syntaxhighlight>


=={{header|Logo}}==
=={{header|Logo}}==
This will draw a graphical Sierpinski gasket using turtle graphics.
This will draw a graphical Sierpinski gasket using turtle graphics.
<lang logo>to sierpinski :n :length
<syntaxhighlight lang="logo">to sierpinski :n :length
if :n = 0 [stop]
if :n = 0 [stop]
repeat 3 [sierpinski :n-1 :length/2 fd :length rt 120]
repeat 3 [sierpinski :n-1 :length/2 fd :length rt 120]
end
end
seth 30 sierpinski 5 200</lang>
seth 30 sierpinski 5 200</syntaxhighlight>


=={{header|Lua}}==
=={{header|Lua}}==
{{libheader|LÖVE}}
{{libheader|LÖVE}}
<lang Lua>-- The argument 'tri' is a list of co-ords: {x1, y1, x2, y2, x3, y3}
<syntaxhighlight lang="lua">-- The argument 'tri' is a list of co-ords: {x1, y1, x2, y2, x3, y3}
function sierpinski (tri, order)
function sierpinski (tri, order)
local new, p, t = {}
local new, p, t = {}
Line 1,657: Line 1,657:
function love.draw ()
function love.draw ()
sierpinski({400, 100, 700, 500, 100, 500}, 7)
sierpinski({400, 100, 700, 500, 100, 500}, 7)
end</lang>
end</syntaxhighlight>
[[File:Love2D-Sierpinski.jpg]]
[[File:Love2D-Sierpinski.jpg]]


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang Mathematica>Sierpinski[n_] :=
<syntaxhighlight lang="mathematica">Sierpinski[n_] :=
Nest[Join @@ Table[With[{a = #[[i, 1]], b = #[[i, 2]], c = #[[i, 3]]},
Nest[Join @@ Table[With[{a = #[[i, 1]], b = #[[i, 2]], c = #[[i, 3]]},
{{a, (a + b)/2, (c + a)/2}, {(a + b)/2, b, (b + c)/2}, {(c + a)/2, (b + c)/2, c}}],
{{a, (a + b)/2, (c + a)/2}, {(a + b)/2, b, (b + c)/2}, {(c + a)/2, (b + c)/2, c}}],
{i, Length[#]}] &, {{{0, 0}, {1/2, 1}, {1, 0}}}, n]
{i, Length[#]}] &, {{{0, 0}, {1/2, 1}, {1, 0}}}, n]
Graphics[{Black, Polygon /@ Sierpinski[8]}]</lang>
Graphics[{Black, Polygon /@ Sierpinski[8]}]</syntaxhighlight>
Another faster version
Another faster version
<lang Mathematica>cf = Compile[{{A, _Real, 2}},
<syntaxhighlight lang="mathematica">cf = Compile[{{A, _Real, 2}},
With[{a = A[[1]], b = A[[2]], c = A[[3]]},
With[{a = A[[1]], b = A[[2]], c = A[[3]]},
With[{ab = (a + b)/2, bc = (b + c)/2, ca = (a + c)/2},
With[{ab = (a + b)/2, bc = (b + c)/2, ca = (a + c)/2},
Line 1,675: Line 1,675:
n = 3;
n = 3;
pts = Flatten[Nest[cf, N@{{{0, 0}, {1, 0}, {1/2, √3/2}}}, n], n];
pts = Flatten[Nest[cf, N@{{{0, 0}, {1, 0}, {1/2, √3/2}}}, n], n];
Graphics[Polygon /@ pts]</lang>
Graphics[Polygon /@ pts]</syntaxhighlight>


[[File:MmaSierpinski.png]]
[[File:MmaSierpinski.png]]
Line 1,681: Line 1,681:
=={{header|MATLAB}}==
=={{header|MATLAB}}==
===Basic Version===
===Basic Version===
<lang MATLAB>[x, x0] = deal(cat(3, [1 0]', [-1 0]', [0 sqrt(3)]'));
<syntaxhighlight lang="matlab">[x, x0] = deal(cat(3, [1 0]', [-1 0]', [0 sqrt(3)]'));
for k = 1 : 6
for k = 1 : 6
x = x(:,:) + x0 * 2 ^ k / 2;
x = x(:,:) + x0 * 2 ^ k / 2;
end
end
patch('Faces', reshape(1 : 3 * 3 ^ k, 3, '')', 'Vertices', x(:,:)')</lang>
patch('Faces', reshape(1 : 3 * 3 ^ k, 3, '')', 'Vertices', x(:,:)')</syntaxhighlight>
{{out}}
{{out}}
Fail to upload output image, use the one of PostScript:
Fail to upload output image, use the one of PostScript:
Line 1,692: Line 1,692:


===Bit Operator Version===
===Bit Operator Version===
<lang MATLAB>t = 0 : 2^16 - 1;
<syntaxhighlight lang="matlab">t = 0 : 2^16 - 1;
plot(t, bitand(t, bitshift(t, -8)), 'k.')</lang>
plot(t, bitand(t, bitshift(t, -8)), 'k.')</syntaxhighlight>


=={{header|Nim}}==
=={{header|Nim}}==
Line 1,699: Line 1,699:
{{libheader|imageman}}
{{libheader|imageman}}
Our triangle is ref on a black background.
Our triangle is ref on a black background.
<lang Nim>import imageman
<syntaxhighlight lang="nim">import imageman


const
const
Line 1,721: Line 1,721:
image.fill(Black)
image.fill(Black)
image.drawSierpinski([400.0, 100.0, 700.0, 500.0, 100.0, 500.0], 7)
image.drawSierpinski([400.0, 100.0, 700.0, 500.0, 100.0, 500.0], 7)
image.savePNG("sierpinski_triangle.png", compression = 9)</lang>
image.savePNG("sierpinski_triangle.png", compression = 9)</syntaxhighlight>


=={{header|Objeck}}==
=={{header|Objeck}}==
<lang objeck>use Game.SDL2;
<syntaxhighlight lang="objeck">use Game.SDL2;
use Game.Framework;
use Game.Framework;


Line 1,792: Line 1,792:
SCREEN_HEIGHT := 480
SCREEN_HEIGHT := 480
}
}
</syntaxhighlight>
</lang>


=={{header|OCaml}}==
=={{header|OCaml}}==


<lang ocaml>open Graphics
<syntaxhighlight lang="ocaml">open Graphics


let round v =
let round v =
Line 1,842: Line 1,842:
let res = loop 6 [ initial_triangle ] in
let res = loop 6 [ initial_triangle ] in
List.iter draw_triangle res;
List.iter draw_triangle res;
ignore (read_key ())</lang>
ignore (read_key ())</syntaxhighlight>


run with:
run with:
Line 1,851: Line 1,851:
[[File:SierpT9.png|right|thumb|Output SierpT9.png]]
[[File:SierpT9.png|right|thumb|Output SierpT9.png]]


<lang parigp>
<syntaxhighlight lang="parigp">
\\ Sierpinski triangle fractal
\\ Sierpinski triangle fractal
\\ Note: plotmat() can be found here on
\\ Note: plotmat() can be found here on
Line 1,864: Line 1,864:
pSierpinskiT(9); \\ SierpT9.png
pSierpinskiT(9); \\ SierpT9.png
}
}
</lang>
</syntaxhighlight>
{{Output}}
{{Output}}
Line 1,875: Line 1,875:
=={{header|Perl}}==
=={{header|Perl}}==
{{trans|Raku}}
{{trans|Raku}}
<lang perl>my $levels = 6;
<syntaxhighlight lang="perl">my $levels = 6;
my $side = 512;
my $side = 512;
my $height = get_height($side);
my $height = get_height($side);
Line 1,922: Line 1,922:
'<g style="fill: #fff; stroke-width: 0;">',
'<g style="fill: #fff; stroke-width: 0;">',
fractal( $side/2, $height, $side*3/4, $height/2, $side/4, $height/2, $levels ),
fractal( $side/2, $height, $side*3/4, $height/2, $side/4, $height/2, $levels ),
'</g></svg>';</lang>
'</g></svg>';</syntaxhighlight>
[https://github.com/SqrtNegInf/Rosettacode-Perl5-Smoke/blob/master/ref/sierpinski_triangle.svg See sierpinski_triangle] (offsite .svg image)
[https://github.com/SqrtNegInf/Rosettacode-Perl5-Smoke/blob/master/ref/sierpinski_triangle.svg See sierpinski_triangle] (offsite .svg image)


Line 1,928: Line 1,928:
Can resize, and change the level from 1 to 12 (press +/-).
Can resize, and change the level from 1 to 12 (press +/-).
{{libheader|Phix/pGUI}}
{{libheader|Phix/pGUI}}
<!--<lang Phix>-->
<!--<syntaxhighlight lang="phix">-->
<span style="color: #000080;font-style:italic;">-- demo\rosetta\SierpinskyTriangle.exw</span>
<span style="color: #000080;font-style:italic;">-- demo\rosetta\SierpinskyTriangle.exw</span>
<span style="color: #008080;">include</span> <span style="color: #000000;">pGUI</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #008080;">include</span> <span style="color: #000000;">pGUI</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 1,999: Line 1,999:
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</lang>-->
<!--</syntaxhighlight>-->


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
[[File:Pil_sierpinski.png|thumb|right]]
[[File:Pil_sierpinski.png|thumb|right]]
Slight modification of the [[Sierpinski_triangle#PicoLisp|text version]], requires ImageMagick's display:
Slight modification of the [[Sierpinski_triangle#PicoLisp|text version]], requires ImageMagick's display:
<lang PicoLisp>(de sierpinski (N)
<syntaxhighlight lang="picolisp">(de sierpinski (N)
(let (D '("1") S "0")
(let (D '("1") S "0")
(do N
(do N
Line 2,019: Line 2,019:
(prinl (length (car Img)) " " (length Img))
(prinl (length (car Img)) " " (length Img))
(mapc prinl Img) ) )
(mapc prinl Img) ) )
</syntaxhighlight>
</lang>


=={{header|PostScript}}==
=={{header|PostScript}}==
[[File:Sierpinski-PS.png|thumb|right]]
[[File:Sierpinski-PS.png|thumb|right]]
<syntaxhighlight lang="postscript">%!PS
<lang PostScript>%!PS


/sierp { % level ax ay bx by cx cy
/sierp { % level ax ay bx by cx cy
Line 2,054: Line 2,054:


6 50 100 550 100 300 533 sierp
6 50 100 550 100 300 533 sierp
showpage</lang>
showpage</syntaxhighlight>


=={{header|Processing}}==
=={{header|Processing}}==
Line 2,064: Line 2,064:
===Pixel based===
===Pixel based===


<syntaxhighlight lang="processing">
<lang Processing>
PVector [] coord = {new PVector(0, 0), new PVector(150, 300), new PVector(300, 0)};
PVector [] coord = {new PVector(0, 0), new PVector(150, 300), new PVector(300, 0)};


Line 2,085: Line 2,085:
}
}
}
}
</syntaxhighlight>
</lang>


===Animated===
===Animated===
<syntaxhighlight lang="processing">
<lang Processing>
int depth = 5;
int depth = 5;
int interval = 50;
int interval = 50;
Line 2,142: Line 2,142:
}
}
}
}
</syntaxhighlight>
</lang>


===3D version===
===3D version===
<syntaxhighlight lang="processing">
<lang Processing>
import peasy.*;
import peasy.*;


Line 2,206: Line 2,206:
}
}
}
}
</syntaxhighlight>
</lang>


=={{header|Prolog}}==
=={{header|Prolog}}==
Line 2,218: Line 2,218:


Works up to sierpinski(13).
Works up to sierpinski(13).
<lang Prolog>sierpinski(N) :-
<syntaxhighlight lang="prolog">sierpinski(N) :-
sformat(A, 'Sierpinski order ~w', [N]),
sformat(A, 'Sierpinski order ~w', [N]),
new(D, picture(A)),
new(D, picture(A)),
Line 2,247: Line 2,247:
draw_Sierpinski(Window, N1, point(X, Y), Len1),
draw_Sierpinski(Window, N1, point(X, Y), Len1),
draw_Sierpinski(Window, N1, point(X1, Y1), Len1),
draw_Sierpinski(Window, N1, point(X1, Y1), Len1),
draw_Sierpinski(Window, N1, point(X2, Y1), Len1).</lang>
draw_Sierpinski(Window, N1, point(X2, Y1), Len1).</syntaxhighlight>


===Iterative version===
===Iterative version===
<lang Prolog>:- dynamic top/1.
<syntaxhighlight lang="prolog">:- dynamic top/1.


sierpinski_iterate(N) :-
sierpinski_iterate(N) :-
Line 2,289: Line 2,289:
; Lst2 = [point(X2, Y1)|Lst1]),
; Lst2 = [point(X2, Y1)|Lst1]),


assert(top(Lst2)).</lang>
assert(top(Lst2)).</syntaxhighlight>


=={{header|Python}}==
=={{header|Python}}==
{{libheader|Turtle}}
{{libheader|Turtle}}
<lang python>
<syntaxhighlight lang="python">
# a very simple version
# a very simple version
import turtle as t
import turtle as t
Line 2,303: Line 2,303:
t.fd(length)
t.fd(length)
t.rt(120)
t.rt(120)
</syntaxhighlight>
</lang>


{{libheader|PyLab}}
{{libheader|PyLab}}
[https://www.dropbox.com/s/gxnl8r8z0kbwi5v/Sierpinski_triangle_Phyton.png?dl=0 Sierpinski triangle image]
[https://www.dropbox.com/s/gxnl8r8z0kbwi5v/Sierpinski_triangle_Phyton.png?dl=0 Sierpinski triangle image]
<lang python>
<syntaxhighlight lang="python">
# otra versión muy simple
# otra versión muy simple
from pylab import*
from pylab import*
Line 2,313: Line 2,313:
for i in'123':x=kron(x,x)
for i in'123':x=kron(x,x)
imsave('a',x)
imsave('a',x)
</syntaxhighlight>
</lang>


{{libheader|NumPy}}
{{libheader|NumPy}}
{{libheader|Turtle}}
{{libheader|Turtle}}
[[File:SierpinskiTriangle-turtle.png|thumb|right]]
[[File:SierpinskiTriangle-turtle.png|thumb|right]]
<lang python>#!/usr/bin/env python
<syntaxhighlight lang="python">#!/usr/bin/env python
##########################################################################################
##########################################################################################
# a very complicated version
# a very complicated version
Line 2,339: Line 2,339:
turt.forward(fwd)
turt.forward(fwd)
return [turn, point, fwd, angle, turt]
return [turn, point, fwd, angle, turt]
</syntaxhighlight>
</lang>
<lang python>##########################################################################################
<syntaxhighlight lang="python">##########################################################################################
# The drawing function
# The drawing function
# --------------------
# --------------------
Line 2,400: Line 2,400:


DrawSierpinskiTriangle(5)
DrawSierpinskiTriangle(5)
</syntaxhighlight>
</lang>


=={{header|Quackery}}==
=={{header|Quackery}}==
<lang Quackery> [ $ "turtleduck.qky" loadfile ] now!
<syntaxhighlight lang="quackery"> [ $ "turtleduck.qky" loadfile ] now!


[ 1 & ] is odd ( n --> b )
[ 1 & ] is odd ( n --> b )
Line 2,440: Line 2,440:
400 1 fly
400 1 fly
3 8 turn
3 8 turn
8 sierpinski</lang>
8 sierpinski</syntaxhighlight>


{{output}}
{{output}}
Line 2,452: Line 2,452:
[[File:SierpTRo6.png|200px|right|thumb|Output SierpTRo6.png]]
[[File:SierpTRo6.png|200px|right|thumb|Output SierpTRo6.png]]
[[File:SierpTRo8.png|200px|right|thumb|Output SierpTRo8.png]]
[[File:SierpTRo8.png|200px|right|thumb|Output SierpTRo8.png]]
<syntaxhighlight lang="r">
<lang r>
## Plotting Sierpinski triangle. aev 4/1/17
## Plotting Sierpinski triangle. aev 4/1/17
## ord - order, fn - file name, ttl - plot title, clr - color
## ord - order, fn - file name, ttl - plot title, clr - color
Line 2,473: Line 2,473:
pSierpinskiT(6,,,"red");
pSierpinskiT(6,,,"red");
pSierpinskiT(8);
pSierpinskiT(8);
</syntaxhighlight>
</lang>
{{Output}}
{{Output}}
<pre>
<pre>
Line 2,490: Line 2,490:
=={{header|Racket}}==
=={{header|Racket}}==
[[File : RacketSierpinski.png|thumb|right]]
[[File : RacketSierpinski.png|thumb|right]]
<syntaxhighlight lang="racket">
<lang Racket>
#lang racket
#lang racket
(require 2htdp/image)
(require 2htdp/image)
Line 2,498: Line 2,498:
(let ([t (sierpinski (- n 1))])
(let ([t (sierpinski (- n 1))])
(freeze (above t (beside t t))))))
(freeze (above t (beside t t))))))
</syntaxhighlight>
</lang>
Test:
Test:
<lang racket>
<syntaxhighlight lang="racket">
;; the following will show the graphics if run in DrRacket
;; the following will show the graphics if run in DrRacket
(sierpinski 8)
(sierpinski 8)
Line 2,506: Line 2,506:
(require file/convertible)
(require file/convertible)
(display-to-file (convert (sierpinski 8) 'png-bytes) "sierpinski.png")
(display-to-file (convert (sierpinski 8) 'png-bytes) "sierpinski.png")
</syntaxhighlight>
</lang>


=={{header|Raku}}==
=={{header|Raku}}==
Line 2,512: Line 2,512:
[[File:Sierpinski-perl6.svg|thumb]]
[[File:Sierpinski-perl6.svg|thumb]]
This is a recursive solution. It is not really practical for more than 8 levels of recursion, but anything more than 7 is barely visible anyway.
This is a recursive solution. It is not really practical for more than 8 levels of recursion, but anything more than 7 is barely visible anyway.
<lang perl6>my $levels = 8;
<syntaxhighlight lang="raku" line>my $levels = 8;
my $side = 512;
my $side = 512;
my $height = get_height($side);
my $height = get_height($side);
Line 2,557: Line 2,557:
'<g style="fill: #fff; stroke-width: 0;">',
'<g style="fill: #fff; stroke-width: 0;">',
fractal( $side/2, $height, $side*3/4, $height/2, $side/4, $height/2, $levels ),
fractal( $side/2, $height, $side*3/4, $height/2, $side/4, $height/2, $levels ),
'</g></svg>';</lang>
'</g></svg>';</syntaxhighlight>


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
load "guilib.ring"
load "guilib.ring"


Line 2,605: Line 2,605:
}
}
label1 { setpicture(p1) show() }
label1 { setpicture(p1) show() }
</syntaxhighlight>
</lang>


Output:
Output:
Line 2,614: Line 2,614:
{{libheader|Shoes}}
{{libheader|Shoes}}
[[File : sierpinski.shoes.png|thumb|right]]
[[File : sierpinski.shoes.png|thumb|right]]
<lang ruby>Shoes.app(:height=>540,:width=>540, :title=>"Sierpinski Triangle") do
<syntaxhighlight lang="ruby">Shoes.app(:height=>540,:width=>540, :title=>"Sierpinski Triangle") do
def triangle(slot, tri, color)
def triangle(slot, tri, color)
x, y, len = tri
x, y, len = tri
Line 2,654: Line 2,654:
end
end
end
end
end</lang>
end</syntaxhighlight>


{{libheader|RubyGems}}
{{libheader|RubyGems}}
{{libheader|JRubyArt}}
{{libheader|JRubyArt}}
JRubyArt is a port of processing to ruby
JRubyArt is a port of processing to ruby
<lang ruby>
<syntaxhighlight lang="ruby">
T_HEIGHT = sqrt(3) / 2
T_HEIGHT = sqrt(3) / 2
TOP_Y = 1 / sqrt(3)
TOP_Y = 1 / sqrt(3)
Line 2,703: Line 2,703:
triangle(cx0, cy0, cx1, cy1, cx2, cy2)
triangle(cx0, cy0, cx1, cy1, cx2, cy2)
end
end
</syntaxhighlight>
</lang>


=={{header|Rust}}==
=={{header|Rust}}==
Output is an SVG file.
Output is an SVG file.
<lang rust>// [dependencies]
<syntaxhighlight lang="rust">// [dependencies]
// svg = "0.8.0"
// svg = "0.8.0"


Line 2,770: Line 2,770:
fn main() {
fn main() {
write_sierpinski_triangle("sierpinski_triangle.svg", 600, 8).unwrap();
write_sierpinski_triangle("sierpinski_triangle.svg", 600, 8).unwrap();
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 2,777: Line 2,777:
=={{header|Seed7}}==
=={{header|Seed7}}==
[[File : SierpinskiSeed7.png|thumb|right]]
[[File : SierpinskiSeed7.png|thumb|right]]
<lang seed7>$ include "seed7_05.s7i";
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "draw.s7i";
include "draw.s7i";
include "keybd.s7i";
include "keybd.s7i";
Line 2,801: Line 2,801:
end for;
end for;
ignore(getc(KEYBOARD));
ignore(getc(KEYBOARD));
end func;</lang>
end func;</syntaxhighlight>


Original source: [http://seed7.sourceforge.net/algorith/graphic.htm#sierpinski]
Original source: [http://seed7.sourceforge.net/algorith/graphic.htm#sierpinski]
Line 2,807: Line 2,807:
=={{header|Sidef}}==
=={{header|Sidef}}==
[[File:Sierpinski_triangle_sidef.png|200px|thumb|right]]
[[File:Sierpinski_triangle_sidef.png|200px|thumb|right]]
<lang ruby>func sierpinski_triangle(n) -> Array {
<syntaxhighlight lang="ruby">func sierpinski_triangle(n) -> Array {
var triangle = ['*']
var triangle = ['*']
{ |i|
{ |i|
Line 2,841: Line 2,841:
var triangle = sierpinski_triangle(8)
var triangle = sierpinski_triangle(8)
var raw_png = triangle.to_png(bgcolor:'black', fgcolor:'red')
var raw_png = triangle.to_png(bgcolor:'black', fgcolor:'red')
File('triangle.png').write(raw_png, :raw)</lang>
File('triangle.png').write(raw_png, :raw)</syntaxhighlight>


=={{header|Tcl}}==
=={{header|Tcl}}==
This code maintains a queue of triangles to cut out; though a stack works just as well, the observed progress is more visually pleasing when a queue is used.
This code maintains a queue of triangles to cut out; though a stack works just as well, the observed progress is more visually pleasing when a queue is used.
{{libheader|Tk}}
{{libheader|Tk}}
<lang tcl>package require Tcl 8.5
<syntaxhighlight lang="tcl">package require Tcl 8.5
package require Tk
package require Tk


Line 2,870: Line 2,870:
pack [canvas .c -width 400 -height 400 -background white]
pack [canvas .c -width 400 -height 400 -background white]
update; # So we can see progress
update; # So we can see progress
sierpinski .c {200 10 390 390 10 390} 7</lang>
sierpinski .c {200 10 390 390 10 390} 7</syntaxhighlight>


=={{header|VBScript}}==
=={{header|VBScript}}==
VBScript does'nt have access to windows graphics. To achieve this i had to implement turtle graphics wtiting SVG commands to an HTML file. At the end the program opens the graphics in the default browser.
VBScript does'nt have access to windows graphics. To achieve this i had to implement turtle graphics wtiting SVG commands to an HTML file. At the end the program opens the graphics in the default browser.
<syntaxhighlight lang="vb">
<lang vb>


option explicit
option explicit
Line 2,996: Line 2,996:
sier 7,64
sier 7,64
set x=nothing 'outputs html file to browser
set x=nothing 'outputs html file to browser
</syntaxhighlight>
</lang>


=={{header|Wren}}==
=={{header|Wren}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
{{libheader|DOME}}
{{libheader|DOME}}
<lang ecmascript>import "graphics" for Canvas, Color
<syntaxhighlight lang="ecmascript">import "graphics" for Canvas, Color
import "dome" for Window
import "dome" for Window


Line 3,031: Line 3,031:
}
}
}
}
}</lang>
}</syntaxhighlight>


=={{header|XPL0}}==
=={{header|XPL0}}==
[[File:TriangXPL0.gif|right]]
[[File:TriangXPL0.gif|right]]
<lang XPL0>include c:\cxpl\codes; \intrinsic 'code' declarations
<syntaxhighlight lang="xpl0">include c:\cxpl\codes; \intrinsic 'code' declarations
def Order=7, Size=1<<Order;
def Order=7, Size=1<<Order;
int X, Y;
int X, Y;
Line 3,044: Line 3,044:
X:= ChIn(1); \wait for keystroke
X:= ChIn(1); \wait for keystroke
SetVid(3); \restore normal text display
SetVid(3); \restore normal text display
]</lang>
]</syntaxhighlight>


=={{header|zkl}}==
=={{header|zkl}}==
Line 3,050: Line 3,050:
Uses the PPM class from http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#zkl
Uses the PPM class from http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#zkl
{{trans|XPL0}}
{{trans|XPL0}}
<lang zkl>const Order=8, Size=(1).shiftLeft(Order);
<syntaxhighlight lang="zkl">const Order=8, Size=(1).shiftLeft(Order);
img:=PPM(300,300);
img:=PPM(300,300);
foreach y,x in (Size,Size){ if(x.bitAnd(y)==0) img[x,y]=0xff0000 }
foreach y,x in (Size,Size){ if(x.bitAnd(y)==0) img[x,y]=0xff0000 }
img.write(File("sierpinskiTriangle.ppm","wb"));</lang>
img.write(File("sierpinskiTriangle.ppm","wb"));</syntaxhighlight>