Sierpinski pentagon: Difference between revisions

(→‎{{header|Wren}}: Added image)
 
(48 intermediate revisions by 19 users not shown)
Line 7:
* [http://ecademy.agnesscott.edu/~lriddle/ifs/pentagon/pentagon.htm Sierpinski pentagon]
<br><br>
 
=={{header|Action!}}==
<syntaxhighlight lang="action!">PROC Main()
INT ARRAY xs=[249 200 96 80 175]
BYTE ARRAY ys=[82 176 159 55 7]
INT x,y
BYTE i,CH=$02FC,COLOR1=$02C5,COLOR2=$02C6
 
Graphics(8+16)
Color=1
COLOR1=$0C
COLOR2=$02
 
x=160+Rand(30)
y=96+Rand(30)
DO
i=Rand(5)
x=x+(xs(i)-x)*62/100
y=y+(ys(i)-y)*62/100
Plot(x,y)
UNTIL CH#$FF
OD
CH=$FF
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Sierpinski_pentagon.png Screenshot from Atari 8-bit computer]
 
=={{header|AutoHotkey}}==
{{trans|Go}}
Requires [https://www.autohotkey.com/boards/viewtopic.php?t=6517 Gdip Library]
<syntaxhighlight lang="autohotkey">W := H := 640
hw := W / 2
margin := 20
radius := hw - 2 * margin
side := radius * Sin(PI := 3.141592653589793 / 5) * 2
order := 5
 
gdip1()
drawPentagon(hw, 3*margin, side, order, 1)
return
 
drawPentagon(x, y, side, depth, colorIndex){
global G, hwnd1, hdc, Width, Height
Red := "0xFFFF0000"
Green := "0xFF00FF00"
Blue := "0xFF0000FF"
Magenta := "0xFFFF00FF"
Cyan := "0xFF00FFFF"
Black := "0xFF000000"
Palette := [Red, Green, Blue, Magenta, Cyan]
PI := 3.141592653589793
Deg72 := 72 * PI/180
angle := 3 * Deg72
ScaleFactor := 1 / ( 2 + Cos(Deg72) * 2)
points .= x "," y
if (depth = 1) {
loop 5 {
prevx := x
prevy := y
x += Cos(angle) * side
y -= Sin(angle) * side
points .= "|" x "," y
pPen := Gdip_CreatePen(Black, 2)
Gdip_DrawLines(G, pPen, prevx "," prevy "|" x "," y)
angle += Deg72
}
pBrush := Gdip_BrushCreateSolid(Palette[colorIndex])
Gdip_FillPolygon(G, pBrush, Points)
UpdateLayeredWindow(hwnd1, hdc, 0, 0, Width, Height)
}
else{
side *= ScaleFactor
dist := side * (1 + (Cos(Deg72)*2))
loop 5{
x += Cos(angle) * dist
y -= Sin(angle) * dist
colorIndex := Mod(colorIndex+1, 5) + 1
drawPentagon(x, y, side, depth-1, colorIndex)
angle += Deg72
}
}
}
; ---------------------------------------------------------------
gdip1(){
global
If !pToken := Gdip_Startup(){
MsgBox, 48, gdiplus error!, Gdiplus failed to start. Please ensure you have gdiplus on your system
ExitApp
}
OnExit, Exit
Width := 640, Height := 640
Gui, 1: -Caption +E0x80000 +LastFound +OwnDialogs +Owner +AlwaysOnTop
Gui, 1: Show, NA
hwnd1 := WinExist()
hbm := CreateDIBSection(Width, Height)
hdc := CreateCompatibleDC()
obm := SelectObject(hdc, hbm)
G := Gdip_GraphicsFromHDC(hdc)
Gdip_SetSmoothingMode(G, 4)
blackCanvas := Gdip_BrushCreateSolid(0xFFFFFFFF)
Gdip_FillRectangle(G, blackCanvas, 0, 0, Width, Height)
UpdateLayeredWindow(hwnd1, hdc, 0, 0, Width, Height)
}
; ---------------------------------------------------------------
gdip2(){
global
Gdip_DeleteBrush(pBrush)
Gdip_DeletePen(pPen)
SelectObject(hdc, obm)
DeleteObject(hbm)
DeleteDC(hdc)
Gdip_DeleteGraphics(G)
}
; ---------------------------------------------------------------
Esc::
GuiEscape:
Exit:
gdip2()
Gdip_Shutdown(pToken)
ExitApp
Return</syntaxhighlight>
 
=={{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 72 ⟶ 194:
return 0;
}
</syntaxhighlight>
</lang>
 
=={{header|C++}}==
{{trans|D}}
<syntaxhighlight lang="cpp">#include <iomanip>
#include <iostream>
 
#define _USE_MATH_DEFINES
#include <math.h>
 
constexpr double degrees(double deg) {
const double tau = 2.0 * M_PI;
return deg * tau / 360.0;
}
 
const double part_ratio = 2.0 * cos(degrees(72));
const double side_ratio = 1.0 / (part_ratio + 2.0);
 
/// Define a position
struct Point {
double x, y;
 
friend std::ostream& operator<<(std::ostream& os, const Point& p);
};
 
std::ostream& operator<<(std::ostream& os, const Point& p) {
auto f(std::cout.flags());
os << std::setprecision(3) << std::fixed << p.x << ',' << p.y << ' ';
std::cout.flags(f);
return os;
}
 
/// Mock turtle implementation sufficiant to handle "drawing" the pentagons
struct Turtle {
private:
Point pos;
double theta;
bool tracing;
 
public:
Turtle() : theta(0.0), tracing(false) {
pos.x = 0.0;
pos.y = 0.0;
}
 
Turtle(double x, double y) : theta(0.0), tracing(false) {
pos.x = x;
pos.y = y;
}
 
Point position() {
return pos;
}
void position(const Point& p) {
pos = p;
}
 
double heading() {
return theta;
}
void heading(double angle) {
theta = angle;
}
 
/// Move the turtle through space
void forward(double dist) {
auto dx = dist * cos(theta);
auto dy = dist * sin(theta);
 
pos.x += dx;
pos.y += dy;
 
if (tracing) {
std::cout << pos;
}
}
 
/// Turn the turtle
void right(double angle) {
theta -= angle;
}
 
/// Start/Stop exporting the points of the polygon
void begin_fill() {
if (!tracing) {
std::cout << "<polygon points=\"";
tracing = true;
}
}
void end_fill() {
if (tracing) {
std::cout << "\"/>\n";
tracing = false;
}
}
};
 
/// Use the provided turtle to draw a pentagon of the specified size
void pentagon(Turtle& turtle, double size) {
turtle.right(degrees(36));
turtle.begin_fill();
for (size_t i = 0; i < 5; i++) {
turtle.forward(size);
turtle.right(degrees(72));
}
turtle.end_fill();
}
 
/// Draw a sierpinski pentagon of the desired order
void sierpinski(int order, Turtle& turtle, double size) {
turtle.heading(0.0);
auto new_size = size * side_ratio;
 
if (order-- > 1) {
// create four more turtles
for (size_t j = 0; j < 4; j++) {
turtle.right(degrees(36));
 
double small = size * side_ratio / part_ratio;
auto distList = { small, size, size, small };
auto dist = *(distList.begin() + j);
 
Turtle spawn{ turtle.position().x, turtle.position().y };
spawn.heading(turtle.heading());
spawn.forward(dist);
 
// recurse for each spawned turtle
sierpinski(order, spawn, new_size);
}
 
// recurse for the original turtle
sierpinski(order, turtle, new_size);
} else {
// The bottom has been reached for this turtle
pentagon(turtle, size);
}
if (order > 0) {
std::cout << '\n';
}
}
 
/// Run the generation of a P(5) sierpinksi pentagon
int main() {
const int order = 5;
double size = 500;
 
Turtle turtle{ size / 2.0, size };
 
std::cout << "<?xml version=\"1.0\" standalone=\"no\"?>\n";
std::cout << "<!DOCTYPE svg PUBLIC \" -//W3C//DTD SVG 1.1//EN\"\n";
std::cout << " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n";
std::cout << "<svg height=\"" << size << "\" width=\"" << size << "\" style=\"fill:blue\" transform=\"translate(" << size / 2 << ", " << size / 2 << ") rotate(-36)\"\n";
std::cout << " version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\">\n";
 
size *= part_ratio;
sierpinski(order, turtle, size);
 
std::cout << "</svg>";
}</syntaxhighlight>
 
=={{header|D}}==
{{trans|Perl 6Raku}}
{{trans|Python}}
This solution combines the turtle graphics concept used in Python, with the SVG output format of the Perl 6Raku solution.
This runs very quickly compared to the Python version.
 
<langsyntaxhighlight Dlang="d">import std.math;
import std.stdio;
 
Line 234 ⟶ 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 242 ⟶ 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 302 ⟶ 660:
drawPentagon(hw, 3*margin, side, order-1)
dc.SavePNG("sierpinski_pentagon.png")
}</langsyntaxhighlight>
 
{{out}}
Line 312 ⟶ 670:
For universal solution see [[Fractal tree#Haskell]]
 
<langsyntaxhighlight lang="haskell">import Graphics.Gloss
 
pentaflake :: Int -> Picture
Line 324 ⟶ 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 334 ⟶ 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 341 ⟶ 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 449 ⟶ 807:
return Color.getHSBColor((float) hue, 1, 1);
}
}</langsyntaxhighlight>
 
=={{header|JavaScript}}==
Line 459 ⟶ 817:
 
 
<langsyntaxhighlight lang="html">
<html>
<head>
Line 548 ⟶ 906:
</body>
</html>
</langsyntaxhighlight>
 
{{Output}}
Line 558 ⟶ 916:
=={{header|Julia}}==
{{trans|Perl}}
<langsyntaxhighlight lang="julia">using Printf
 
const sides = 5
Line 567 ⟶ 925:
const orders = map(x -> ((1 - scale) * dim) * scale ^ x, 0:order-1)
cis(x) = Complex(cos(x), sin(x))
const vertices = map(x -> cis(x * τ / sides), 0:sides-1)
 
fh = open("sierpinski_pentagon.svg", "w")
Line 576 ⟶ 934:
varr = [vertices[parse(Int, ch) + 1] for ch in split(string(i, base=sides, pad=order), "")]
vector = sum(map(x -> varr[x] * orders[x], 1:length(orders)))
vprod = map(x -> vector + orders[end] * (1-scale) * x, vertices)
 
points = join([@sprintf("%.3f %.3f", real(v), imag(v)) for v in vprod], " ")
Line 583 ⟶ 941:
 
print(fh, "</svg>")
close(fh)</langsyntaxhighlight>
 
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">// version 1.1.2
 
import java.awt.*
Line 690 ⟶ 1,048:
f.isVisible = true
}
}</langsyntaxhighlight>
 
