Sierpinski triangle/Graphical: Difference between revisions
m
syntax highlighting fixup automation
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
|||
Line 10:
It uses 320x200 mode, so the maximum order is 7.
<
;;; (order 7 is the maximum that fits in 200 lines)
mode: equ 0Fh ; INT 10H call to get current video mode
Line 83:
section .bss
order: resb 1 ; Order of Sierpinski triangle
vmode: resb 1 ; Store old video mode (to restore later)</
=={{header|Action!}}==
<
BYTE i,x,y,size
Line 114:
DO UNTIL CH#$FF OD
CH=$FF
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Sierpinski_triangle_graphical.png Screenshot from Atari 8-bit computer]
Line 120:
=={{header|ActionScript}}==
SierpinskiTriangle class:
<syntaxhighlight lang="actionscript3">
package {
Line 200:
}
</syntaxhighlight>
Document class:
<syntaxhighlight lang="actionscript3">
package {
Line 226:
}
</syntaxhighlight>
=={{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.
<
return
point(p, node) --
Line 249:
}
sierpinski((0, 0) -- (5 inch, 1 inch) -- (2 inch, 6 inch) -- cycle, 10);</
Una versión mas corta:
<
void sierpinski(pair p, int d) {
Line 265:
}
sierpinski((0, 0), 0);</
=={{header|ATS}}==
{{libheader|SDL}}
<
#include "share/atspre_staload.hats"
Line 371:
SDL_RenderDrawLine(sdlren, x1, y1, x2, y2);
}
%}</
=={{header|AutoHotkey}}==
{{libheader|GDIP}}
<
#SingleInstance, Force
SetBatchLines, -1
Line 473:
If (A_Gui = 1)
PostMessage, 0xA1, 2
}</
Line 479:
{{works with|QBasic}}
<!-- {{works with|QBasic}} codificado por: Kibbee, 12 junio 2012 -->
<
SCREEN 9
H=.5
Line 494:
PSET(P-X*P,P-Y*P)
NEXT
</syntaxhighlight>
[https://www.dropbox.com/s/c3g1ae1i771ox7g/Sierpinski_triangle_QBasic.png?dl=0 Sierpinski triangle QBasic image]
==={{header|BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
<
size% = 2^order%
VDU 23,22,size%;size%;8,8,16,128
Line 507:
NEXT
NEXT Y%
</syntaxhighlight>
[[File:sierpinski_triangle_bbc.gif]]
==={{header|FreeBASIC}}===
<
' compile with: fbc -s console or with: fbc -s gui
Line 534:
WindowTitle "Hit any key to end program"
Sleep
End</
==={{header|IS-BASIC}}===
<
110 SET VIDEO MODE 1:SET VIDEO COLOR 0:SET VIDEO X 40:SET VIDEO Y 27
120 OPEN #101:"video:"
Line 550:
210 PLOT X,Y;X+W/2,Y+W;X+W,Y;X,Y
220 END IF
230 END DEF</
==={{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...
<syntaxhighlight lang="lb">
nomainwin
Line 594:
end
end sub
</syntaxhighlight>
Up to order 10 displays on a 1080 vertical pixel screen.
==={{header|Run BASIC}}===
[[File : SierpinskiRunBasic.png|thumb|right]]
<
order = 8
width = 100
Line 616:
render #g
#g "flush"
wait</
==={{header|SmileBASIC}}===
{{Trans|Action!}}
<
OPTION DEFINT
DEF DRAW X0, Y0, DEPTH
Line 634:
END
CALL "DRAW", 96, 32, 7
END</
==={{header|TI-83 BASIC}}===
<
:Zdecimal
:Horizontal 3.1
Line 650:
:If pxl-Test(Y-1,X) xor (pxl-Test(Y,X-1
:PxlOn(Y,X
:End</
This could be made faster, but I just wanted to use the DS<( command
Line 657:
3D version.
<
//Sierpinski triangle gasket drawn with lines from any 3 given points
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)
next i
</syntaxhighlight>
Simple recursive version
<
open window w, h
window origin "lb"
Line 786:
end sub
SierpinskyTriangle(7, w*0.05, h*0.05, w*0.9, h*0.9)</
=={{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.
<
#include <stdlib.h>
#include <string.h>
Line 903:
return 0;
}</
=={{header|C++}}==
[[file:STriCpp.png|thumb|right|200px]]
<
#include <windows.h>
#include <string>
Line 1,028:
return 0;
}
</syntaxhighlight>
=={{header|D}}==
The output image is the same as the Go version. This requires the module from the Grayscale image Task.
{{trans|Go}}
<
import grayscale_image;
Line 1,048:
im[x + margin, y + margin] = Gray.black;
im.savePGM("sierpinski.pgm");
}</
=={{header|Erlang}}==
<syntaxhighlight lang="erlang">
-module(sierpinski).
-author("zduchac").
Line 1,081:
}]),
wxFrame:show(Frame).
</syntaxhighlight>
=={{header|ERRE}}==
<syntaxhighlight lang="erre">
PROGRAM SIERPINSKY
Line 1,101:
GET(K$)
END PROGRAM
</syntaxhighlight>
=={{header|Factor}}==
<
math.bits math.functions make sequences ;
IN: rosetta-code.sierpinski-triangle-graphical
Line 1,144:
"sierpinski-triangle.png" save-graphic-image ;
MAIN: main</
{{out}}
[https://i.imgur.com/wjwCrvL.png]
=={{header|Forth}}==
{{works with|4tH v3.62}}
<
520 pic_width ! \ width of the image
Line 1,162:
: 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</
{{Out}}''Because Rosetta code doesn't allow file uploads, the output can't be shown.''
=={{header|gnuplot}}==
Generating X,Y coordinates by the ternary digits of parameter t.
<
# 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)
Line 1,183:
set parametric
set key off
plot triangle_x(t), triangle_y(t) with points</
=={{header|Go}}==
[[file:GoSierpinski.png|right|thumb|Output png]]
{{trans|Icon and Unicon}}
<
import (
Line 1,227:
fmt.Println(err)
}
}</
=={{header|Haskell}}==
Line 1,237:
[[File:Sierpinski-Haskell.svg|thumb|Sierpinski Triangle]]
<
import Diagrams.Backend.Cairo.CmdLine
Line 1,249:
main = defaultMain $ sierpinski !! 7
</syntaxhighlight>
=={{header|Icon}} and {{header|Unicon}}==
Line 1,255:
[[File:Sierpinski_Triangle_Unicon.PNG|thumb|Sample Output for order=8]]
<
procedure main(A)
Line 1,270:
Event()
end</
{{libheader|Icon Programming Library}}
[http://www.cs.arizona.edu/icon/library/src/gprogs/sier1.icn Original source IPL Graphics/sier1.]
Line 1,276:
=={{header|J}}==
'''Solution:'''
<
'rgb'viewmat--. |. (~:_1&|.)^:(<@#) (2^8){.1</
This looks almost exactly (except for OS specific decorations) like the [[:File:Sierpinski_Triangle_Unicon.PNG|task example image]]
Line 1,283:
Other approaches are possible
<
viewmat(,~,.~)^:4,1</
Similarly, <
And, of course, other approaches are [[j:File:Triangle.png|viable]].
Line 1,292:
=={{header|Java}}==
'''Solution:'''
<
import java.awt.*;
Line 1,349:
drawSierpinskyTriangle(level-1, x, y+size/2, size/2, g);
}
}</
===Animated version===
{{works with|Java|8}}
<
import java.awt.event.ActionEvent;
import java.awt.geom.Path2D;
Line 1,414:
});
}
}</
=={{header|JavaScript}}==
Line 1,425:
{{Works with|Chrome}}
[[File:SierpTRjs.png|200px|right|thumb|Output SierpTRjs.png]]
<
<!-- SierpinskiTriangle.html -->
<html>
Line 1,483:
<!--canvas id="cvsId" width="1280" height="1280" style="border: 2px inset;"></canvas-->
</body></html>
</syntaxhighlight>
{{Output}}
<pre>
Line 1,501:
depending on the location of the included file, and the command-line
options used.
<
# Compute the curve using a Lindenmayer system of rules
Line 1,543:
sierpinski_curve(5)
| svg
</syntaxhighlight>
=={{header|Julia}}==
Produces a png graphic on a transparent background. The brushstroke used for fill might need to be modified for a white background.
<
using Luxor
Line 1,569:
finish()
preview()
</syntaxhighlight>
=={{header|Kotlin}}==
'''From Java code:'''
<
import javax.swing.JFrame
import javax.swing.JPanel
Line 1,625:
drawSierpinskyTriangle(level - 1, x, y + size / 2, size / 2)
}
}</
=={{header|Logo}}==
This will draw a graphical Sierpinski gasket using turtle graphics.
<
if :n = 0 [stop]
repeat 3 [sierpinski :n-1 :length/2 fd :length rt 120]
end
seth 30 sierpinski 5 200</
=={{header|Lua}}==
{{libheader|LÖVE}}
<
function sierpinski (tri, order)
local new, p, t = {}
Line 1,657:
function love.draw ()
sierpinski({400, 100, 700, 500, 100, 500}, 7)
end</
[[File:Love2D-Sierpinski.jpg]]
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
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}}],
{i, Length[#]}] &, {{{0, 0}, {1/2, 1}, {1, 0}}}, n]
Graphics[{Black, Polygon /@ Sierpinski[8]}]</
Another faster version
<
With[{a = A[[1]], b = A[[2]], c = A[[3]]},
With[{ab = (a + b)/2, bc = (b + c)/2, ca = (a + c)/2},
Line 1,675:
n = 3;
pts = Flatten[Nest[cf, N@{{{0, 0}, {1, 0}, {1/2, √3/2}}}, n], n];
Graphics[Polygon /@ pts]</
[[File:MmaSierpinski.png]]
Line 1,681:
=={{header|MATLAB}}==
===Basic Version===
<
for k = 1 : 6
x = x(:,:) + x0 * 2 ^ k / 2;
end
patch('Faces', reshape(1 : 3 * 3 ^ k, 3, '')', 'Vertices', x(:,:)')</
{{out}}
Fail to upload output image, use the one of PostScript:
Line 1,692:
===Bit Operator Version===
<
plot(t, bitand(t, bitshift(t, -8)), 'k.')</
=={{header|Nim}}==
Line 1,699:
{{libheader|imageman}}
Our triangle is ref on a black background.
<
const
Line 1,721:
image.fill(Black)
image.drawSierpinski([400.0, 100.0, 700.0, 500.0, 100.0, 500.0], 7)
image.savePNG("sierpinski_triangle.png", compression = 9)</
=={{header|Objeck}}==
<
use Game.Framework;
Line 1,792:
SCREEN_HEIGHT := 480
}
</syntaxhighlight>
=={{header|OCaml}}==
<
let round v =
Line 1,842:
let res = loop 6 [ initial_triangle ] in
List.iter draw_triangle res;
ignore (read_key ())</
run with:
Line 1,851:
[[File:SierpT9.png|right|thumb|Output SierpT9.png]]
<
\\ Sierpinski triangle fractal
\\ Note: plotmat() can be found here on
Line 1,864:
pSierpinskiT(9); \\ SierpT9.png
}
</
{{Output}}
Line 1,875:
=={{header|Perl}}==
{{trans|Raku}}
<
my $side = 512;
my $height = get_height($side);
Line 1,922:
'<g style="fill: #fff; stroke-width: 0;">',
fractal( $side/2, $height, $side*3/4, $height/2, $side/4, $height/2, $levels ),
'</g></svg>';</
[https://github.com/SqrtNegInf/Rosettacode-Perl5-Smoke/blob/master/ref/sierpinski_triangle.svg See sierpinski_triangle] (offsite .svg image)
Line 1,928:
Can resize, and change the level from 1 to 12 (press +/-).
{{libheader|Phix/pGUI}}
<!--<
<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>
Line 1,999:
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</
=={{header|PicoLisp}}==
[[File:Pil_sierpinski.png|thumb|right]]
Slight modification of the [[Sierpinski_triangle#PicoLisp|text version]], requires ImageMagick's display:
<
(let (D '("1") S "0")
(do N
Line 2,019:
(prinl (length (car Img)) " " (length Img))
(mapc prinl Img) ) )
</syntaxhighlight>
=={{header|PostScript}}==
[[File:Sierpinski-PS.png|thumb|right]]
<syntaxhighlight lang="postscript">%!PS
/sierp { % level ax ay bx by cx cy
Line 2,054:
6 50 100 550 100 300 533 sierp
showpage</
=={{header|Processing}}==
Line 2,064:
===Pixel based===
<syntaxhighlight lang="processing">
PVector [] coord = {new PVector(0, 0), new PVector(150, 300), new PVector(300, 0)};
Line 2,085:
}
}
</syntaxhighlight>
===Animated===
<syntaxhighlight lang="processing">
int depth = 5;
int interval = 50;
Line 2,142:
}
}
</syntaxhighlight>
===3D version===
<syntaxhighlight lang="processing">
import peasy.*;
Line 2,206:
}
}
</syntaxhighlight>
=={{header|Prolog}}==
Line 2,218:
Works up to sierpinski(13).
<
sformat(A, 'Sierpinski order ~w', [N]),
new(D, picture(A)),
Line 2,247:
draw_Sierpinski(Window, N1, point(X, Y), Len1),
draw_Sierpinski(Window, N1, point(X1, Y1), Len1),
draw_Sierpinski(Window, N1, point(X2, Y1), Len1).</
===Iterative version===
<
sierpinski_iterate(N) :-
Line 2,289:
; Lst2 = [point(X2, Y1)|Lst1]),
assert(top(Lst2)).</
=={{header|Python}}==
{{libheader|Turtle}}
<
# a very simple version
import turtle as t
Line 2,303:
t.fd(length)
t.rt(120)
</syntaxhighlight>
{{libheader|PyLab}}
[https://www.dropbox.com/s/gxnl8r8z0kbwi5v/Sierpinski_triangle_Phyton.png?dl=0 Sierpinski triangle image]
<
# otra versión muy simple
from pylab import*
Line 2,313:
for i in'123':x=kron(x,x)
imsave('a',x)
</syntaxhighlight>
{{libheader|NumPy}}
{{libheader|Turtle}}
[[File:SierpinskiTriangle-turtle.png|thumb|right]]
<
##########################################################################################
# a very complicated version
Line 2,339:
turt.forward(fwd)
return [turn, point, fwd, angle, turt]
</syntaxhighlight>
<
# The drawing function
# --------------------
Line 2,400:
DrawSierpinskiTriangle(5)
</syntaxhighlight>
=={{header|Quackery}}==
<
[ 1 & ] is odd ( n --> b )
Line 2,440:
400 1 fly
3 8 turn
8 sierpinski</
{{output}}
Line 2,452:
[[File:SierpTRo6.png|200px|right|thumb|Output SierpTRo6.png]]
[[File:SierpTRo8.png|200px|right|thumb|Output SierpTRo8.png]]
<syntaxhighlight lang="r">
## Plotting Sierpinski triangle. aev 4/1/17
## ord - order, fn - file name, ttl - plot title, clr - color
Line 2,473:
pSierpinskiT(6,,,"red");
pSierpinskiT(8);
</syntaxhighlight>
{{Output}}
<pre>
Line 2,490:
=={{header|Racket}}==
[[File : RacketSierpinski.png|thumb|right]]
<syntaxhighlight lang="racket">
#lang racket
(require 2htdp/image)
Line 2,498:
(let ([t (sierpinski (- n 1))])
(freeze (above t (beside t t))))))
</syntaxhighlight>
Test:
<
;; the following will show the graphics if run in DrRacket
(sierpinski 8)
Line 2,506:
(require file/convertible)
(display-to-file (convert (sierpinski 8) 'png-bytes) "sierpinski.png")
</syntaxhighlight>
=={{header|Raku}}==
Line 2,512:
[[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.
<syntaxhighlight lang="raku"
my $side = 512;
my $height = get_height($side);
Line 2,557:
'<g style="fill: #fff; stroke-width: 0;">',
fractal( $side/2, $height, $side*3/4, $height/2, $side/4, $height/2, $levels ),
'</g></svg>';</
=={{header|Ring}}==
<
load "guilib.ring"
Line 2,605:
}
label1 { setpicture(p1) show() }
</syntaxhighlight>
Output:
Line 2,614:
{{libheader|Shoes}}
[[File : sierpinski.shoes.png|thumb|right]]
<
def triangle(slot, tri, color)
x, y, len = tri
Line 2,654:
end
end
end</
{{libheader|RubyGems}}
{{libheader|JRubyArt}}
JRubyArt is a port of processing to ruby
<
T_HEIGHT = sqrt(3) / 2
TOP_Y = 1 / sqrt(3)
Line 2,703:
triangle(cx0, cy0, cx1, cy1, cx2, cy2)
end
</syntaxhighlight>
=={{header|Rust}}==
Output is an SVG file.
<
// svg = "0.8.0"
Line 2,770:
fn main() {
write_sierpinski_triangle("sierpinski_triangle.svg", 600, 8).unwrap();
}</
{{out}}
Line 2,777:
=={{header|Seed7}}==
[[File : SierpinskiSeed7.png|thumb|right]]
<
include "draw.s7i";
include "keybd.s7i";
Line 2,801:
end for;
ignore(getc(KEYBOARD));
end func;</
Original source: [http://seed7.sourceforge.net/algorith/graphic.htm#sierpinski]
Line 2,807:
=={{header|Sidef}}==
[[File:Sierpinski_triangle_sidef.png|200px|thumb|right]]
<
var triangle = ['*']
{ |i|
Line 2,841:
var triangle = sierpinski_triangle(8)
var raw_png = triangle.to_png(bgcolor:'black', fgcolor:'red')
File('triangle.png').write(raw_png, :raw)</
=={{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.
{{libheader|Tk}}
<
package require Tk
Line 2,870:
pack [canvas .c -width 400 -height 400 -background white]
update; # So we can see progress
sierpinski .c {200 10 390 390 10 390} 7</
=={{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.
<syntaxhighlight lang="vb">
option explicit
Line 2,996:
sier 7,64
set x=nothing 'outputs html file to browser
</syntaxhighlight>
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|DOME}}
<
import "dome" for Window
Line 3,031:
}
}
}</
=={{header|XPL0}}==
[[File:TriangXPL0.gif|right]]
<
def Order=7, Size=1<<Order;
int X, Y;
Line 3,044:
X:= ChIn(1); \wait for keystroke
SetVid(3); \restore normal text display
]</
=={{header|zkl}}==
Line 3,050:
Uses the PPM class from http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#zkl
{{trans|XPL0}}
<
img:=PPM(300,300);
foreach y,x in (Size,Size){ if(x.bitAnd(y)==0) img[x,y]=0xff0000 }
img.write(File("sierpinskiTriangle.ppm","wb"));</
|