Sunflower fractal: Difference between revisions

Content added Content deleted
(with credit to homme_chauve_souris. The 2*pi typo is corrected with their solution to use 8*ATN(1))
m (syntax highlighting fixup automation)
Line 9: Line 9:
{{trans|Perl}}
{{trans|Perl}}


<lang 11l>-V
<syntaxhighlight lang="11l">-V
phi = (1 + sqrt(5)) / 2
phi = (1 + sqrt(5)) / 2
size = 600
size = 600
Line 23: Line 23:
r * cos(t) + size / 2, sqrt(i) / 13))
r * cos(t) + size / 2, sqrt(i) / 13))


print(‘</svg>’)</lang>
print(‘</svg>’)</syntaxhighlight>


=={{header|Action!}}==
=={{header|Action!}}==
Line 29: Line 29:
{{libheader|Action! Tool Kit}}
{{libheader|Action! Tool Kit}}
{{libheader|Action! Real Math}}
{{libheader|Action! Real Math}}
<lang Action!>INCLUDE "H6:REALMATH.ACT"
<syntaxhighlight lang="action!">INCLUDE "H6:REALMATH.ACT"


INT ARRAY SinTab=[
INT ARRAY SinTab=[
Line 144: Line 144:
DO UNTIL CH#$FF OD
DO UNTIL CH#$FF OD
CH=$FF
CH=$FF
RETURN</lang>
RETURN</syntaxhighlight>
{{out}}
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Sunflower_fractal.png Screenshot from Atari 8-bit computer]
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Sunflower_fractal.png Screenshot from Atari 8-bit computer]


=={{header|Applesoft BASIC}}==
=={{header|Applesoft BASIC}}==
<lang gwbasic>HGR:A=PEEK(49234):C=(SQR(5)+1)/2:N=900:FORI=0TO1600:R=(I^C)/N:A=8*ATN(1)*C*I:X=R*SIN(A)+139:Y=R*COS(A)+96:F=7-4*((X-INT(X/2)*2)>=.75):X=(X>=0ANDX<280)*X:Y=(Y>=0ANDY<192)*Y:HCOLOR=F*(XANDY):HPLOTX,Y:NEXT</lang>
<syntaxhighlight lang="gwbasic">HGR:A=PEEK(49234):C=(SQR(5)+1)/2:N=900:FORI=0TO1600:R=(I^C)/N:A=8*ATN(1)*C*I:X=R*SIN(A)+139:Y=R*COS(A)+96:F=7-4*((X-INT(X/2)*2)>=.75):X=(X>=0ANDX<280)*X:Y=(Y>=0ANDY<192)*Y:HCOLOR=F*(XANDY):HPLOTX,Y:NEXT</syntaxhighlight>


=={{header|C}}==
=={{header|C}}==
The colouring of the "fractal" is determined with every iteration to ensure that the resulting graphic looks similar to a real Sunflower, thus the parameter ''diskRatio'' determines the radius of the central disk as the maximum radius of the flower is known from the number of iterations. The scaling factor is currently hardcoded but can also be externalized. Requires the [http://www.cs.colorado.edu/~main/bgi/cs1300/ WinBGIm] library.
The colouring of the "fractal" is determined with every iteration to ensure that the resulting graphic looks similar to a real Sunflower, thus the parameter ''diskRatio'' determines the radius of the central disk as the maximum radius of the flower is known from the number of iterations. The scaling factor is currently hardcoded but can also be externalized. Requires the [http://www.cs.colorado.edu/~main/bgi/cs1300/ WinBGIm] library.


<syntaxhighlight lang="c">
<lang C>
/*Abhishek Ghosh, 14th September 2018*/
/*Abhishek Ghosh, 14th September 2018*/


Line 193: Line 193:
return 0;
return 0;
}
}
</syntaxhighlight>
</lang>


=={{header|C++}}==
=={{header|C++}}==
{{trans|Perl}}
{{trans|Perl}}
<lang cpp>#include <cmath>
<syntaxhighlight lang="cpp">#include <cmath>
#include <fstream>
#include <fstream>
#include <iostream>
#include <iostream>
Line 237: Line 237:
}
}
return EXIT_SUCCESS;
return EXIT_SUCCESS;
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 243: Line 243:


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>
<syntaxhighlight lang="freebasic">
Const PI As Double = 4 * Atn(1)
Const PI As Double = 4 * Atn(1)
Const ancho = 400
Const ancho = 400
Line 269: Line 269:
Sleep
Sleep
End
End
</syntaxhighlight>
</lang>


=={{header|Go}}==
=={{header|Go}}==
Line 276: Line 276:
<br>
<br>
The image produced, when viewed with (for example) EOG, is similar to the Ring entry.
The image produced, when viewed with (for example) EOG, is similar to the Ring entry.
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 303: Line 303:
dc.Stroke()
dc.Stroke()
dc.SavePNG("sunflower_fractal.png")
dc.SavePNG("sunflower_fractal.png")
}</lang>
}</syntaxhighlight>