=={{header|MathematicaLua}}==
An ASCII-interpretation of the task. Uses the Bitmap class and text renderer from [[Bitmap/Bresenham's_line_algorithm#Lua|here]].
<lang mathematica>pentaFlake[0] = RegularPolygon[5];
<syntaxhighlight lang="lua">Bitmap.chaosgame = function(self, n, r, niters)
pentaFlake[n_] :=
local w, h, vertices = self.width, self.height, {}
GeometricTransformation[pentaFlake[n - 1],
for i = 1, n do
TranslationTransform /@
vertices[i] = {
CirclePoints[{GoldenRatio^(2 n - 1), Pi/10}, 5]]
x = w/2 + w/2 * math.cos(math.pi/2+(i-1)*math.pi*2/n),
y = h/2 - h/2 * math.sin(math.pi/2+(i-1)*math.pi*2/n)
}
end
local x, y = w/2, h/2
for i = 1, niters do
local v = math.random(n)
x = x + r * (vertices[v].x - x)
y = y + r * (vertices[v].y - y)
self:set(x,y,0xFFFFFFFF)
end
end
 
local bitmap = Bitmap(128, 128)
bitmap:chaosgame(5, 1/((1+math.sqrt(5))/2), 1e6)
bitmap:render({[0x000000]='..', [0xFFFFFFFF]='██'})</syntaxhighlight>
{{out}}
Shown at 25% scale:
<pre style="font-size:25%">............................................................................................................................████████............................................................................................................................
..........................................................................................................................████████████..........................................................................................................................
........................................................................................................................████████████████........................................................................................................................
....................................................................................................................██..██████....██████........................................................................................................................
..................................................................................................................██████..████████████..██████..................................................................................................................
..............................................................................................................████████████████████████████████████..............................................................................................................
..............................................................................................................████████████████████████████████████..............................................................................................................
..............................................................................................................██████..██████........██████..██████..............................................................................................................
..............................................................................................................██████████████........██████████████..............................................................................................................
..............................................................................................................██████████████........██████████████..............................................................................................................
..................................................................................................████..........████████████........████████████..........████..................................................................................................
..............................................................................................██████████............██████████....██████████............██████████..............................................................................................
............................................................................................████████████████......████████████████████████████......████████████████............................................................................................
............................................................................................██████..██████........██████..████████████..██████........██████..██████............................................................................................
......................................................................................████..██████..██████..████..██████..████████████..██████..████..██████..██████..████......................................................................................
....................................................................................████████████████████████████████████████████████████████████████████████████████████████....................................................................................
................................................................................██████████████████████████████████████████████....██████████████████████████████████████████████................................................................................
................................................................................████████..██████......████████████████....................██████..████████......██████..████████................................................................................
..................................................................................██████████████........██████████████....................██████████████........██████████████..................................................................................
..................................................................................████████████..........████████████........................████████████..........████████████..................................................................................
..................................................................................████████████..........████████████........................████████████..........████████████..................................................................................
........................................................................................████████......████████....................................████████......████████........................................................................................
......................................................................................████████████..████████████................................████████████..████████████......................................................................................
....................................................................................██████████████████████████████............................██████████████████████████████....................................................................................
......................................................................................██████..██████████....██████............................██████....██████████████████......................................................................................
......................................................................................████████████..████████████................................████████████..████████████......................................................................................
......................................................................................████████████..████████████................................████████████..████████████......................................................................................
..................................................██████................................██████████..████████████................................████████████..██████████................................██████..................................................
................................................██████████..............................................████████████........................████████████..............................................██████████................................................
..............................................██████████████..........................................████████████████....................████████████████..........................................██████████████..............................................
..............................................██████..██████..........................................████████..██████....................██████..████████..........................................██████..██████..............................................
........................................████..██████████████..████..............................██████..████████████████████........████████████████████..██████..............................████..██████████████..██████......................................
....................................██████████████████████████████████........................████████████████████████████████....████████████████████████████████........................██████████████████████████████████....................................
..................................██████████████████████████████████████....................████████████████████████████████████████████████████████████████████████....................██████████████████████████████████████..................................
..................................██████..████████......██████..████████....................██████..██████........██████..████████████..██████........██████..██████....................████████..██████......████████..██████..................................
..................................██████████████........██████████████......................██████████████........████████████████████████████........██████████████......................██████████████........██████████████..................................
....................................████████████..........████████████........................████████████..........████████████████████████..........████████████........................████████████..........████████████....................................
......................████..........████████████..........████████████..........████..........██████████............██████████....██████████............██████████............██..........████████████..........████████████..........████......................
....................████████..............████████......████████..............████████..............████████......██████................██████........██████..............████████..............████████......████████..............████████....................
................████████████████........██████████████████████████........██████████████........████████████████████████████........████████████████████████████........██████████████........████████████████████████████......██████████████..................
................████████████████......██████████████████████████████......████████████████......████████████████████████████........████████████████████████████......████████████████......██████████████████████████████......████████████████................
............██..██████....██████..██..██████....██████████..██████..████..██████..██████..████..██████..████████████..██████........██████..████████████..██████..██....██████..██████..████..██████..██████████....██████..██..██████....██████..██............
..........██████..████████████..██████..████████████..████████████████████..████████████████████..██████████████████████████........██████████████████████████..████████████████████..████████████████████..████████████..██████..████████████..██████..........
......██████████████████████████████████████████████..████████████████████████████████████████████████████████..██████████............██████████..████████████████████████████████████████████████████████..██████████████████████████████████████████████......
......██████████████████████████████████████████████..██████████████████████████████████████████████████████....██████████............██████████....██████████████████████████████████████████████████████..██████████████████████████████████████████████......
......██████..██████........██████..██████......................██████..██████........██████..██████........................................................██████..██████........██████..██████......................██████..██████........██████..██████......
......██████████████........██████████████......................████████████..........████████████............................................................████████████..........████████████......................██████████████........██████████████......
........██████████............██████████........................████████████..........████████████............................................................████████████..........████████████........................██████████............██████████........
........████████████........████████████..........................████████████........████████████............................................................████████████........████████████..........................████████████........████████████........
............██████████..████████████................................████████████..████████████....................................................................████████████..████████████................................████████████████████████............
..........████████████████████████████............................██████████████████████████████................................................................██████████████████████████████............................████████████████████████████..........
..........██████..████████████..██████..............................██████..████████████..██████................................................................██████..████████████..██████..............................██████..████████████..██████..........
..........████████████████████████████..............................████████████████████████████................................................................████████████████████████████..............................████████████████████████████..........
............████████████████████████................................████████████..████████████....................................................................████████████..████████████................................████████████████████████............
............██████████....██████████................................████████████..████████████....................................................................████████████..████████████................................██████████....██████████............
................................██████............................████████............................................................................................................████████............................██████................................
............................██████████████......................██████████████....................................................................................................██████████████......................██████████████............................
............................██████████████....................████████████████....................................................................................................████████████████....................██████████████............................
......................████..██████..██████..████..........████..██████..██████..████........................................................................................████..██████..██████..████..........████..██████..██████..████......................
....................██████████████████████████████......████████████████████..████████....................................................................................████████..████████████████████......██████████████████████████████....................
................████████████████████████████████████████████████████████████████████████................................................................................████████████████████████████████████████████████████████████████████████................
................██████████████████████████████████████████████████████████████████████████............................................................................██████████████████████████████████████████████████████████████████████████................
................████████████████......████████████████████..██████........██████..██████................................................................................██████..██████........██████..████████████████████......████████████████................
..................████████████..........████████████..████████████..........████████████................................................................................████████████..........████████████..████████████..........████████████..................
..................████████████..........████████████..████████████..........████████████................................................................................████████████..........████████████..████████████....██....████████████..................
..................██████████████......██████████████..██████████████......████████████....................................................................................████████████......██████████████..██████████████......██████████████..................
......................████████████..████████████..........████████████..████████████........................................................................................████████████..████████████..........████████████..████████████......................
....................██████████████████████████████......██████████████████████████████....................................................................................██████████████████████████████......██████████████████████████████....................
....................████████..██████████..████████......██████..████████████..██████........................................................................................██████..████████████..██████......████████..██████████..████████....................
......................██████████████████████████........████████████████████████████........................................................................................████████████████████████████........██████████████████████████......................
......................████████████..████████████..........████████████..████████████........................................................................................████████████..████████████..........████████████..████████████......................
......................████████████..████████████..........████████████..████████████........................................................................................████████████..████████████..........████████████..████████████......................
..............................................................................████████....................................................................................████████..............................................................................
..........................................................................████████████████............................................................................████████████████..........................................................................
..........................................................................████████████████............................................................................████████████████..........................................................................
....................................................................████..██████..██████..████....................................................................████..██████..██████..████....................................................................
....................................................................██████..████████████████████................................................................████████████████████..████████..................................................................
..............................................................██████████████████████████████████████........................................................██████████████████████████████████████..............................................................
..............................................................██████████████████████████████████████........................................................██████████████████████████████████████..............................................................
................................................................██████..██████........██████..██████........................................................██████..██████........██████..██████................................................................
................................................................████████████..........████████████............................................................████████████..........████████████................................................................
................................................................████████████..........████████████............................................................████████████..........████████████................................................................
..................................................██████................██████......████████..............████████............................████████..............████████......██████................██████..................................................
..............................................████████████..........████████████..████████████..........████████████........................████████████..........████████████..████████████..........████████████..............................................
............................................████████████████......██████████████████████████████......████████████████....................████████████████......██████████████████████████████......████████████████............................................
..............................................██████..██████..██....██████..████████████..██████..██..████████..██████................██..██████..████████..██..██████..████████████..██████........██████..██████....██........................................
......................................████████████████████..████████████████████..████████████..██████..████████████..██████........██████..████████████..██████..████████████..████████████████████..████████████████████......................................
....................................████████████████████████████████████████████..████████████████████████████████████████████████████████████████████████████████████████████..████████████████████████████████████████████....................................
..................................██████████████████████████████████████████████..████████████████████████████████████████████████████████████████████████████████████████████..██████████████████████████████████████████████..................................
..................................██████..████████......██████..████████....................██████..██████........██████..████████████..██████........██████..██████....................████████..██████......████████..██████..................................
....................................████████████..........████████████........................████████████..........████████████████████████..........████████████........................████████████..........████████████....................................
....................................████████████..........████████████........................████████████..........████████████████████████..........████████████........................████████████..........████████████....................................
....................................████████████..........████████████........................████████████..........██████████....██████████..........████████████........................████████████..........████████████....................................
........................................████████████..██████████..................................██████████....██████████............██████████....██████████..................................██████████..████████████........................................
......................................██████████████████████████████............................████████████████████████████........████████████████████████████............................██████████████████████████████......................................
......................................████████████████████..████████............................██████..████████████..██████........██████..████████████..██████............................████████..████████████████████......................................
......................................████████████████████..██████..............................██████..████████████..██████........██████..████████████..██████..............██..............██████..██████████████████........................................
........................................████████████..████████████................................██████████████████████████........██████████████████████████................................████████████..████████████........................................
........................................████████████..████████████................................██████████....██████████............██████████..████████████................................████████████..████████████........................................
............................................................████████............................██████....................................................██████............................████████............................................................
..........................................................████████████........................████████████............................................██████████████......................████████████..........................................................
........................................................████████████████....................████████████████........................................████████████████....................████████████████........................................................
....................................................██..██████....████....██............██..██████..██████..████................................██....██████..██████..████..........██....██████..██████..██....................................................
..................................................██████..████████████..██████......████████..████████████████████............................████████████████████..████████......██████..████████████..██████..................................................
..............................................██████████████████████████████████..████████████████████████████████████......................██████████████████████████████████████████████████████████████████████..............................................
..............................................████████████████████████████████████████████████████████████████████████....................████████████████████████████████████████████████████████████████████████..............................................
..............................................██████..██████........██████..████████████..██████........████....██████....................██████....████........██████..████████████..██████........██████..██████..............................................
..............................................████████████..........████████████..████████████..........████████████........................████████████..........████████████..████████████..........████████████..............................................
..............................................████████████..........████████████..████████████..........████████████........................████████████..........████████████..████████████..........████████████..............................................
................................................████████████........████████████..██████████████........████████████........................████████████........██████████████..████████████........████████████................................................
....................................................██████████....██████████..........████████████..████████████................................████████████..████████████..........██████████....██████████....................................................
..................................................████████████████████████████......██████████████████████████████............................██████████████████████████████......████████████████████████████..................................................
..................................................██████..████████████..██████......████████..████████████████████............................████████████████████..████████......██████..████████████..██████..................................................
..................................................████████████████████████████........██████████████████████████................................██████████████████████████........████████████████████████████..................................................
..................................................██████████████████████████..........████████████..████████████................................████████████..████████████..........██████████████████████████..................................................
....................................................██████████..████████████..........████████████..████████████................................████████████..████████████..........████████████..██████████....................................................
................................................................................................................................................................................................................................................................
................................................................................................................................................................................................................................................................
................................................................................................................................................................................................................................................................
................................................................................................................................................................................................................................................................
................................................................................................................................................................................................................................................................
................................................................................................................................................................................................................................................................
................................................................................................................................................................................................................................................................
................................................................................................................................................................................................................................................................
................................................................................................................................................................................................................................................................
................................................................................................................................................................................................................................................................
................................................................................................................................................................................................................................................................
................................................................................................................................................................................................................................................................</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Graphics@pentaFlake[4]</lang>
<syntaxhighlight lang="mathematica">pentaFlake[0] = RegularPolygon[5];
pentaFlake[n_] := GeometricTransformation[pentaFlake[n - 1], TranslationTransform /@ CirclePoints[{GoldenRatio^(2 n - 1), Pi/10}, 5]]
Graphics@pentaFlake[4]</syntaxhighlight>
{{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
 
=={{header|Nim}}==
{{trans|Go}}
{{libheader|imageman}}
<syntaxhighlight lang="nim">import math
import imageman
 
const
Red = ColorRGBU [byte 255, 0, 0]
Green = ColorRGBU [byte 0, 255, 0]
Blue = ColorRGBU [byte 0, 0, 255]
Magenta = ColorRGBU [byte 255, 0, 255]
Cyan = ColorRGBU [byte 0, 255, 255]
Black = ColorRGBU [byte 0, 0, 0]
 
(W, H) = (640, 640)
Deg72 = degToRad(72.0)
ScaleFactor = 1 / ( 2 + cos(Deg72) * 2)
Palette = [Red, Green, Blue, Magenta, Cyan]
 
 
proc drawPentagon(img: var Image; x, y, side: float; depth: int) =
var (x, y) = (x, y)
var colorIndex {.global.} = 0
var angle = 3 * Deg72
if depth == 0:
for _ in 0..4:
let (prevx, prevy) = (x, y)
x += cos(angle) * side
y -= sin(angle) * side
img.drawLine(prevx.toInt, prevy.toInt, x.toInt, y.toInt, Palette[colorIndex])
angle += Deg72
colorIndex = (colorIndex + 1) mod 5
else:
let side = side * ScaleFactor
let dist = side * (1 + cos(Deg72) * 2)
for _ in 0..4:
x += cos(angle) * dist
y -= sin(angle) * dist
img.drawPentagon(x, y, side, depth - 1)
angle += Deg72
 
var image = initImage[ColorRGBU](W, H)
image.fill(Black)
var order = 5
let hw = W / 2
let margin = 20.0
let radius = hw - 2 * margin
let side = radius * sin(PI / 5) * 2
image.drawPentagon(hw, 3 * margin, side, order - 1)
image.savePNG("Sierpinski_pentagon.png", compression = 9)</syntaxhighlight>
 
{{out}}
Same output as that of Go version except that background is black.
 
=={{header|Perl}}==
{{libheader|ntheory}}
{{trans|Perl 6Raku}}
<langsyntaxhighlight lang="perl">use ntheory qw(todigits);
use Math::Complex;
 
Line 750 ⟶ 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)
 
=={{header|Perl 6Phix}}==
{{libheader|Phix/pGUI}}
{{works with|rakudo|2018-10}}
{{libheader|Phix/online}}
<lang perl6>constant $sides = 5;
You can run this online [http://phix.x10.mx/p2js/SierpinskyPentagon.htm here]. Use +/- to change the level, 0..5.
constant order = 5;
<!--<syntaxhighlight lang="phix">(phixonline)-->
constant $dim = 250;
<span style="color: #000080;font-style:italic;">--
constant scaling-factor = ( 3 - 5**.5 ) / 2;
-- demo\rosetta\SierpinskyPentagon.exw
my @orders = ((1 - scaling-factor) * $dim) «*» scaling-factor «**» (^order);
-- ===================================
--</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</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: #004080;">Ihandle</span> <span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">canvas</span>
<span style="color: #004080;">cdCanvas</span> <span style="color: #000000;">cddbuffer</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">title</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"Sierpinski Pentagon"</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">scale_factor</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">/(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">+</span><span style="color: #7060A8;">cos</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #004600;">PI</span><span style="color: #0000FF;">/</span><span style="color: #000000;">5</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">2</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">side_factor</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">+</span><span style="color: #7060A8;">cos</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #004600;">PI</span><span style="color: #0000FF;">/</span><span style="color: #000000;">5</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">angles</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sq_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #004600;">PI</span><span style="color: #0000FF;">/</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">7</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">)),</span>
<span style="color: #000000;">cosangles</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #000000;">angles</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">cos</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">sinangles</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #000000;">angles</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sin</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">level</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">3</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">drawPentagon</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">side</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">w</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">h</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">depth</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">depth</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">cdCanvasBegin</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span><span style="color: #004600;">CD_FILL</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">5</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">x</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">cosangles</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">*</span> <span style="color: #000000;">side</span>
<span style="color: #000000;">y</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">sinangles</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">*</span> <span style="color: #000000;">side</span>
<span style="color: #7060A8;">cdCanvasVertex</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">w</span><span style="color: #0000FF;">+</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">h</span><span style="color: #0000FF;">-</span><span style="color: #000000;">y</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">cdCanvasEnd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">else</span>
<span style="color: #000000;">side</span> <span style="color: #0000FF;">*=</span> <span style="color: #000000;">scale_factor</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">distance</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">side</span><span style="color: #0000FF;">*</span><span style="color: #000000;">side_factor</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">5</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">x</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">cosangles</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">*</span> <span style="color: #000000;">distance</span>
<span style="color: #000000;">y</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">sinangles</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">*</span> <span style="color: #000000;">distance</span>
<span style="color: #000000;">drawPentagon</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">side</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">w</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">h</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">depth</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">redraw_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000080;font-style:italic;">/*ih*/</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000080;font-style:italic;">/*posx*/</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">/*posy*/</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">w</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">h</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupGetIntInt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"DRAWSIZE"</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">hw</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">min</span><span style="color: #0000FF;">(</span><span style="color: #000000;">w</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">h</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">margin</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">20</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">radius</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">hw</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #000000;">margin</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">side</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">radius</span> <span style="color: #0000FF;">*</span> <span style="color: #7060A8;">sin</span><span style="color: #0000FF;">(</span><span style="color: #004600;">PI</span><span style="color: #0000FF;">/</span><span style="color: #000000;">5</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">*</span> <span style="color: #000000;">2</span>
<span style="color: #7060A8;">cdCanvasActivate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">cdCanvasClear</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">drawPentagon</span><span style="color: #0000FF;">(</span><span style="color: #000000;">hw</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">*</span><span style="color: #000000;">margin</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">side</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">w</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">-</span><span style="color: #000000;">radius</span><span style="color: #0000FF;">-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #000000;">margin</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">h</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">level</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">cdCanvasFlush</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULT</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">map_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000000;">ih</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">cdCanvas</span> <span style="color: #000000;">cdcanvas</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">cdCreateCanvas</span><span style="color: #0000FF;">(</span><span style="color: #004600;">CD_IUP</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ih</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">cddbuffer</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">cdCreateCanvas</span><span style="color: #0000FF;">(</span><span style="color: #004600;">CD_DBUFFER</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">cdcanvas</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">cdCanvasSetBackground</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #004600;">CD_PARCHMENT</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">cdCanvasSetForeground</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #004600;">CD_BLUE</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULT</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">set_dlg_title</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">IupSetStrAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"TITLE"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"%s (level %d)"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">title</span><span style="color: #0000FF;">,</span><span style="color: #000000;">level</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">key_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000080;font-style:italic;">/*ih*/</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=</span><span style="color: #004600;">K_ESC</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #004600;">IUP_CLOSE</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"+-"</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=</span><span style="color: #008000;">'+'</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">level</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">min</span><span style="color: #0000FF;">(</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">level</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=</span><span style="color: #008000;">'-'</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">level</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">max</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">level</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">set_dlg_title</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">IupRedraw</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_CONTINUE</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">IupOpen</span><span style="color: #0000FF;">()</span>
<span style="color: #000000;">canvas</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupCanvas</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"RASTERSIZE=640x640"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupSetCallbacks</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"MAP_CB"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"map_cb"</span><span style="color: #0000FF;">),</span>
<span style="color: #008000;">"ACTION"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"redraw_cb"</span><span style="color: #0000FF;">)})</span>
<span style="color: #000000;">dlg</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupDialog</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupSetCallback</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"KEY_CB"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"key_cb"</span><span style="color: #0000FF;">))</span>
<span style="color: #000000;">set_dlg_title</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">IupShow</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"RASTERSIZE"</span><span style="color: #0000FF;">,</span> <span style="color: #004600;">NULL</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()!=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">IupMainLoop</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</syntaxhighlight>-->
 
=={{header|Processing}}==
my $fh = open('sierpinski_pentagon.svg', :w);
<syntaxhighlight lang="java">
float s_angle, scale, margin = 25, total = 4;
float p_size = 700;
float radius = p_size/2-2*margin;
float side = radius * sin(PI/5)*2;
 
void setup() {
$fh.say: qq|<svg height="{$dim*2}" width="{$dim*2}" style="fill:blue" version="1.1" xmlns="http://www.w3.org/2000/svg">|;
float temp = width/2;
size(590, 590);
background(0, 0, 200);
stroke(255);
s_angle = 72*PI/180;
scale = 1/(2+cos(s_angle)*2);
for (int i = 0; i < total; i++) {
background(0, 0, 200);
drawPentagon(width/2, (height-p_size)/2 + 3*margin, side, total);
}
}
 
void drawPentagon(float x, float y, float side, float depth) {
my @vertices = map { cis( $_ * τ / $sides ) }, ^$sides;
float angle = 3*s_angle;
if (depth == 0) {
for (int i = 0; i < 5; i++) {
float px = x;
float py = y;
x = x+cos(angle)*side;
y = y-sin(angle)*side;
line(x, y, px, py);
angle += s_angle;
}
} else {
side *= scale;
float distance = side+side*cos(s_angle)*2;
for (int j = 0; j < 5; j++) {
x = x+cos(angle)*distance;
y = y-sin(angle)*distance;
drawPentagon(x, y, side, depth-1);
angle += s_angle;
}
}
}</syntaxhighlight>'''The sketch can be run online''' :<BR> [https://www.openprocessing.org/sketch/955331 here.]
 
=={{header|Prolog}}==
for 0 ..^ $sides ** order -> $i {
{{works with|SWI Prolog}}
my $vector = [+] @vertices[$i.base($sides).fmt("%{order}d").comb] «*» @orders;
This code is based on the Java solution. The output is an SVG file.
$fh.say: pgon ((@orders[*-1] * (1 - scaling-factor)) «*» @vertices «+» $vector)».reals».fmt("%0.3f");
<syntaxhighlight lang="prolog">main:-
};
write_sierpinski_pentagon('sierpinski_pentagon.svg', 600, 5).
 
write_sierpinski_pentagon(File, Size, Order):-
sub pgon (@q) { qq|<polygon points="{@q}" transform="translate({$dim},{$dim}) rotate(-18)"/>| }
open(File, write, Stream),
format(Stream, "<svg xmlns='http://www.w3.org/2000/svg' width='~d' height='~d'>\n",
[Size, Size]),
write(Stream, "<rect width='100%' height='100%' fill='white'/>\n"),
Margin = 5,
Radius is Size/2 - 2 * Margin,
Side is Radius * sin(pi/5) * 2,
Height is Side * (sin(pi/5) + sin(2 * pi/5)),
X is Size/2,
Y is (Size - Height)/2,
Scale_factor is 1/(2 + cos(2 * pi/5) * 2),
sierpinski_pentagon(Stream, X, Y, Scale_factor, Side, Order),
write(Stream, "</svg>\n"),
close(Stream).
 
sierpinski_pentagon(Stream, X, Y, _, Side, 1):-
$fh.say: '</svg>';
!,
$fh.close;</lang>
write(Stream, "<polygon stroke-width='1' stroke='black' fill='blue' points='"),
format(Stream, '~g,~g', [X, Y]),
Angle is 6 * pi/5,
write_pentagon_points(Stream, Side, Angle, X, Y, 5),
write(Stream, "'/>\n").
sierpinski_pentagon(Stream, X, Y, Scale_factor, Side, N):-
Side1 is Side * Scale_factor,
N1 is N - 1,
Angle is 6 * pi/5,
sierpinski_pentagons(Stream, X, Y, Scale_factor, Side1, Angle, N1, 5).
 
write_pentagon_points(_, _, _, _, _, 0):-!.
See [http://rosettacode.org/mw/images/5/57/Perl6_pentaflake.svg 5th order pentaflake]
write_pentagon_points(Stream, Side, Angle, X, Y, N):-
N1 is N - 1,
X1 is X + cos(Angle) * Side,
Y1 is Y - sin(Angle) * Side,
Angle1 is Angle + 2 * pi/5,
format(Stream, ' ~g,~g', [X1, Y1]),
write_pentagon_points(Stream, Side, Angle1, X1, Y1, N1).
 
sierpinski_pentagons(_, _, _, _, _, _, _, 0):-!.
sierpinski_pentagons(Stream, X, Y, Scale_factor, Side, Angle, N, I):-
I1 is I - 1,
Distance is Side + Side * cos(2 * pi/5) * 2,
X1 is X + cos(Angle) * Distance,
Y1 is Y - sin(Angle) * Distance,
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).</syntaxhighlight>
 
{{out}}
[[Media:Sierpinski_pentagon_prolog.svg]]
 
=={{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 856 ⟶ 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}}==
<syntaxhighlight lang="quackery">[ $ "turtleduck.qky" loadfile ] now!
 
[ [ 1 1
30 times
[ tuck + ]
swap join ] constant
do ] is phi ( --> n/d )
 
[ 5 times
[ 2dup walk
1 5 turn ]
2drop ] is pentagon ( n/d n --> )
 
forward is pentaflake
 
[ dup 0 = iff
[ drop
' [ 79 126 229 ] fill
pentagon ] done
1 - temp put
5 times
[ 2dup 2 1 phi v- v*
temp share pentaflake
2dup fly
1 5 turn ]
temp release
2drop ] resolves pentaflake ( n/d n --> )
 
turtle
0 frames
3 10 turn
300 1 fly
2 5 turn
' [ 79 126 229 ] colour
400 1 5 pentaflake
1 frames</syntaxhighlight>
 
{{output}}
 
[[File:Quackery Sierpinski pentaflake.png]]
 
=={{header|Racket}}==
{{trans|Java}}
 
<langsyntaxhighlight lang="racket">#lang racket/base
(require racket/draw pict racket/math racket/class)
 
Line 919 ⟶ 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]]
<syntaxhighlight lang="raku" line>constant $sides = 5;
constant order = 5;
constant $dim = 250;
constant scaling-factor = ( 3 - 5**.5 ) / 2;
my @orders = ((1 - scaling-factor) * $dim) «*» scaling-factor «**» (^order);
 
