Sierpinski pentagon: Difference between revisions

(Added solution for Action!)
(→‎{{header|Wren}}: Added image)
 
(13 intermediate revisions by 8 users not shown)
Line 9:
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">PROC Main()
INT ARRAY xs=[249 200 96 80 175]
BYTE ARRAY ys=[82 176 159 55 7]
Line 30:
OD
CH=$FF
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Sierpinski_pentagon.png Screenshot from Atari 8-bit computer]
Line 37:
{{trans|Go}}
Requires [https://www.autohotkey.com/boards/viewtopic.php?t=6517 Gdip Library]
<langsyntaxhighlight AutoHotkeylang="autohotkey">W := H := 640
hw := W / 2
margin := 20
Line 128:
Gdip_Shutdown(pToken)
ExitApp
Return</langsyntaxhighlight>
 
=={{header|C}}==
The Sierpinski fractals can be generated via the [http://mathworld.wolfram.com/ChaosGame.html Chaos Game]. This implementation thus generalizes the [[Chaos game]] C implementation on Rosettacode. As the number of sides increases, the number of iterations must increase dramatically for a well pronounced fractal ( 30000 for a pentagon). This is in keeping with the requirements that the implementation should work for polygons with sides 1 to 4 as well. Requires the [http://www.cs.colorado.edu/~main/bgi/cs1300/ WinBGIm] library.
<syntaxhighlight lang="c">
<lang C>
#include<graphics.h>
#include<stdlib.h>
Line 194:
return 0;
}
</syntaxhighlight>
</lang>
 
=={{header|C++}}==
{{trans|D}}
<langsyntaxhighlight lang="cpp">#include <iomanip>
#include <iostream>
 
Line 352:
 
std::cout << "</svg>";
}</langsyntaxhighlight>
 
=={{header|D}}==
Line 360:
This runs very quickly compared to the Python version.
 
<langsyntaxhighlight Dlang="d">import std.math;
import std.stdio;
 
Line 514:
tracing = false;
}
}</langsyntaxhighlight>
 