=={{header|javascript}}==
=={{header|javascript}}==
Line 325: Line 325:
</html>
</html>
</pre>
</pre>
<lang javascript>const SIZE = 400, HS = SIZE >> 1, WAIT = .005, SEEDS = 3000,
<syntaxhighlight lang="javascript">const SIZE = 400, HS = SIZE >> 1, WAIT = .005, SEEDS = 3000,
TPI = Math.PI * 2, C = (Math.sqrt(10) + 1) / 2;
TPI = Math.PI * 2, C = (Math.sqrt(10) + 1) / 2;
class Sunflower {
class Sunflower {
Line 381: Line 381:
const sunflower = new Sunflower();
const sunflower = new Sunflower();
sunflower.start();
sunflower.start();
}</lang>
}</syntaxhighlight>


=={{header|J}}==
=={{header|J}}==
Line 389: Line 389:
This implementation assumes a recent J implementation (for example, J903):
This implementation assumes a recent J implementation (for example, J903):


<lang J>require'format/printf'
<syntaxhighlight lang="j">require'format/printf'


sunfract=: {{ NB. y: number of "sunflower seeds"
sunfract=: {{ NB. y: number of "sunflower seeds"
Line 406: Line 406:
</svg>
</svg>
}} sprintf (;/<.20+}:>./fract),<C) fwrite y}}
}} sprintf (;/<.20+}:>./fract),<C) fwrite y}}
</syntaxhighlight>
</lang>


Example use:
Example use:


<syntaxhighlight lang="j">
<lang J>
3000 sunfractsvg '~/sunfract.html'
3000 sunfractsvg '~/sunfract.html'
129147
129147
</syntaxhighlight>
</lang>


(The number displayed is the size of the generated file.)
(The number displayed is the size of the generated file.)
Line 422: Line 422:
'''Works with gojq, the Go implementation of jq'''
'''Works with gojq, the Go implementation of jq'''


<lang jq># SVG headers
<syntaxhighlight lang="jq"># SVG headers
def svg(size):
def svg(size):
"<svg xmlns='http://www.w3.org/2000/svg' width='\(size)'",
"<svg xmlns='http://www.w3.org/2000/svg' width='\(size)'",
Line 449: Line 449:
svg(600),
svg(600),
sunflower(600),
sunflower(600),
end_svg</lang>
end_svg</syntaxhighlight>


=={{header|Julia}}==
=={{header|Julia}}==
{{trans|R}}
{{trans|R}}
Run from REPL.
Run from REPL.
<lang julia>using Makie
<syntaxhighlight lang="julia">using Makie


function sunflowerplot()
function sunflowerplot()
Line 473: Line 473:


sunflowerplot()
sunflowerplot()
</syntaxhighlight>
</lang>


=={{header|Liberty BASIC}}==
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">
<lang lb>
nomainwin
nomainwin
UpperLeftX=1:UpperLeftY=1
UpperLeftX=1:UpperLeftY=1
Line 506: Line 506:
close #1
close #1
end
end
</syntaxhighlight>
</lang>


=={{header|Mathematica}} / {{header|Wolfram Language}}==
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<lang Mathematica>numseeds = 3000;
<syntaxhighlight lang="mathematica">numseeds = 3000;
pts = Table[
pts = Table[
i = N[ni];
i = N[ni];
Line 518: Line 518:
{ni, numseeds}
{ni, numseeds}
];
];
Graphics[pts]</lang>
Graphics[pts]</syntaxhighlight>


=={{header|Microsoft Small Basic}}==
=={{header|Microsoft Small Basic}}==
{{trans|Ring}}
{{trans|Ring}}
<lang smallbasic>' Sunflower fractal - 24/07/2018
<syntaxhighlight lang="smallbasic">' Sunflower fractal - 24/07/2018
GraphicsWindow.Width=410
GraphicsWindow.Width=410
GraphicsWindow.Height=400
GraphicsWindow.Height=400
Line 533: Line 533:
y=r*Math.Cos(angle)+200
y=r*Math.Cos(angle)+200
GraphicsWindow.DrawEllipse(x, y, i/numberofseeds*10, i/numberofseeds*10)
GraphicsWindow.DrawEllipse(x, y, i/numberofseeds*10, i/numberofseeds*10)
EndFor </lang>
EndFor </syntaxhighlight>
{{out}}
{{out}}
[https://1drv.ms/u/s!AoFH_AlpH9oZgf5kvtRou1Wuc5lSCg Sunflower fractal]
[https://1drv.ms/u/s!AoFH_AlpH9oZgf5kvtRou1Wuc5lSCg Sunflower fractal]
Line 540: Line 540:
{{trans|Go}}
{{trans|Go}}
{{libheader|imageman}}
{{libheader|imageman}}
<lang Nim>import math
<syntaxhighlight lang="nim">import math
import imageman
import imageman


Line 563: Line 563:
image.drawCircle(x, y, toInt(8 * fi / Fn), Foreground)
image.drawCircle(x, y, toInt(8 * fi / Fn), Foreground)


image.savePNG("sunflower.png", compression = 9)</lang>
image.savePNG("sunflower.png", compression = 9)</syntaxhighlight>


=={{header|Objeck}}==
=={{header|Objeck}}==
{{trans|C}}
{{trans|C}}
<lang perl>use Game.SDL2;
<syntaxhighlight lang="perl">use Game.SDL2;
use Game.Framework;
use Game.Framework;


Line 639: Line 639:
SCREEN_HEIGHT := 480
SCREEN_HEIGHT := 480
}
}
</syntaxhighlight>
</lang>