my $fh = open('sierpinski_pentagon.svg', :w);
 
$fh.say: qq|<svg height="{$dim*2}" width="{$dim*2}" style="fill:blue" version="1.1" xmlns="http://www.w3.org/2000/svg">|;
 
my @vertices = map { cis( $_ * τ / $sides ) }, ^$sides;
 
for 0 ..^ $sides ** order -> $i {
my $vector = [+] @vertices[$i.base($sides).fmt("%{order}d").comb] «*» @orders;
$fh.say: pgon ((@orders[*-1] * (1 - scaling-factor)) «*» @vertices «+» $vector)».reals».fmt("%0.3f");
};
 
sub pgon (@q) { qq|<polygon points="{@q}" transform="translate({$dim},{$dim}) rotate(-18)"/>| }
 
$fh.say: '</svg>';
$fh.close;</syntaxhighlight>
 
=={{header|Ruby}}==
{{libheader|RubyGems}}
{{libheader|JRubyArt}}
JRubyArt is a port of processing to ruby
<syntaxhighlight lang="ruby">
THETA = Math::PI * 2 / 5
SCALE_FACTOR = (3 - Math.sqrt(5)) / 2
MARGIN = 20
 
attr_reader :pentagons, :renderer
def settings
size(400, 400)
end
 
