Jump to content

Sierpinski triangle/Graphical: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 10:
It uses 320x200 mode, so the maximum order is 7.
 
<langsyntaxhighlight lang="asm"> ;;; Display a Sierpinski triangle on a CGA screen
;;; (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)</langsyntaxhighlight>
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">PROC Draw(INT x0 BYTE y0,depth)
BYTE i,x,y,size
 
Line 114:
DO UNTIL CH#$FF OD
CH=$FF
RETURN</langsyntaxhighlight>
{{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">
<lang ActionScript3>
package {
Line 200:
 
}
</syntaxhighlight>
</lang>
 
Document class:
<syntaxhighlight lang="actionscript3">
<lang ActionScript3>
package {
Line 226:
 
}
</syntaxhighlight>
</lang>
 
=={{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.
 
<langsyntaxhighlight lang="asymptote">path subtriangle(path p, real node) {
return
point(p, node) --
Line 249:
}
sierpinski((0, 0) -- (5 inch, 1 inch) -- (2 inch, 6 inch) -- cycle, 10);</langsyntaxhighlight>
 
Una versión mas corta:
<langsyntaxhighlight lang="asymptote">pair A = (0, 0), B = (1, 0), C = (.5, 1);
 
void sierpinski(pair p, int d) {
Line 265:
}
 
sierpinski((0, 0), 0);</langsyntaxhighlight>
 
=={{header|ATS}}==
{{libheader|SDL}}
<langsyntaxhighlight ATSlang="ats">// patscc -O2 -flto -D_GNU_SOURCE -DATS_MEMALLOC_LIBC sierpinski.dats -o sierpinski -latslib -lSDL2
#include "share/atspre_staload.hats"
 
Line 371:
SDL_RenderDrawLine(sdlren, x1, y1, x2, y2);
}
%}</langsyntaxhighlight>
 
=={{header|AutoHotkey}}==
{{libheader|GDIP}}
<langsyntaxhighlight AutoHotkeylang="autohotkey">#NoEnv
#SingleInstance, Force
SetBatchLines, -1
Line 473:
If (A_Gui = 1)
PostMessage, 0xA1, 2
}</langsyntaxhighlight>
 
 
Line 479:
{{works with|QBasic}}
<!-- {{works with|QBasic}} codificado por: Kibbee, 12 junio 2012 -->
<langsyntaxhighlight lang="basic">
SCREEN 9
H=.5
Line 494:
PSET(P-X*P,P-Y*P)
NEXT
</syntaxhighlight>
</lang>
[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}}
<langsyntaxhighlight lang="bbcbasic"> order% = 8
size% = 2^order%
VDU 23,22,size%;size%;8,8,16,128
Line 507:
NEXT
NEXT Y%
</syntaxhighlight>
</lang>
[[File:sierpinski_triangle_bbc.gif]]
 
==={{header|FreeBASIC}}===
<langsyntaxhighlight FreeBASIClang="freebasic">' version 06-07-2015
' compile with: fbc -s console or with: fbc -s gui
 
Line 534:
WindowTitle "Hit any key to end program"
Sleep
End</langsyntaxhighlight>
 
==={{header|IS-BASIC}}===
<langsyntaxhighlight ISlang="is-BASICbasic">100 PROGRAM "Triangle.bas"
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</langsyntaxhighlight>
 