=={{header|Perl}}==
=={{header|Perl}}==
{{trans|Sidef}}
{{trans|Sidef}}
<lang perl>use utf8;
<syntaxhighlight lang="perl">use utf8;
use constant π => 3.14159265;
use constant π => 3.14159265;
use constant φ => (1 + sqrt(5)) / 2;
use constant φ => (1 + sqrt(5)) / 2;
Line 661: Line 661:
}
}


print "</svg>\n";</lang>
print "</svg>\n";</syntaxhighlight>
See [https://github.com/SqrtNegInf/Rosettacode-Perl5-Smoke/blob/master/ref/sunflower.svg Phi-packing image] (SVG image)
See [https://github.com/SqrtNegInf/Rosettacode-Perl5-Smoke/blob/master/ref/sunflower.svg Phi-packing image] (SVG image)


Line 668: Line 668:
{{libheader|Phix/online}}
{{libheader|Phix/online}}
You can run this online [http://phix.x10.mx/p2js/SunflowerFractal.htm here].
You can run this online [http://phix.x10.mx/p2js/SunflowerFractal.htm here].
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">numberofseeds</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">3000</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">numberofseeds</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">3000</span>
Line 723: Line 723:
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</lang>-->
<!--</syntaxhighlight>-->


=={{header|Processing}}==
=={{header|Processing}}==
{{trans|C}}
{{trans|C}}
<lang java>
<syntaxhighlight lang="java">
//Abhishek Ghosh, 26th June 2022
//Abhishek Ghosh, 26th June 2022


Line 753: Line 753:
ellipse(x + r*sin(theta), y + r*cos(theta), 10 * i/(1.0*iter),10 * i/(1.0*iter));
ellipse(x + r*sin(theta), y + r*cos(theta), 10 * i/(1.0*iter),10 * i/(1.0*iter));
}
}
</syntaxhighlight>
</lang>


=={{header|Python}}==
=={{header|Python}}==
<lang python>
<syntaxhighlight lang="python">
from turtle import *
from turtle import *
from math import *
from math import *
Line 807: Line 807:


done()
done()
</syntaxhighlight>
</lang>


=={{header|R}}==
=={{header|R}}==
<syntaxhighlight lang="r">
<lang R>
phi=1/2+sqrt(5)/2
phi=1/2+sqrt(5)/2
r=seq(0,1,length.out=2000)
r=seq(0,1,length.out=2000)
Line 827: Line 827:
points(x[i],y[i],cex=size[i],lwd=thick[i],col="goldenrod1")
points(x[i],y[i],cex=size[i],lwd=thick[i],col="goldenrod1")
}
}
</syntaxhighlight>
</lang>
{{Out}}
{{Out}}
[https://raw.githubusercontent.com/schwartstack/sunflower/master/sunflower2.png Sunflower]
[https://raw.githubusercontent.com/schwartstack/sunflower/master/sunflower2.png Sunflower]
Line 835: Line 835:
{{trans|C}}
{{trans|C}}


<lang racket>#lang racket
<syntaxhighlight lang="racket">#lang racket


(require 2htdp/image)
(require 2htdp/image)
Line 853: Line 853:
(+ (/ WIDTH 2) (* r (sin theta)))
(+ (/ WIDTH 2) (* r (sin theta)))
(+ (/ HEIGHT 2) (* r (cos theta)))
(+ (/ HEIGHT 2) (* r (cos theta)))
image))</lang>
image))</syntaxhighlight>