def setup
sketch_title 'Pentaflake'
radius = width / 2 - 2 * MARGIN
center = Vec2D.new(radius - 2 * MARGIN, 3 * MARGIN)
pentaflake = Pentaflake.new(center, radius, 5)
@pentagons = pentaflake.pentagons
end
 
def draw
background(255)
stroke(0)
pentagons.each do |penta|
draw_pentagon(penta)
end
no_loop
end
 
def draw_pentagon(pent)
points = pent.vertices
begin_shape
points.each do |pnt|
pnt.to_vertex(renderer)
end
end_shape(CLOSE)
end
 
def renderer
@renderer ||= GfxRender.new(self.g)
end
 
 
class Pentaflake
attr_reader :pentagons
 
def initialize(center, radius, depth)
@pentagons = []
create_pentagons(center, radius, depth)
end
 
def create_pentagons(center, radius, depth)
if depth.zero?
pentagons << Pentagon.new(center, radius)
else
radius *= SCALE_FACTOR
distance = radius * Math.sin(THETA) * 2
(0..4).each do |idx|
x = center.x + Math.cos(idx * THETA) * distance
y = center.y + Math.sin(idx * THETA) * distance
center = Vec2D.new(x, y)
create_pentagons(center, radius, depth - 1)
end
end
end
end
 