==={{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">
<lang lb>
nomainwin
 
Line 594:
end
end sub
</syntaxhighlight>
</lang>
Up to order 10 displays on a 1080 vertical pixel screen.
 
==={{header|Run BASIC}}===
[[File : SierpinskiRunBasic.png|thumb|right]]
<langsyntaxhighlight lang="runbasic">graphic #g, 300,300
order = 8
width = 100
Line 616:
render #g
#g "flush"
wait</langsyntaxhighlight>
 
==={{header|SmileBASIC}}===
{{Trans|Action!}}
<langsyntaxhighlight lang="basic">OPTION STRICT
OPTION DEFINT
DEF DRAW X0, Y0, DEPTH
Line 634:
END
CALL "DRAW", 96, 32, 7
END</langsyntaxhighlight>
 
==={{header|TI-83 BASIC}}===
<langsyntaxhighlight lang="ti83b">:1→X:1→Y
:Zdecimal
:Horizontal 3.1
Line 650:
:If pxl-Test(Y-1,X) xor (pxl-Test(Y,X-1
:PxlOn(Y,X
:End</langsyntaxhighlight>
This could be made faster, but I just wanted to use the DS<( command
 
Line 657:
 
3D version.
<langsyntaxhighlight Yabasiclang="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
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>
</lang>
 
Simple recursive version
<langsyntaxhighlight Yabasiclang="yabasic">w = 800 : h = 600
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)</langsyntaxhighlight>
 
=={{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.
 
<langsyntaxhighlight Clang="c">#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Line 903:
return 0;
}</langsyntaxhighlight>
 
=={{header|C++}}==
[[file:STriCpp.png|thumb|right|200px]]
<langsyntaxhighlight lang="cpp">
#include <windows.h>
#include <string>
Line 1,028:
return 0;
}
</syntaxhighlight>
</lang>
 
=={{header|D}}==
The output image is the same as the Go version. This requires the module from the Grayscale image Task.
{{trans|Go}}
<langsyntaxhighlight lang="d">void main() {
import grayscale_image;
 
Line 1,048:
im[x + margin, y + margin] = Gray.black;
im.savePGM("sierpinski.pgm");
}</langsyntaxhighlight>
 
=={{header|Erlang}}==
<syntaxhighlight lang="erlang">
<lang Erlang>
-module(sierpinski).
-author("zduchac").
Line 1,081:
}]),
wxFrame:show(Frame).
</syntaxhighlight>
</lang>
 
=={{header|ERRE}}==
<syntaxhighlight lang="erre">
<lang ERRE>
PROGRAM SIERPINSKY
 
Line 1,101:
GET(K$)
END PROGRAM
</syntaxhighlight>
</lang>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: accessors images images.loader kernel literals math
math.bits math.functions make sequences ;
IN: rosetta-code.sierpinski-triangle-graphical
Line 1,144:
"sierpinski-triangle.png" save-graphic-image ;
 
MAIN: main</langsyntaxhighlight>
{{out}}
[https://i.imgur.com/wjwCrvL.png]
=={{header|Forth}}==
{{works with|4tH v3.62}}
<langsyntaxhighlight lang="forth">include lib/graphics.4th \ graphics support is needed
 
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</langsyntaxhighlight>
{{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.
 
<langsyntaxhighlight lang="gnuplot"># triangle_x(n) and triangle_y(n) return X,Y coordinates for the
# 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</langsyntaxhighlight>
 
=={{header|Go}}==
[[file:GoSierpinski.png|right|thumb|Output png]]
{{trans|Icon and Unicon}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,227:
fmt.Println(err)
}
}</langsyntaxhighlight>
 
=={{header|Haskell}}==
Line 1,237:
 
[[File:Sierpinski-Haskell.svg|thumb|Sierpinski Triangle]]
<langsyntaxhighlight lang="haskell">import Diagrams.Prelude
import Diagrams.Backend.Cairo.CmdLine
 
Line 1,249:
 
main = defaultMain $ sierpinski !! 7
</syntaxhighlight>
</lang>
 
=={{header|Icon}} and {{header|Unicon}}==
Line 1,255:
 
[[File:Sierpinski_Triangle_Unicon.PNG|thumb|Sample Output for order=8]]
<langsyntaxhighlight Iconlang="icon">link wopen
 
procedure main(A)
Line 1,270:
 
Event()
end</langsyntaxhighlight>
{{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:'''
<langsyntaxhighlight lang="j"> load 'viewmat'
'rgb'viewmat--. |. (~:_1&|.)^:(<@#) (2^8){.1</langsyntaxhighlight>
 
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
 
<langsyntaxhighlight lang="j">load'viewmat'
viewmat(,~,.~)^:4,1</langsyntaxhighlight> generates a [[j:File:Sier1.jpg|"smaller" image]] and is white on black instead of black on white.
 
Similarly, <langsyntaxhighlight Jlang="j">viewmat #:(~:/&.#:@, +:)^:(<32) 1</langsyntaxhighlight> presents the [[j:File:Sierpinksi.png|image]] in a different orientation.
 
And, of course, other approaches are [[j:File:Triangle.png|viable]].
Line 1,292:
=={{header|Java}}==
'''Solution:'''
<langsyntaxhighlight lang="java">import javax.swing.*;
import java.awt.*;
 
Line 1,349:
drawSierpinskyTriangle(level-1, x, y+size/2, size/2, g);
}
}</langsyntaxhighlight>
 
===Animated version===
{{works with|Java|8}}
<langsyntaxhighlight lang="java">import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.geom.Path2D;
Line 1,414:
});
}
}</langsyntaxhighlight>
 
=={{header|JavaScript}}==
Line 1,425:
{{Works with|Chrome}}
[[File:SierpTRjs.png|200px|right|thumb|Output SierpTRjs.png]]
<langsyntaxhighlight lang="html">
<!-- SierpinskiTriangle.html -->
<html>
Line 1,483:
<!--canvas id="cvsId" width="1280" height="1280" style="border: 2px inset;"></canvas-->
</body></html>
</syntaxhighlight>
</lang>
{{Output}}
<pre>
Line 1,501:
depending on the location of the included file, and the command-line
options used.
<langsyntaxhighlight lang="jq">include "turtle" {search: "."};
 
# Compute the curve using a Lindenmayer system of rules
Line 1,543:
sierpinski_curve(5)
| svg
</syntaxhighlight>
</lang>
 
=={{header|Julia}}==
Produces a png graphic on a transparent background. The brushstroke used for fill might need to be modified for a white background.
<langsyntaxhighlight lang="julia">
using Luxor
 
Line 1,569:
finish()
preview()
</syntaxhighlight>
</lang>
 
=={{header|Kotlin}}==
'''From Java code:'''
<langsyntaxhighlight lang="scala">import java.awt.*
import javax.swing.JFrame
import javax.swing.JPanel
Line 1,625:
drawSierpinskyTriangle(level - 1, x, y + size / 2, size / 2)
}
}</langsyntaxhighlight>
 
=={{header|Logo}}==
This will draw a graphical Sierpinski gasket using turtle graphics.
<langsyntaxhighlight lang="logo">to sierpinski :n :length
if :n = 0 [stop]
repeat 3 [sierpinski :n-1 :length/2 fd :length rt 120]
end
seth 30 sierpinski 5 200</langsyntaxhighlight>
 
=={{header|Lua}}==
{{libheader|LÖVE}}
<langsyntaxhighlight Lualang="lua">-- The argument 'tri' is a list of co-ords: {x1, y1, x2, y2, x3, y3}
function sierpinski (tri, order)
local new, p, t = {}
Line 1,657:
function love.draw ()
sierpinski({400, 100, 700, 500, 100, 500}, 7)
end</langsyntaxhighlight>
[[File:Love2D-Sierpinski.jpg]]
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">Sierpinski[n_] :=
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]}]</langsyntaxhighlight>
Another faster version
<langsyntaxhighlight Mathematicalang="mathematica">cf = Compile[{{A, _Real, 2}},
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]</langsyntaxhighlight>
 
[[File:MmaSierpinski.png]]
Line 1,681:
=={{header|MATLAB}}==
===Basic Version===
<langsyntaxhighlight MATLABlang="matlab">[x, x0] = deal(cat(3, [1 0]', [-1 0]', [0 sqrt(3)]'));
for k = 1 : 6
x = x(:,:) + x0 * 2 ^ k / 2;
end
patch('Faces', reshape(1 : 3 * 3 ^ k, 3, '')', 'Vertices', x(:,:)')</langsyntaxhighlight>
{{out}}
Fail to upload output image, use the one of PostScript:
Line 1,692:
 
===Bit Operator Version===
<langsyntaxhighlight MATLABlang="matlab">t = 0 : 2^16 - 1;
plot(t, bitand(t, bitshift(t, -8)), 'k.')</langsyntaxhighlight>
 
=={{header|Nim}}==
Line 1,699:
{{libheader|imageman}}
Our triangle is ref on a black background.
<langsyntaxhighlight Nimlang="nim">import imageman
 
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)</langsyntaxhighlight>
 
=={{header|Objeck}}==
<langsyntaxhighlight lang="objeck">use Game.SDL2;
use Game.Framework;
 
Line 1,792:
SCREEN_HEIGHT := 480
}
</syntaxhighlight>
</lang>
 
=={{header|OCaml}}==
 
<langsyntaxhighlight lang="ocaml">open Graphics
 
let round v =
Line 1,842:
let res = loop 6 [ initial_triangle ] in
List.iter draw_triangle res;
ignore (read_key ())</langsyntaxhighlight>
 
run with:
Line 1,851:
[[File:SierpT9.png|right|thumb|Output SierpT9.png]]
 
<langsyntaxhighlight lang="parigp">
\\ Sierpinski triangle fractal
\\ Note: plotmat() can be found here on
Line 1,864:
pSierpinskiT(9); \\ SierpT9.png
}
</langsyntaxhighlight>
{{Output}}
Line 1,875:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">my $levels = 6;
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>';</langsyntaxhighlight>
[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}}
<!--<langsyntaxhighlight Phixlang="phix">-->
<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>
<!--</langsyntaxhighlight>-->
 
=={{header|PicoLisp}}==
[[File:Pil_sierpinski.png|thumb|right]]
Slight modification of the [[Sierpinski_triangle#PicoLisp|text version]], requires ImageMagick's display:
<langsyntaxhighlight PicoLisplang="picolisp">(de sierpinski (N)
(let (D '("1") S "0")
(do N
Line 2,019:
(prinl (length (car Img)) " " (length Img))
(mapc prinl Img) ) )
</syntaxhighlight>
</lang>
 
=={{header|PostScript}}==
[[File:Sierpinski-PS.png|thumb|right]]
<syntaxhighlight lang="postscript">%!PS
<lang PostScript>%!PS
 
/sierp { % level ax ay bx by cx cy
Line 2,054:
 
6 50 100 550 100 300 533 sierp
showpage</langsyntaxhighlight>
 
=={{header|Processing}}==
Line 2,064:
===Pixel based===
 
<syntaxhighlight lang="processing">
<lang Processing>
PVector [] coord = {new PVector(0, 0), new PVector(150, 300), new PVector(300, 0)};
 
Line 2,085:
}
}
</syntaxhighlight>
</lang>
 
===Animated===
<syntaxhighlight lang="processing">
<lang Processing>
int depth = 5;
int interval = 50;
Line 2,142:
}
}
</syntaxhighlight>
</lang>
 
===3D version===
<syntaxhighlight lang="processing">
<lang Processing>
import peasy.*;
 
Line 2,206:
}
}
</syntaxhighlight>
</lang>
 
=={{header|Prolog}}==
Line 2,218:
 
Works up to sierpinski(13).
<langsyntaxhighlight Prologlang="prolog">sierpinski(N) :-
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).</langsyntaxhighlight>
 
===Iterative version===
<langsyntaxhighlight Prologlang="prolog">:- dynamic top/1.
 
sierpinski_iterate(N) :-
Line 2,289:
; Lst2 = [point(X2, Y1)|Lst1]),
 
assert(top(Lst2)).</langsyntaxhighlight>
 
=={{header|Python}}==
{{libheader|Turtle}}
<langsyntaxhighlight lang="python">
# a very simple version
import turtle as t
Line 2,303:
t.fd(length)
t.rt(120)
</syntaxhighlight>
</lang>
 
{{libheader|PyLab}}
[https://www.dropbox.com/s/gxnl8r8z0kbwi5v/Sierpinski_triangle_Phyton.png?dl=0 Sierpinski triangle image]
<langsyntaxhighlight lang="python">
# otra versión muy simple
from pylab import*
Line 2,313:
for i in'123':x=kron(x,x)
imsave('a',x)
</syntaxhighlight>
</lang>
 
{{libheader|NumPy}}
{{libheader|Turtle}}
[[File:SierpinskiTriangle-turtle.png|thumb|right]]
<langsyntaxhighlight lang="python">#!/usr/bin/env python
##########################################################################################
# a very complicated version
Line 2,339:
turt.forward(fwd)
return [turn, point, fwd, angle, turt]
</syntaxhighlight>
</lang>
<langsyntaxhighlight lang="python">##########################################################################################
# The drawing function
# --------------------
Line 2,400:
 
DrawSierpinskiTriangle(5)
</syntaxhighlight>
</lang>
 
=={{header|Quackery}}==
<langsyntaxhighlight Quackerylang="quackery"> [ $ "turtleduck.qky" loadfile ] now!
 
[ 1 & ] is odd ( n --> b )
Line 2,440:
400 1 fly
3 8 turn
8 sierpinski</langsyntaxhighlight>
 
{{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">
<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>
</lang>
{{Output}}
<pre>
Line 2,490:
=={{header|Racket}}==
[[File : RacketSierpinski.png|thumb|right]]
<syntaxhighlight lang="racket">
<lang Racket>
#lang racket
(require 2htdp/image)
Line 2,498:
(let ([t (sierpinski (- n 1))])
(freeze (above t (beside t t))))))
</syntaxhighlight>
</lang>
Test:
<langsyntaxhighlight lang="racket">
;; 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>
</lang>
 
=={{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" perl6line>my $levels = 8;
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>';</langsyntaxhighlight>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "guilib.ring"
 
Line 2,605:
}
label1 { setpicture(p1) show() }
</syntaxhighlight>
</lang>
 
Output:
Line 2,614:
{{libheader|Shoes}}
[[File : sierpinski.shoes.png|thumb|right]]
<langsyntaxhighlight lang="ruby">Shoes.app(:height=>540,:width=>540, :title=>"Sierpinski Triangle") do
def triangle(slot, tri, color)
x, y, len = tri
Line 2,654:
end
end
end</langsyntaxhighlight>
 
{{libheader|RubyGems}}
{{libheader|JRubyArt}}
JRubyArt is a port of processing to ruby
<langsyntaxhighlight lang="ruby">
T_HEIGHT = sqrt(3) / 2
TOP_Y = 1 / sqrt(3)
Line 2,703:
triangle(cx0, cy0, cx1, cy1, cx2, cy2)
end
</syntaxhighlight>
</lang>
 
=={{header|Rust}}==
Output is an SVG file.
<langsyntaxhighlight lang="rust">// [dependencies]
// svg = "0.8.0"
 
Line 2,770:
fn main() {
write_sierpinski_triangle("sierpinski_triangle.svg", 600, 8).unwrap();
}</langsyntaxhighlight>
 
{{out}}
Line 2,777:
=={{header|Seed7}}==
[[File : SierpinskiSeed7.png|thumb|right]]
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "draw.s7i";
include "keybd.s7i";
Line 2,801:
end for;
ignore(getc(KEYBOARD));
end func;</langsyntaxhighlight>
 
Original source: [http://seed7.sourceforge.net/algorith/graphic.htm#sierpinski]
Line 2,807:
=={{header|Sidef}}==
[[File:Sierpinski_triangle_sidef.png|200px|thumb|right]]
<langsyntaxhighlight lang="ruby">func sierpinski_triangle(n) -> Array {
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)</langsyntaxhighlight>
 
=={{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}}
<langsyntaxhighlight lang="tcl">package require Tcl 8.5
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</langsyntaxhighlight>
 
=={{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">
<lang vb>
 
option explicit
Line 2,996:
sier 7,64
set x=nothing 'outputs html file to browser
</syntaxhighlight>
</lang>
 
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|DOME}}
<langsyntaxhighlight lang="ecmascript">import "graphics" for Canvas, Color
import "dome" for Window
 
Line 3,031:
}
}
}</langsyntaxhighlight>
 
=={{header|XPL0}}==
[[File:TriangXPL0.gif|right]]
<langsyntaxhighlight XPL0lang="xpl0">include c:\cxpl\codes; \intrinsic 'code' declarations
def Order=7, Size=1<<Order;
int X, Y;
Line 3,044:
X:= ChIn(1); \wait for keystroke
SetVid(3); \restore normal text display
]</langsyntaxhighlight>
 
=={{header|zkl}}==
Line 3,050:
Uses the PPM class from http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#zkl
{{trans|XPL0}}
<langsyntaxhighlight lang="zkl">const Order=8, Size=(1).shiftLeft(Order);
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"));</langsyntaxhighlight>
 
 
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.