=={{header|EasyLang}}==
{{Trans|Processing}}
[https://easylang.dev/show/#cod=pZHNrsIgEIX3PMVJ3GgbsSVpdNOHacroJemFBog/by+DqHXh3Vx2w/mYc2ZwXpNHj06sIMaJBi8mY+lidPxBI5UI4zBRAlrssFaoMbqAvUIFteE3s3cjZrJxODmLK24IRhM0zamBhBQAzLHUPRqu0/l1Z2K6lEfnMdhTdmoQIs3sER3U4VCQdK6o++z/QKts9ZZvLAdjv8g818IyJ6MpPIkcu0oNeOByp02I6B9SXYjFAv4Xnpv/Ef5T/rbhLdrlPFJI8UJVh7ZD18DlT2b0Dg== Run it]
<syntaxhighlight lang="easylang">
order = 5
#
clear
linewidth 0.2
scale = 1 / (2 + cos 72 * 2)
#
proc pentagon x y side depth . .
if depth = 0
move x y
for angle = 0 step 72 to 288
x += cos angle * side
y += sin angle * side
line x y
.
else
side *= scale
dist = side + side * cos 72 * 2
for angle = 0 step 72 to 288
x += cos angle * dist
y += sin angle * dist
pentagon x y side depth - 1
.
.
.
pentagon 25 15 50 order - 1
</syntaxhighlight>
 
=={{header|FreeBASIC}}==
{{trans|XPL0}}
<syntaxhighlight lang="vb">#define pi 4 * Atn(1)
#define yellow Rgb(255,255,0)
 
Dim As Byte orden = 5 'can also set this to 1, 2, 3, or 4
 
Dim Shared As Single deg72
deg72 = 72 * pi / 180 '72 degrees in radians
Dim As Integer HW = 640/2
Dim As Byte tam = 20
Dim As Integer radio = HW - 2*tam
Dim Shared As Single ScaleFactor
 
Sub DrawPentagon(posX As Integer, posY As Integer, largo As Single, fondo As Byte)
Dim As Byte i
Dim As Single angulo = 3 * deg72, dist
If fondo = 0 Then
Pset (posX, posY)
For i = 0 To 4
posX += Fix(largo * Cos(angulo))
posY -= Fix(largo * Sin(angulo))
Line - (posX, posY), yellow
angulo += Deg72
Next
Else
largo *= ScaleFactor
dist = largo * (1 + Cos(Deg72) * 2)
For i = 0 To 4
posX += Fix(dist * Cos(angulo))
posY -= Fix(dist * Sin(angulo))
DrawPentagon(posX, posY, largo, fondo-1)
angulo += deg72
Next
End If
End Sub
 
Screenres 640, 640, 32
 
ScaleFactor = 1 / (2 + Cos(Deg72) * 2)
Dim As Single largo
largo = radio * Sin(Pi/5) * 2
DrawPentagon (HW, 3*tam, largo, orden-1)
 
Windowtitle "Hit any key to end program"
Sleep</syntaxhighlight>
 
=={{header|Go}}==
Line 522 ⟶ 600:
 
As output is to an external .png file, only a pentaflake of order 5 is drawn though pentaflakes of lower orders can still be drawn by setting the 'order' variable to the appropriate figure.
<langsyntaxhighlight lang="go">package main
 
import (
Line 582 ⟶ 660:
drawPentagon(hw, 3*margin, side, order-1)
dc.SavePNG("sierpinski_pentagon.png")
}</langsyntaxhighlight>
 
{{out}}
Line 592 ⟶ 670:
For universal solution see [[Fractal tree#Haskell]]
 
<langsyntaxhighlight lang="haskell">import Graphics.Gloss
 
pentaflake :: Int -> Picture
Line 604 ⟶ 682:
 
main = display dc white (Color blue $ Scale 300 300 $ pentaflake 5)
where dc = InWindow "Pentaflake" (400, 400) (0, 0)</langsyntaxhighlight>
 
'''Explanation''': Since <tt>Picture</tt> forms a monoid with image overlaying as multiplication, so do functions having type <tt>Picture -> Picture</tt>:
Line 614 ⟶ 692:
 
If one wants to get all intermediate pentaflakes <code>transformation</code> shoud be changed as follows:
<langsyntaxhighlight lang="haskell">transformation = Scale s s . (Rotate 36 <> foldMap copy [0,72..288])</langsyntaxhighlight>
 
See also the implementation using [http://projects.haskell.org/diagrams/gallery/Pentaflake.html Diagrams]
Line 621 ⟶ 699:
[[File:sierpinski_pentagon.png|300px|thumb|right]]
{{works with|Java|8}}
<langsyntaxhighlight lang="java">import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.geom.Path2D;
Line 729 ⟶ 807:
return Color.getHSBColor((float) hue, 1, 1);
}
}</langsyntaxhighlight>
 
=={{header|JavaScript}}==
Line 739 ⟶ 817:
 
 
<langsyntaxhighlight lang="html">
<html>
<head>
Line 828 ⟶ 906:
</body>
</html>
</langsyntaxhighlight>
 
{{Output}}
Line 838 ⟶ 916:
=={{header|Julia}}==
{{trans|Perl}}
<langsyntaxhighlight lang="julia">using Printf
 
const sides = 5
Line 863 ⟶ 941:
 
print(fh, "</svg>")
close(fh)</langsyntaxhighlight>
 
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">// version 1.1.2
 
import java.awt.*
Line 970 ⟶ 1,048:
f.isVisible = true
}
}</langsyntaxhighlight>
 