class Pentagon
attr_reader :center, :radius
 
def initialize(center, radius)
@center = center
@radius = radius
end
 
def vertices
(0..4).map do |idx|
center + Vec2D.new(radius * Math.sin(THETA * idx), radius * Math.cos(THETA * idx))
end
end
end
 
</syntaxhighlight>
 
=={{header|Rust}}==
This code is based on the Java solution. The output is a file in SVG format.
<syntaxhighlight lang="rust">// [dependencies]
// svg = "0.8.0"
 
fn sierpinski_pentagon(
mut document: svg::Document,
mut x: f64,
mut y: f64,
mut side: f64,
order: usize,
) -> svg::Document {
use std::f64::consts::PI;
use svg::node::element::Polygon;
 
let degrees72 = 0.4 * PI;
let mut angle = 3.0 * degrees72;
let scale_factor = 1.0 / (2.0 + degrees72.cos() * 2.0);
 
if order == 1 {
let mut points = Vec::new();
points.push((x, y));
for _ in 0..5 {
x += angle.cos() * side;
y -= angle.sin() * side;
angle += degrees72;
points.push((x, y));
}
let polygon = Polygon::new()
.set("fill", "blue")
.set("stroke", "black")
.set("stroke-width", "1")
.set("points", points);
document = document.add(polygon);
} else {
side *= scale_factor;
let distance = side + side * degrees72.cos() * 2.0;
for _ in 0..5 {
x += angle.cos() * distance;
y -= angle.sin() * distance;
angle += degrees72;
document = sierpinski_pentagon(document, x, y, side, order - 1);
}
}
document
}
 
