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