=={{header|Raku}}==
=={{header|Raku}}==
Line 862: Line 862:
Or, to be completely accurate: It is a variation of a generative [[wp:Fermat's_spiral|Fermat's spiral]] using the Vogel model to implement phi-packing. See: [https://thatsmaths.com/2014/06/05/sunflowers-and-fibonacci-models-of-efficiency/ https://thatsmaths.com/2014/06/05/sunflowers-and-fibonacci-models-of-efficiency]
Or, to be completely accurate: It is a variation of a generative [[wp:Fermat's_spiral|Fermat's spiral]] using the Vogel model to implement phi-packing. See: [https://thatsmaths.com/2014/06/05/sunflowers-and-fibonacci-models-of-efficiency/ https://thatsmaths.com/2014/06/05/sunflowers-and-fibonacci-models-of-efficiency]


<lang perl6>use SVG;
<syntaxhighlight lang="raku" line>use SVG;


my $seeds = 3000;
my $seeds = 3000;
Line 881: Line 881:
|@c.map( { :circle[:cx(.[0]), :cy(.[1]), :r(.[2])] } ),
|@c.map( { :circle[:cx(.[0]), :cy(.[1]), :r(.[2])] } ),
],
],
);</lang>
);</syntaxhighlight>
See: [https://github.com/thundergnat/rc/blob/master/img/phi-packing-perl6.svg Phi packing] (SVG image)
See: [https://github.com/thundergnat/rc/blob/master/img/phi-packing-perl6.svg Phi packing] (SVG image)


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
# Project : Sunflower fractal
# Project : Sunflower fractal


Line 937: Line 937:
}
}
label1 { setpicture(p1) show() }
label1 { setpicture(p1) show() }
</syntaxhighlight>
</lang>
Output:
Output:


Line 944: Line 944:
=={{header|Sidef}}==
=={{header|Sidef}}==
{{trans|Go}}
{{trans|Go}}
<lang ruby>require('Imager')
<syntaxhighlight lang="ruby">require('Imager')


func draw_sunflower(seeds=3000) {
func draw_sunflower(seeds=3000) {
Line 965: Line 965:


var img = draw_sunflower()
var img = draw_sunflower()
img.write(file => "sunflower.png")</lang>
img.write(file => "sunflower.png")</syntaxhighlight>
Output image: [https://github.com/trizen/rc/blob/master/img/sunflower-sidef.png Sunflower fractal]
Output image: [https://github.com/trizen/rc/blob/master/img/sunflower-sidef.png Sunflower fractal]


=={{header|Vlang}}==
=={{header|Vlang}}==
<lang vlang>import gg
<syntaxhighlight lang="vlang">import gg
import gx
import gx
import math
import math
Line 998: Line 998:
}
}
ctx.end()
ctx.end()
}</lang>
}</syntaxhighlight>


=={{header|Wren}}==
=={{header|Wren}}==
{{trans|Go}}
{{trans|Go}}
{{libheader|DOME}}
{{libheader|DOME}}
<lang ecmascript>import "graphics" for Canvas, Color
<syntaxhighlight lang="ecmascript">import "graphics" for Canvas, Color
import "dome" for Window
import "dome" for Window


Line 1,033: Line 1,033:
}
}
}
}
}</lang>
}</syntaxhighlight>


=={{header|Yabasic}}==
=={{header|Yabasic}}==
{{trans|Wren}}
{{trans|Wren}}
<lang Yabasic>// Rosetta Code problem: http://rosettacode.org/wiki/Sunflower_fractal
<syntaxhighlight lang="yabasic">// Rosetta Code problem: http://rosettacode.org/wiki/Sunflower_fractal
// Adapted from Wren to Yabasic by Galileo, 01/2022
// Adapted from Wren to Yabasic by Galileo, 01/2022


Line 1,057: Line 1,057:
y = r * cos(angle) + 200
y = r * cos(angle) + 200
circle x, y, i / seeds * 5
circle x, y, i / seeds * 5
next</lang>
next</syntaxhighlight>


=={{header|zkl}}==
=={{header|zkl}}==
Line 1,063: Line 1,063:
Uses Image Magick and
Uses Image Magick and
the PPM class from http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#zkl
the PPM class from http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#zkl
<lang zkl>fcn sunflower(seeds=3000){
<syntaxhighlight lang="zkl">fcn sunflower(seeds=3000){
img,color := PPM(400,400), 0x00ff00; // green
img,color := PPM(400,400), 0x00ff00; // green
c:=((5.0).sqrt() + 1)/2;
c:=((5.0).sqrt() + 1)/2;
Line 1,073: Line 1,073:
}
}
img.writeJPGFile("sunflower.zkl.jpg");
img.writeJPGFile("sunflower.zkl.jpg");
}();</lang>
}();</syntaxhighlight>
{{out}}
{{out}}
Image at [http://www.zenkinetic.com/Images/RosettaCode/sunflower.zkl.jpg sunflower fractal]
Image at [http://www.zenkinetic.com/Images/RosettaCode/sunflower.zkl.jpg sunflower fractal]