Sunflower fractal: Difference between revisions
(→{{header|Go}}: Removed superfluous call to dc.Fill() method.) |
Thundergnat (talk | contribs) (→{{header|Perl 6}}: Add a Perl 6 example) |
||
Line 53: | Line 53: | ||
{{out}} |
{{out}} |
||
[https://1drv.ms/u/s!AoFH_AlpH9oZgf5kvtRou1Wuc5lSCg Sunflower fractal] |
[https://1drv.ms/u/s!AoFH_AlpH9oZgf5kvtRou1Wuc5lSCg Sunflower fractal] |
||
=={{header|Perl 6}}== |
|||
{{works with|Rakudo|2018.06}} |
|||
This is not really a fractal. It is more accurately an example of a Fibonacci spiral or Phi-packing. |
|||
<lang perl6>use SVG; |
|||
my $seeds = 3000; |
|||
my @center = 330, 330; |
|||
my $scale = 5; |
|||
constant \φ = (1 + 5.sqrt) / 2; |
|||
my @c = map { |
|||
my ($x, $y) = ($scale * .sqrt) «*« |cis($_ * φ * π).reals »+« @center; |
|||
[ $x.round(.01), $y.round(.01), .sqrt * $scale / 100 ] |
|||
}, 1 .. $seeds; |
|||
say SVG.serialize( |
|||
svg => [ |
|||
:660width, :660height, :style<stroke:gold>, |
|||
:rect[:width<100%>, :height<100%>, :fill<black>], |
|||
|@c.map( { :circle[ :cx($_[0]), :cy($_[1]), :r($_[2]) ] } ), |
|||
], |
|||
);</lang> |
|||
See: [https://github.com/thundergnat/rc/blob/master/img/phi-packing-perl6.svg Phi packing] (SVG image) |
|||
=={{header|Ring}}== |
=={{header|Ring}}== |
Revision as of 22:48, 25 July 2018
Draw Sunflower fractal
Go
This uses the Go Graphics library for rendering 2D graphics in pure Go. The image produced, when viewed with (for example) EOG, is similar to the Ring entry.
<lang go>package main
import (
"github.com/fogleman/gg" "math"
)
func main() {
dc := gg.NewContext(400, 400) dc.SetRGB(1, 1, 1) dc.Clear() dc.SetRGB(0, 0, 1) c := (math.Sqrt(5) + 1) / 2 numberOfSeeds := 3000 for i := 0; i <= numberOfSeeds; i++ { fi := float64(i) fn := float64(numberOfSeeds) r := math.Pow(fi, c) / fn angle := 2 * math.Pi * c * fi x := r*math.Sin(angle) + 200 y := r*math.Cos(angle) + 200 fi /= fn / 5 dc.DrawCircle(x, y, fi) } dc.SetLineWidth(1) dc.Stroke() dc.SavePNG("sunflower_fractal.png")
}</lang>
Microsoft Small Basic
<lang smallbasic>' Sunflower fractal - 24/07/2018
GraphicsWindow.Width=410 GraphicsWindow.Height=400 c=(Math.SquareRoot(5)+1)/2 numberofseeds=3000 For i=0 To numberofseeds r=Math.Power(i,c)/numberofseeds angle=2*Math.Pi*c*i x=r*Math.Sin(angle)+200 y=r*Math.Cos(angle)+200 GraphicsWindow.DrawEllipse(x, y, i/numberofseeds*10, i/numberofseeds*10) EndFor </lang>
- Output:
Perl 6
This is not really a fractal. It is more accurately an example of a Fibonacci spiral or Phi-packing.
<lang perl6>use SVG;
my $seeds = 3000; my @center = 330, 330; my $scale = 5;
constant \φ = (1 + 5.sqrt) / 2;
my @c = map {
my ($x, $y) = ($scale * .sqrt) «*« |cis($_ * φ * π).reals »+« @center; [ $x.round(.01), $y.round(.01), .sqrt * $scale / 100 ]
}, 1 .. $seeds;
say SVG.serialize(
svg => [ :660width, :660height, :style<stroke:gold>, :rect[:width<100%>, :height<100%>, :fill<black>], |@c.map( { :circle[ :cx($_[0]), :cy($_[1]), :r($_[2]) ] } ), ],
);</lang> See: Phi packing (SVG image)
Ring
<lang ring>
- Project : Sunflower fractal
- Date : 2018/07/24
- Author : Gal Zsolt (~ CalmoSoft ~)
- Email : calmosoft@gmail.com
load "guilib.ring"
paint = null
new qapp
{ win1 = new qwidget() { setwindowtitle("Sunflower fractal") setgeometry(100,100,320,500) label1 = new qlabel(win1) { setgeometry(10,10,400,400) settext("") } new qpushbutton(win1) { setgeometry(100,400,100,30) settext("draw") setclickevent("draw()") } show() } exec() }
func draw
p1 = new qpicture() color = new qcolor() { setrgb(0,0,255,255) } pen = new qpen() { setcolor(color) setwidth(1) } paint = new qpainter() { begin(p1) setpen(pen)
c = (sqrt(5) + 1) / 2 numberofseeds = 3000 for i = 0 to numberofseeds r = pow(i, c ) / (numberofseeds) angle = 2 * 3.14 * c * i x = r * sin(angle) + 100 y = r * cos(angle) + 100 drawellipse(x, y, i / (numberofseeds / 10), i / (numberofseeds / 10)) next
endpaint() } label1 { setpicture(p1) show() }
</lang> Output:
Sidef
<lang ruby>require('Imager')
func draw_sunflower(seeds=3000) {
var img = %O<Imager>.new( xsize => 400, ysize => 400, )
var c = (sqrt(1.25) + 0.5) { |i| var r = (i**c / seeds) var θ = (2 * Num.pi * c * i) var x = (r * sin(θ) + 200) var y = (r * cos(θ) + 200) img.circle(x => x, y => y, r => i/(5*seeds)) } * seeds
return img
}
var img = draw_sunflower() img.write(file => "sunflower.png")</lang>
- Output:
zkl
Uses Image Magick and the PPM class from http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#zkl <lang zkl>fcn sunflower(seeds=3000){
img,color := PPM(400,400), 0x00ff00; // green c:=((5.0).sqrt() + 1)/2; foreach n in ([0.0 .. seeds]){ // floats r:=n.pow(c)/seeds; x,y := r.toRectangular(r.pi*c*n*2); r=(n/seeds*5).toInt(); img.circle(200 + x, 200 + y, r,color); } img.writeJPGFile("sunflower.zkl.jpg");
}();</lang>
- Output:
Image at sunflower fractal