fn write_sierpinski_pentagon(file: &str, size: usize, order: usize) -> std::io::Result<()> {
use std::f64::consts::PI;
use svg::node::element::Rectangle;
 
let margin = 5.0;
let radius = (size as f64) / 2.0 - 2.0 * margin;
let side = radius * (0.2 * PI).sin() * 2.0;
let height = side * ((0.2 * PI).sin() + (0.4 * PI).sin());
let x = (size as f64) / 2.0;
let y = (size as f64 - height) / 2.0;
 
let rect = Rectangle::new()
.set("width", "100%")
.set("height", "100%")
.set("fill", "white");
 
let mut document = svg::Document::new()
.set("width", size)
.set("height", size)
.add(rect);
 
document = sierpinski_pentagon(document, x, y, side, order);
svg::save(file, &document)
}
 
fn main() {
write_sierpinski_pentagon("sierpinski_pentagon.svg", 600, 5).unwrap();
}</syntaxhighlight>
 
{{out}}
[[Media:Sierpinski_pentagon_rust.svg]]
 
=={{header|Scala}}==
===Java Swing Interoperability===
<langsyntaxhighlight Scalalang="scala">import java.awt._
import java.awt.event.ActionEvent
import java.awt.geom.Path2D
Line 1,029 ⟶ 1,999:
})
 
}</langsyntaxhighlight>
 
