Sierpinski pentagon: Difference between revisions
Content added Content deleted
m (→{{header|Phix}}: added use +/- comment) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 9: | Line 9: | ||
=={{header|Action!}}== |
=={{header|Action!}}== |
||
< |
<syntaxhighlight lang="action!">PROC Main() |
||
INT ARRAY xs=[249 200 96 80 175] |
INT ARRAY xs=[249 200 96 80 175] |
||
BYTE ARRAY ys=[82 176 159 55 7] |
BYTE ARRAY ys=[82 176 159 55 7] |
||
Line 30: | Line 30: | ||
OD |
OD |
||
CH=$FF |
CH=$FF |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Sierpinski_pentagon.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Sierpinski_pentagon.png Screenshot from Atari 8-bit computer] |
||
Line 37: | Line 37: | ||
{{trans|Go}} |
{{trans|Go}} |
||
Requires [https://www.autohotkey.com/boards/viewtopic.php?t=6517 Gdip Library] |
Requires [https://www.autohotkey.com/boards/viewtopic.php?t=6517 Gdip Library] |
||
< |
<syntaxhighlight lang="autohotkey">W := H := 640 |
||
hw := W / 2 |
hw := W / 2 |
||
margin := 20 |
margin := 20 |
||
Line 128: | Line 128: | ||
Gdip_Shutdown(pToken) |
Gdip_Shutdown(pToken) |
||
ExitApp |
ExitApp |
||
Return</ |
Return</syntaxhighlight> |
||
=={{header|C}}== |
=={{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. |
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<graphics.h> |
||
#include<stdlib.h> |
#include<stdlib.h> |
||
Line 194: | Line 194: | ||
return 0; |
return 0; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|C++}}== |
=={{header|C++}}== |
||
{{trans|D}} |
{{trans|D}} |
||
< |
<syntaxhighlight lang="cpp">#include <iomanip> |
||
#include <iostream> |
#include <iostream> |
||
Line 352: | Line 352: | ||
std::cout << "</svg>"; |
std::cout << "</svg>"; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|D}}== |
=={{header|D}}== |
||
Line 360: | Line 360: | ||
This runs very quickly compared to the Python version. |
This runs very quickly compared to the Python version. |
||
< |
<syntaxhighlight lang="d">import std.math; |
||
import std.stdio; |
import std.stdio; |
||
Line 514: | Line 514: | ||
tracing = false; |
tracing = false; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
Line 522: | Line 522: | ||
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. |
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. |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 582: | Line 582: | ||
drawPentagon(hw, 3*margin, side, order-1) |
drawPentagon(hw, 3*margin, side, order-1) |
||
dc.SavePNG("sierpinski_pentagon.png") |
dc.SavePNG("sierpinski_pentagon.png") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 592: | Line 592: | ||
For universal solution see [[Fractal tree#Haskell]] |
For universal solution see [[Fractal tree#Haskell]] |
||
< |
<syntaxhighlight lang="haskell">import Graphics.Gloss |
||
pentaflake :: Int -> Picture |
pentaflake :: Int -> Picture |
||
Line 604: | Line 604: | ||
main = display dc white (Color blue $ Scale 300 300 $ pentaflake 5) |
main = display dc white (Color blue $ Scale 300 300 $ pentaflake 5) |
||
where dc = InWindow "Pentaflake" (400, 400) (0, 0)</ |
where dc = InWindow "Pentaflake" (400, 400) (0, 0)</syntaxhighlight> |
||
'''Explanation''': Since <tt>Picture</tt> forms a monoid with image overlaying as multiplication, so do functions having type <tt>Picture -> Picture</tt>: |
'''Explanation''': Since <tt>Picture</tt> forms a monoid with image overlaying as multiplication, so do functions having type <tt>Picture -> Picture</tt>: |
||
Line 614: | Line 614: | ||
If one wants to get all intermediate pentaflakes <code>transformation</code> shoud be changed as follows: |
If one wants to get all intermediate pentaflakes <code>transformation</code> shoud be changed as follows: |
||
< |
<syntaxhighlight lang="haskell">transformation = Scale s s . (Rotate 36 <> foldMap copy [0,72..288])</syntaxhighlight> |
||
See also the implementation using [http://projects.haskell.org/diagrams/gallery/Pentaflake.html Diagrams] |
See also the implementation using [http://projects.haskell.org/diagrams/gallery/Pentaflake.html Diagrams] |
||
Line 621: | Line 621: | ||
[[File:sierpinski_pentagon.png|300px|thumb|right]] |
[[File:sierpinski_pentagon.png|300px|thumb|right]] |
||
{{works with|Java|8}} |
{{works with|Java|8}} |
||
< |
<syntaxhighlight lang="java">import java.awt.*; |
||
import java.awt.event.ActionEvent; |
import java.awt.event.ActionEvent; |
||
import java.awt.geom.Path2D; |
import java.awt.geom.Path2D; |
||
Line 729: | Line 729: | ||
return Color.getHSBColor((float) hue, 1, 1); |
return Color.getHSBColor((float) hue, 1, 1); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
Line 739: | Line 739: | ||
< |
<syntaxhighlight lang="html"> |
||
<html> |
<html> |
||
<head> |
<head> |
||
Line 828: | Line 828: | ||
</body> |
</body> |
||
</html> |
</html> |
||
</ |
</syntaxhighlight> |
||
{{Output}} |
{{Output}} |
||
Line 838: | Line 838: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
{{trans|Perl}} |
{{trans|Perl}} |
||
< |
<syntaxhighlight lang="julia">using Printf |
||
const sides = 5 |
const sides = 5 |
||
Line 863: | Line 863: | ||
print(fh, "</svg>") |
print(fh, "</svg>") |
||
close(fh)</ |
close(fh)</syntaxhighlight> |
||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="scala">// version 1.1.2 |
||
import java.awt.* |
import java.awt.* |
||
Line 970: | Line 970: | ||
f.isVisible = true |
f.isVisible = true |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
An ASCII-interpretation of the task. Uses the Bitmap class and text renderer from [[Bitmap/Bresenham's_line_algorithm#Lua|here]]. |
An ASCII-interpretation of the task. Uses the Bitmap class and text renderer from [[Bitmap/Bresenham's_line_algorithm#Lua|here]]. |
||
< |
<syntaxhighlight lang="lua">Bitmap.chaosgame = function(self, n, r, niters) |
||
local w, h, vertices = self.width, self.height, {} |
local w, h, vertices = self.width, self.height, {} |
||
for i = 1, n do |
for i = 1, n do |
||
Line 993: | Line 993: | ||
local bitmap = Bitmap(128, 128) |
local bitmap = Bitmap(128, 128) |
||
bitmap:chaosgame(5, 1/((1+math.sqrt(5))/2), 1e6) |
bitmap:chaosgame(5, 1/((1+math.sqrt(5))/2), 1e6) |
||
bitmap:render({[0x000000]='..', [0xFFFFFFFF]='██'})</ |
bitmap:render({[0x000000]='..', [0xFFFFFFFF]='██'})</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Shown at 25% scale: |
Shown at 25% scale: |
||
Line 1,126: | Line 1,126: | ||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">pentaFlake[0] = RegularPolygon[5]; |
||
pentaFlake[n_] := GeometricTransformation[pentaFlake[n - 1], TranslationTransform /@ CirclePoints[{GoldenRatio^(2 n - 1), Pi/10}, 5]] |
pentaFlake[n_] := GeometricTransformation[pentaFlake[n - 1], TranslationTransform /@ CirclePoints[{GoldenRatio^(2 n - 1), Pi/10}, 5]] |
||
Graphics@pentaFlake[4]</ |
Graphics@pentaFlake[4]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
https://i.imgur.com/rvXvQc0.png |
https://i.imgur.com/rvXvQc0.png |
||
=={{header|MATLAB}}== |
=={{header|MATLAB}}== |
||
< |
<syntaxhighlight lang="matlab">[x, x0] = deal(exp(1i*(0.5:.4:2.1)*pi)); |
||
for k = 1 : 4 |
for k = 1 : 4 |
||
x = x(:) + x0 * (1 + sqrt(5)) * (3 + sqrt(5)) ^(k - 1) / 2 ^ k; |
x = x(:) + x0 * (1 + sqrt(5)) * (3 + sqrt(5)) ^(k - 1) / 2 ^ k; |
||
end |
end |
||
patch('Faces', reshape(1 : 5 * 5 ^ k, 5, '')', 'Vertices', [real(x(:)) imag(x(:))]) |
patch('Faces', reshape(1 : 5 * 5 ^ k, 5, '')', 'Vertices', [real(x(:)) imag(x(:))]) |
||
axis image off</ |
axis image off</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
http://i.imgur.com/8ht6HqG.png |
http://i.imgur.com/8ht6HqG.png |
||
Line 1,145: | Line 1,145: | ||
{{trans|Go}} |
{{trans|Go}} |
||
{{libheader|imageman}} |
{{libheader|imageman}} |
||
< |
<syntaxhighlight lang="nim">import math |
||
import imageman |
import imageman |
||
Line 1,191: | Line 1,191: | ||
let side = radius * sin(PI / 5) * 2 |
let side = radius * sin(PI / 5) * 2 |
||
image.drawPentagon(hw, 3 * margin, side, order - 1) |
image.drawPentagon(hw, 3 * margin, side, order - 1) |
||
image.savePNG("Sierpinski_pentagon.png", compression = 9)</ |
image.savePNG("Sierpinski_pentagon.png", compression = 9)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,199: | Line 1,199: | ||
{{libheader|ntheory}} |
{{libheader|ntheory}} |
||
{{trans|Raku}} |
{{trans|Raku}} |
||
< |
<syntaxhighlight lang="perl">use ntheory qw(todigits); |
||
use Math::Complex; |
use Math::Complex; |
||
Line 1,233: | Line 1,233: | ||
print $fh '</svg>'; |
print $fh '</svg>'; |
||
close $fh;</ |
close $fh;</syntaxhighlight> |
||
[https://github.com/SqrtNegInf/Rosettacode-Perl5-Smoke/blob/master/ref/sierpinski_pentagon.svg Sierpinski pentagon] (offsite image) |
[https://github.com/SqrtNegInf/Rosettacode-Perl5-Smoke/blob/master/ref/sierpinski_pentagon.svg Sierpinski pentagon] (offsite image) |
||
Line 1,240: | Line 1,240: | ||
{{libheader|Phix/online}} |
{{libheader|Phix/online}} |
||
You can run this online [http://phix.x10.mx/p2js/SierpinskyPentagon.htm here]. Use +/- to change the level, 0..5. |
You can run this online [http://phix.x10.mx/p2js/SierpinskyPentagon.htm here]. Use +/- to change the level, 0..5. |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #000080;font-style:italic;">-- |
<span style="color: #000080;font-style:italic;">-- |
||
-- demo\rosetta\SierpinskyPentagon.exw |
-- demo\rosetta\SierpinskyPentagon.exw |
||
Line 1,336: | Line 1,336: | ||
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span> |
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
=={{header|Processing}}== |
=={{header|Processing}}== |
||
< |
<syntaxhighlight lang="java"> |
||
float s_angle, scale, margin = 25, total = 4; |
float s_angle, scale, margin = 25, total = 4; |
||
float p_size = 700; |
float p_size = 700; |
||
Line 1,379: | Line 1,379: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight>'''The sketch can be run online''' :<BR> [https://www.openprocessing.org/sketch/955331 here.] |
||
=={{header|Prolog}}== |
=={{header|Prolog}}== |
||
{{works with|SWI Prolog}} |
{{works with|SWI Prolog}} |
||
This code is based on the Java solution. The output is an SVG file. |
This code is based on the Java solution. The output is an SVG file. |
||
< |
<syntaxhighlight lang="prolog">main:- |
||
write_sierpinski_pentagon('sierpinski_pentagon.svg', 600, 5). |
write_sierpinski_pentagon('sierpinski_pentagon.svg', 600, 5). |
||
Line 1,433: | Line 1,433: | ||
Angle1 is Angle + 2 * pi/5, |
Angle1 is Angle + 2 * pi/5, |
||
sierpinski_pentagon(Stream, X1, Y1, Scale_factor, Side, N), |
sierpinski_pentagon(Stream, X1, Y1, Scale_factor, Side, N), |
||
sierpinski_pentagons(Stream, X1, Y1, Scale_factor, Side, Angle1, N, I1).</ |
sierpinski_pentagons(Stream, X1, Y1, Scale_factor, Side, Angle1, N, I1).</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,441: | Line 1,441: | ||
Draws the result on a canvas. Runs pretty slowly. |
Draws the result on a canvas. Runs pretty slowly. |
||
< |
<syntaxhighlight lang="python">from turtle import * |
||
import math |
import math |
||
speed(0) # 0 is the fastest speed. Otherwise, 1 (slow) to 10 (fast) |
speed(0) # 0 is the fastest speed. Otherwise, 1 (slow) to 10 (fast) |
||
Line 1,515: | Line 1,515: | ||
sierpinski(i, t, size) |
sierpinski(i, t, size) |
||
main()</ |
main()</syntaxhighlight> |
||
See [https://trinket.io/python/5137ae2b92 online implementation]. See [http://i.imgur.com/96D0c7i.png completed output]. |
See [https://trinket.io/python/5137ae2b92 online implementation]. See [http://i.imgur.com/96D0c7i.png completed output]. |
||
=={{header|Quackery}}== |
=={{header|Quackery}}== |
||
< |
<syntaxhighlight lang="quackery">[ $ "turtleduck.qky" loadfile ] now! |
||
[ [ 1 1 |
[ [ 1 1 |
||
Line 1,553: | Line 1,553: | ||
2 5 turn |
2 5 turn |
||
' [ 79 126 229 ] colour |
' [ 79 126 229 ] colour |
||
400 1 5 pentaflake</ |
400 1 5 pentaflake</syntaxhighlight> |
||
{{output}} |
{{output}} |
||
Line 1,562: | Line 1,562: | ||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="racket">#lang racket/base |
||
(require racket/draw pict racket/math racket/class) |
(require racket/draw pict racket/math racket/class) |
||
Line 1,618: | Line 1,618: | ||
(dc-draw-pentagon 3 120 120) |
(dc-draw-pentagon 3 120 120) |
||
(dc-draw-pentagon 4 120 120) |
(dc-draw-pentagon 4 120 120) |
||
(dc-draw-pentagon 5 640 640)</ |
(dc-draw-pentagon 5 640 640)</syntaxhighlight> |
||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
(formerly Perl 6) |
(formerly Perl 6) |
||
{{works with|rakudo|2018-10}} |
{{works with|rakudo|2018-10}} |
||
<lang |
<syntaxhighlight lang="raku" line>constant $sides = 5; |
||
constant order = 5; |
constant order = 5; |
||
constant $dim = 250; |
constant $dim = 250; |
||
Line 1,643: | Line 1,643: | ||
$fh.say: '</svg>'; |
$fh.say: '</svg>'; |
||
$fh.close;</ |
$fh.close;</syntaxhighlight> |
||
See [http://rosettacode.org/mw/images/5/57/Perl6_pentaflake.svg 5th order pentaflake] |
See [http://rosettacode.org/mw/images/5/57/Perl6_pentaflake.svg 5th order pentaflake] |
||
Line 1,651: | Line 1,651: | ||
{{libheader|JRubyArt}} |
{{libheader|JRubyArt}} |
||
JRubyArt is a port of processing to ruby |
JRubyArt is a port of processing to ruby |
||
< |
<syntaxhighlight lang="ruby"> |
||
THETA = Math::PI * 2 / 5 |
THETA = Math::PI * 2 / 5 |
||
SCALE_FACTOR = (3 - Math.sqrt(5)) / 2 |
SCALE_FACTOR = (3 - Math.sqrt(5)) / 2 |
||
Line 1,731: | Line 1,731: | ||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
This code is based on the Java solution. The output is a file in SVG format. |
This code is based on the Java solution. The output is a file in SVG format. |
||
< |
<syntaxhighlight lang="rust">// [dependencies] |
||
// svg = "0.8.0" |
// svg = "0.8.0" |
||
Line 1,807: | Line 1,807: | ||
fn main() { |
fn main() { |
||
write_sierpinski_pentagon("sierpinski_pentagon.svg", 600, 5).unwrap(); |
write_sierpinski_pentagon("sierpinski_pentagon.svg", 600, 5).unwrap(); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,814: | Line 1,814: | ||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
===Java Swing Interoperability=== |
===Java Swing Interoperability=== |
||
< |
<syntaxhighlight lang="scala">import java.awt._ |
||
import java.awt.event.ActionEvent |
import java.awt.event.ActionEvent |
||
import java.awt.geom.Path2D |
import java.awt.geom.Path2D |
||
Line 1,920: | Line 1,920: | ||
}) |
}) |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
{{trans|Raku}} |
{{trans|Raku}} |
||
Generates a SVG image to STDOUT. Redirect to a file to capture and display it. |
Generates a SVG image to STDOUT. Redirect to a file to capture and display it. |
||
< |
<syntaxhighlight lang="ruby">define order = 5 |
||
define sides = 5 |
define sides = 5 |
||
define dim = 500 |
define dim = 500 |
||
Line 1,948: | Line 1,948: | ||
} |
} |
||
say '</svg>'</ |
say '</svg>'</syntaxhighlight> |
||
=={{header|VBA}}== |
=={{header|VBA}}== |
||
Using Excel |
Using Excel |
||
< |
<syntaxhighlight lang="vb">Private Sub sierpinski(Order_ As Integer, Side As Double) |
||
Dim Circumradius As Double, Inradius As Double |
Dim Circumradius As Double, Inradius As Double |
||
Dim Height As Double, Diagonal As Double, HeightDiagonal As Double |
Dim Height As Double, Diagonal As Double, HeightDiagonal As Double |
||
Line 1,998: | Line 1,998: | ||
Public Sub main() |
Public Sub main() |
||
sierpinski Order_:=5, Side:=200 |
sierpinski Order_:=5, Side:=200 |
||
End Sub</ |
End Sub</syntaxhighlight> |
||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
Line 2,004: | Line 2,004: | ||
{{libheader|DOME}} |
{{libheader|DOME}} |
||
Black backgound and slightly different palette to Go. Also pentagons are unfilled. |
Black backgound and slightly different palette to Go. Also pentagons are unfilled. |
||
< |
<syntaxhighlight lang="ecmascript">import "graphics" for Canvas, Color |
||
import "dome" for Window |
import "dome" for Window |
||
import "math" for Math |
import "math" for Math |
||
Line 2,065: | Line 2,065: | ||
} |
} |
||
var Game = SierpinskiPentagon.new(640, 640)</ |
var Game = SierpinskiPentagon.new(640, 640)</syntaxhighlight> |
||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
{{trans|Raku}} |
{{trans|Raku}} |
||
< |
<syntaxhighlight 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 |
const tau=(0.0).pi*2; // 2*pi*r |
||
orders:=order.pump(List,fcn(n){ (1.0 - scaleFactor)*dim*scaleFactor.pow(n) }); |
orders:=order.pump(List,fcn(n){ (1.0 - scaleFactor)*dim*scaleFactor.pow(n) }); |
||
Line 2,098: | Line 2,098: | ||
0'|<polygon points="%s"/>|.fmt( |
0'|<polygon points="%s"/>|.fmt( |
||
vertices.pump(String,fcn(v){ "%.3f %.3f ".fmt(v.xplode()) }) ) |
vertices.pump(String,fcn(v){ "%.3f %.3f ".fmt(v.xplode()) }) ) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
See [http://www.zenkinetic.com/Images/RosettaCode/sierpinskiPentagon.zkl.svg this image]. |
See [http://www.zenkinetic.com/Images/RosettaCode/sierpinskiPentagon.zkl.svg this image]. |