=={{header|Lua}}==
An ASCII-interpretation of the task. Uses the Bitmap class and text renderer from [[Bitmap/Bresenham's_line_algorithm#Lua|here]].
<langsyntaxhighlight lang="lua">Bitmap.chaosgame = function(self, n, r, niters)
local w, h, vertices = self.width, self.height, {}
for i = 1, n do
Line 993 ⟶ 1,071:
local bitmap = Bitmap(128, 128)
bitmap:chaosgame(5, 1/((1+math.sqrt(5))/2), 1e6)
bitmap:render({[0x000000]='..', [0xFFFFFFFF]='██'})</langsyntaxhighlight>
{{out}}
Shown at 25% scale:
Line 1,126 ⟶ 1,204:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight lang="mathematica">pentaFlake[0] = RegularPolygon[5];
pentaFlake[n_] := GeometricTransformation[pentaFlake[n - 1], TranslationTransform /@ CirclePoints[{GoldenRatio^(2 n - 1), Pi/10}, 5]]
Graphics@pentaFlake[4]</langsyntaxhighlight>
{{out}}
https://i.imgur.com/rvXvQc0.png
 
=={{header|MATLAB}}==
<langsyntaxhighlight MATLABlang="matlab">[x, x0] = deal(exp(1i*(0.5:.4:2.1)*pi));
for k = 1 : 4
x = x(:) + x0 * (1 + sqrt(5)) * (3 + sqrt(5)) ^(k - 1) / 2 ^ k;
end
patch('Faces', reshape(1 : 5 * 5 ^ k, 5, '')', 'Vertices', [real(x(:)) imag(x(:))])
axis image off</langsyntaxhighlight>
{{out}}
http://i.imgur.com/8ht6HqG.png
Line 1,145 ⟶ 1,223:
{{trans|Go}}
{{libheader|imageman}}
<langsyntaxhighlight Nimlang="nim">import math
import imageman
 
Line 1,191 ⟶ 1,269:
let side = radius * sin(PI / 5) * 2
image.drawPentagon(hw, 3 * margin, side, order - 1)
image.savePNG("Sierpinski_pentagon.png", compression = 9)</langsyntaxhighlight>
 
{{out}}
Line 1,199 ⟶ 1,277:
{{libheader|ntheory}}
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use ntheory qw(todigits);
use Math::Complex;
 
Line 1,233 ⟶ 1,311:
 
print $fh '</svg>';
close $fh;</langsyntaxhighlight>
[https://github.com/SqrtNegInf/Rosettacode-Perl5-Smoke/blob/master/ref/sierpinski_pentagon.svg Sierpinski pentagon] (offsite image)
 
Line 1,239 ⟶ 1,317:
{{libheader|Phix/pGUI}}
{{libheader|Phix/online}}
You can run this online [http://phix.x10.mx/p2js/SierpinskyPentagon.htm here]. Use +/- to change the level, 0..5.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\SierpinskyPentagon.exw
Line 1,336 ⟶ 1,414:
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</langsyntaxhighlight>-->
 
=={{header|Processing}}==
<langsyntaxhighlight lang="java">
float s_angle, scale, margin = 25, total = 4;
float p_size = 700;
Line 1,379 ⟶ 1,457:
}
}
}</langsyntaxhighlight>'''The sketch can be run online''' :<BR> [https://www.openprocessing.org/sketch/955331 here.]
 
=={{header|Prolog}}==
{{works with|SWI Prolog}}
This code is based on the Java solution. The output is an SVG file.
<langsyntaxhighlight lang="prolog">main:-
write_sierpinski_pentagon('sierpinski_pentagon.svg', 600, 5).
 
Line 1,433 ⟶ 1,511:
Angle1 is Angle + 2 * pi/5,
sierpinski_pentagon(Stream, X1, Y1, Scale_factor, Side, N),
sierpinski_pentagons(Stream, X1, Y1, Scale_factor, Side, Angle1, N, I1).</langsyntaxhighlight>
 
{{out}}
[[Media:Sierpinski_pentagon_prolog.svg]]
See: [https://slack-files.com/T0CNUL56D-F017EQ2D1K2-f0b29621ab sierpinski_pentagon.svg] (offsite SVG image)
 
=={{header|Python}}==
Draws the result on a canvas. Runs pretty slowly.
 
<langsyntaxhighlight lang="python">from turtle import *
import math
speed(0) # 0 is the fastest speed. Otherwise, 1 (slow) to 10 (fast)
Line 1,515 ⟶ 1,593:
sierpinski(i, t, size)
 
main()</langsyntaxhighlight>
 
See [https://trinket.io/python/5137ae2b92 online implementation]. See [http://i.imgur.com/96D0c7i.png completed output].
 
=={{header|Quackery}}==
<langsyntaxhighlight Quackerylang="quackery">[ $ "turtleduck.qky" loadfile ] now!
 
[ [ 1 1
Line 1,549 ⟶ 1,627:
 
turtle
0 frames
3 10 turn
300 1 fly
2 5 turn
' [ 79 126 229 ] colour
400 1 5 pentaflake</lang>
1 frames</syntaxhighlight>
 
{{output}}
 
[[File:Quackery Sierpinski pentaflake.png]]
https://imgur.com/1U7chba
 
=={{header|Racket}}==
{{trans|Java}}
 
<langsyntaxhighlight lang="racket">#lang racket/base
(require racket/draw pict racket/math racket/class)
 
Line 1,618 ⟶ 1,698:
(dc-draw-pentagon 3 120 120)
(dc-draw-pentagon 4 120 120)
(dc-draw-pentagon 5 640 640)</langsyntaxhighlight>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|rakudo|2018-10}}
[[File:Perl6 pentaflake.svg|300x300px|thumb|right|5th order pentaflake]]
<lang perl6>constant $sides = 5;
<syntaxhighlight lang="raku" line>constant $sides = 5;
constant order = 5;
constant $dim = 250;
Line 1,643 ⟶ 1,724:
 
$fh.say: '</svg>';
$fh.close;</langsyntaxhighlight>
 
See [http://rosettacode.org/mw/images/5/57/Perl6_pentaflake.svg 5th order pentaflake]
 
=={{header|Ruby}}==
Line 1,651 ⟶ 1,730:
{{libheader|JRubyArt}}
JRubyArt is a port of processing to ruby
<langsyntaxhighlight lang="ruby">
THETA = Math::PI * 2 / 5
SCALE_FACTOR = (3 - Math.sqrt(5)) / 2
Line 1,731 ⟶ 1,810:
end
 
</syntaxhighlight>
</lang>
 
=={{header|Rust}}==
This code is based on the Java solution. The output is a file in SVG format.
<langsyntaxhighlight lang="rust">// [dependencies]
// svg = "0.8.0"
 
Line 1,807 ⟶ 1,886:
fn main() {
write_sierpinski_pentagon("sierpinski_pentagon.svg", 600, 5).unwrap();
}</langsyntaxhighlight>
 
{{out}}
[[Media:Sierpinski_pentagon_rust.svg]]
See: [https://slack-files.com/T0CNUL56D-F016A745N87-f57f7e3b2a sierpinski_pentagon.svg] (offsite SVG image)
 
=={{header|Scala}}==
===Java Swing Interoperability===
<langsyntaxhighlight Scalalang="scala">import java.awt._
import java.awt.event.ActionEvent
import java.awt.geom.Path2D
Line 1,920 ⟶ 1,999:
})
 
}</langsyntaxhighlight>
 
=={{header|Sidef}}==
{{trans|Raku}}
Generates a SVG image to STDOUT. Redirect to a file to capture and display it.
<langsyntaxhighlight lang="ruby">define order = 5
define sides = 5
define dim = 500
Line 1,948 ⟶ 2,027:
}
 
say '</svg>'</langsyntaxhighlight>
 
=={{header|VBA}}==
Using Excel
<langsyntaxhighlight lang="vb">Private Sub sierpinski(Order_ As Integer, Side As Double)
Dim Circumradius As Double, Inradius As Double
Dim Height As Double, Diagonal As Double, HeightDiagonal As Double
Line 1,998 ⟶ 2,077:
Public Sub main()
sierpinski Order_:=5, Side:=200
End Sub</langsyntaxhighlight>
 
=={{header|Wren}}==
Line 2,004 ⟶ 2,083:
{{libheader|DOME}}
Black backgound and slightly different palette to Go. Also pentagons are unfilled.
<langsyntaxhighlight ecmascriptlang="wren">import "graphics" for Canvas, Color
import "dome" for Window
import "math" for Math
Line 2,065 ⟶ 2,144:
}
 
var Game = SierpinskiPentagon.new(640, 640)</langsyntaxhighlight>
 
{{out}}
[[File:Wren-Sierpinski_pentagon.png|400px]]
 
=={{header|XPL0}}==
{{trans|Wren}}
[[File:SierXPL0.gif|200px|thumb|right]]
<syntaxhighlight lang "XPL0">def Order = 5; \can also set this to 1, 2, 3, or 4
def Width=640, Height=640;
def Pi = 3.14159265358979323846;
def Deg72 = 72.*Pi/180.; \72 degrees in radians
def HW = Width/2;
def Margin = 20;
def Radius = HW - 2*Margin;
real ScaleFactor;
int ColorIndex;
 
proc DrawPentagon(X, Y, Side, Depth);
real X, Y, Side; int Depth;
real Angle, Dist;
int I;
[Angle:= 3. * Deg72;
if Depth = 0 then
[Move(fix(X), fix(Y));
for I:= 0 to 4 do
[X:= X + Cos(Angle) * Side;
Y:= Y - Sin(Angle) * Side;
Line(fix(X), fix(Y), ColorIndex+9);
Angle:= Angle + Deg72;
];
ColorIndex:= ColorIndex+1;
if ColorIndex >= 5 then ColorIndex:= 0;
]
else [Side:= Side * ScaleFactor;
Dist:= Side * (1. + Cos(Deg72) * 2.);
for I:= 0 to 4 do
[X:= X + Cos(Angle) * Dist;
Y:= Y - Sin(Angle) * Dist;
DrawPentagon(X, Y, Side, Depth-1);
Angle:= Angle + Deg72;
];
];
];
 
real Side;
[SetFB(Width, Height, 8);
ScaleFactor:= 1. / (2. + Cos(Deg72) * 2.);
ColorIndex:= 0;
Side:= float(Radius) * Sin(Pi/5.) * 2.;
DrawPentagon(float(HW), float(3*Margin), Side, Order-1);
]</syntaxhighlight>
=={{header|zkl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="zkl">const order=5, sides=5, dim=250, scaleFactor=((3.0 - (5.0).pow(0.5))/2);
const tau=(0.0).pi*2; // 2*pi*r
orders:=order.pump(List,fcn(n){ (1.0 - scaleFactor)*dim*scaleFactor.pow(n) });
Line 2,098 ⟶ 2,228:
0'|<polygon points="%s"/>|.fmt(
vertices.pump(String,fcn(v){ "%.3f %.3f ".fmt(v.xplode()) }) )
}</langsyntaxhighlight>
{{out}}
See [http://www.zenkinetic.com/Images/RosettaCode/sierpinskiPentagon.zkl.svg this image].
9,476

edits