=={{header|Sidef}}==
{{trans|Perl 6Raku}}
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,056 ⟶ 2,027:
}
 
say '</svg>'</langsyntaxhighlight>
 
=={{header|VBA}}==
Using Excel
<syntaxhighlight 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
Dim Pi As Double, p(5) As String, Shp As Shape
Circumradius = Sqr(50 + 10 * Sqr(5)) / 10
Inradius = Sqr(25 + 10 * Sqr(5)) / 10
Height = Circumradius + Inradius
Diagonal = (1 + Sqr(5)) / 2
HeightDiagonal = Sqr(10 + 2 * Sqr(5)) / 4
Pi = WorksheetFunction.Pi
Ratio = Height / (2 * Height + HeightDiagonal)
'Get a base figure
Set Shp = ThisWorkbook.Worksheets(1).Shapes.AddShape(msoShapeRegularPentagon, _
2 * Side, 3 * Side / 2 + (Circumradius - Inradius) * Side, Diagonal * Side, Height * Side)
p(0) = Shp.Name
Shp.Rotation = 180
Shp.Line.Weight = 0
For j = 1 To Order_
'Place 5 copies of the figure in a circle around it
For i = 0 To 4
'Copy the figure
Set Shp = Shp.Duplicate
p(i + 1) = Shp.Name
If i = 0 Then Shp.Rotation = 0
'Place around in a circle
Shp.Left = 2 * Side + Side * Inradius * 2 * Cos(2 * Pi * (i - 1 / 4) / 5)
Shp.Top = 3 * Side / 2 + Side * Inradius * 2 * Sin(2 * Pi * (i - 1 / 4) / 5)
Shp.Visible = msoTrue
Next i
'Group the 5 figures
Set Shp = ThisWorkbook.Worksheets(1).Shapes.Range(p()).Group
p(0) = Shp.Name
If j < Order_ Then
'Shrink the figure
Shp.ScaleHeight Ratio, False
Shp.ScaleWidth Ratio, False
'Flip vertical and place in the center
Shp.Rotation = 180
Shp.Left = 2 * Side
Shp.Top = 3 * Side / 2 + (Circumradius - Inradius) * Side
End If
Next j
End Sub
 
Public Sub main()
sierpinski Order_:=5, Side:=200
End Sub</syntaxhighlight>
 
=={{header|Wren}}==
{{trans|Go}}
{{libheader|DOME}}
Black backgound and slightly different palette to Go. Also pentagons are unfilled.
<syntaxhighlight lang="wren">import "graphics" for Canvas, Color
import "dome" for Window
import "math" for Math
 
var Deg72 = 72 * Num.pi / 180 // 72 degrees in radians
var ScaleFactor = 1 / (2 + Math.cos(Deg72) * 2)
var Palette = [Color.red, Color.blue, Color.green, Color.indigo, Color.brown]
var ColorIndex = 0
var OldX = 0
var OldY = 0
 
class SierpinskiPentagon {
construct new(width, height) {
Window.title = "Sierpinksi Pentagon"
Window.resize(width, height)
Canvas.resize(width, height)
_w = width
_h = height
}
 
init() {
var order = 5 // can also set this to 1, 2, 3, or 4
var hw = _w / 2
var margin = 20
var radius = hw - 2 * margin
var side = radius * Math.sin(Num.pi/5) * 2
drawPentagon(hw, 3 * margin, side, order - 1)
}
 
drawPentagon(x, y, side, depth) {
var angle = 3 * Deg72
if (depth == 0) {
var col = Palette[ColorIndex]
OldX = x
OldY = y
for (i in 0..4) {
x = x + Math.cos(angle) * side
y = y - Math.sin(angle) * side
Canvas.line(OldX, OldY, x, y, col, 2)
OldX = x
OldY = y
angle = angle + Deg72
}
ColorIndex = (ColorIndex + 1) % 5
} else {
side = side * ScaleFactor
var dist = side * (1 + Math.cos(Deg72) * 2)
for (i in 0..4) {
x = x + Math.cos(angle) * dist
y = y - Math.sin(angle) * dist
drawPentagon(x, y, side, depth-1)
angle = angle + Deg72
}
}
}
 
update() {}
 
draw(alpha) {}
}
 
var Game = SierpinskiPentagon.new(640, 640)</syntaxhighlight>
 
{{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|Perl 6Raku}}
<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 1,089 ⟶ 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,483

edits