Sunflower fractal: Difference between revisions

m
(Clarify task)
 
(26 intermediate revisions by 16 users not shown)
Line 9:
{{trans|Perl}}
 
<langsyntaxhighlight lang="11l">-V
phi = (1 + sqrt(5)) / 2
size = 600
Line 23:
r * cos(t) + size / 2, sqrt(i) / 13))
 
print(‘</svg>’)</langsyntaxhighlight>
 
=={{header|Action!}}==
Calculations on a real Atari 8-bit computer take quite long time. It is recommended to use an emulator capable with increasing speed of Atari CPU.
{{libheader|Action! Tool Kit}}
{{libheader|Action! Real Math}}
<syntaxhighlight lang="action!">INCLUDE "H6:REALMATH.ACT"
 
INT ARRAY SinTab=[
0 4 9 13 18 22 27 31 36 40 44 49 53 58 62 66 71 75 79 83
88 92 96 100 104 108 112 116 120 124 128 132 136 139 143
147 150 154 158 161 165 168 171 175 178 181 184 187 190
193 196 199 202 204 207 210 212 215 217 219 222 224 226
228 230 232 234 236 237 239 241 242 243 245 246 247 248
249 250 251 252 253 254 254 255 255 255 256 256 256 256]
 
INT FUNC Sin(INT a)
WHILE a<0 DO a==+360 OD
WHILE a>360 DO a==-360 OD
IF a<=90 THEN
RETURN (SinTab(a))
ELSEIF a<=180 THEN
RETURN (SinTab(180-a))
ELSEIF a<=270 THEN
RETURN (-SinTab(a-180))
ELSE
RETURN (-SinTab(360-a))
FI
RETURN (0)
 
INT FUNC Cos(INT a)
RETURN (Sin(a-90))
 
PROC Circle(INT x0,y0,d)
BYTE MaxD=[13]
BYTE ARRAY Start=[0 1 2 4 6 9 12 16 20 25 30 36 42 49]
BYTE ARRAY MaxY=[0 0 1 1 2 2 3 3 4 4 5 5 6 6]
INT ARRAY CircleX=[
0 0 1 0 1 1 2 1 0 2 2 1 3 2 2 0 3 3 2 1
4 4 3 2 1 4 4 4 3 2 5 5 4 4 3 1 5 5 5 4 4 2
6 6 5 5 4 3 1 6 6 6 5 5 4 2]
 
INT i,ind,max
CARD x
BYTE dx,y
 
IF d>MAXD THEN d=MaxD FI
IF d<0 THEN d=0 FI
 
ind=Start(d)
max=MaxY(d)
FOR i=0 TO max
DO
dx=CircleX(ind)
y=y0-i
IF (y>=0) AND (y<=191) THEN
Plot(x0-dx,y) DrawTo(x0+dx,y)
FI
y=y0+i
IF (y>=0) AND (y<=191) THEN
Plot(x0-dx,y) DrawTo(x0+dx,y)
FI
ind==+1
OD
RETURN
 
PROC DrawFractal(CARD seeds INT x0,y0)
CARD i
REAL a,c,r,ir,tmp,tmp2,r256,rx,ry,rr,r360,c360,seeds2
INT ia,sc,x,y
 
IntToReal(256,r256)
ValR("1.618034",c) ;c=(sqrt(5)+1)/2
IntToReal(seeds/2,seeds2) ;seeds2=seeds/2
IntToReal(360,r360)
RealMult(r360,c,c360) ;c360=360*c
 
FOR i=0 TO seeds
DO
IntToReal(i,ir)
Power(ir,c,tmp)
RealDiv(tmp,seeds2,r) ;r=i^c/(seeds/2)
RealMult(c360,ir,a) ;a=360*c*i
 
WHILE RealGreaterOrEqual(a,r360)
DO
RealSub(a,r360,tmp)
RealAssign(tmp,a)
OD
 
ia=RealToInt(a)
sc=Sin(ia)
IntToRealForNeg(sc,tmp)
RealDiv(tmp,r256,tmp2)
RealMult(r,tmp2,rx)
x=Round(rx) ;x=r*sin(a)
sc=Cos(ia)
IntToRealForNeg(sc,tmp)
RealDiv(tmp,r256,tmp2)
RealMult(r,tmp2,ry)
y=Round(ry) ;y=r*cos(a)
 
Circle(x+x0,y+y0,10*i/seeds)
 
Poke(77,0) ;turn off the attract mode
OD
RETURN
 
PROC Main()
BYTE CH=$02FC,COLOR1=$02C5,COLOR2=$02C6
 
Graphics(8+16)
Color=1
COLOR1=$12
COLOR2=$18
 
DrawFractal(1000,160,96)
 
DO UNTIL CH#$FF OD
CH=$FF
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Sunflower_fractal.png Screenshot from Atari 8-bit computer]
 
=={{header|Applesoft BASIC}}==
<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}}==
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*/
 
Line 67 ⟶ 193:
return 0;
}
</syntaxhighlight>
</lang>
 
=={{header|C++}}==
{{trans|Perl}}
<langsyntaxhighlight lang="cpp">#include <cmath>
#include <fstream>
#include <iostream>
Line 111 ⟶ 237:
}
return EXIT_SUCCESS;
}</langsyntaxhighlight>
 
{{out}}
[[Media:Sunflower cpp.svg]]
See: [https://slack-files.com/T0CNUL56D-F016R4G8MQB-27761bfe01 sunflower.svg] (offsite SVG image)
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">
Const PI As Double = 4 * Atn(1)
Const ancho = 400
Line 143 ⟶ 269:
Sleep
End
</syntaxhighlight>
</lang>
 
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Sunflower_model}}
 
'''Solution'''
 
The method consists in drawing points on a spriral, an archimedean spiral, where two contiguous points are separated (in angle) by the golden angle.
 
Because the points tend to agglomerate in the center, they are smaller there.
 
[[File:Fōrmulæ - Sunflower model 01.png]]
 
[[File:Fōrmulæ - Sunflower model 02.png]]
 
[[File:Fōrmulæ - Sunflower model 03.png]]
 
'''Improvement'''
 
Last result is not natural. Florets in a sunflower are all equal size.
 
H. Vogel proposed to use a Fermat spiral, in such a case, the florets are equally spaced, and we can use now circles with the same size:
 
[[File:Fōrmulæ - Sunflower model 04.png]]
 
[[File:Fōrmulæ - Sunflower model 05.png]]
 
[[File:Fōrmulæ - Sunflower model 06.png]]
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
window 1, @"Sunflower Fractal", ( 0, 0, 400, 400 )
WindowSetBackgroundColor( 1, fn ColorBlack )
 
void local fn SunflowerFractal
NSUinteger seeds = 4000
double c, i, angle, x, y, r
pen 2.0, fn ColorWithRGB( 0.997, 0.838, 0.038, 1.0 )
c = ( sqr(5) + 1 ) / 2
for i = 0 to seeds
r = (i ^ c) / seeds
angle = 2 * pi * c * i
x = r * sin(angle) + 200
y = r * cos(angle) + 200
oval ( x, y, i / seeds * 5, i / seeds * 5 )
next
end fn
 
fn SunflowerFractal
 
HandleEvents
</syntaxhighlight>
[[file:Sunflower_Fractal.png]]
 
=={{header|Go}}==
Line 150 ⟶ 331:
<br>
The image produced, when viewed with (for example) EOG, is similar to the Ring entry.
<langsyntaxhighlight lang="go">package main
 
import (
Line 177 ⟶ 358:
dc.Stroke()
dc.SavePNG("sunflower_fractal.png")
}</langsyntaxhighlight>
 
=={{header|javascript}}==
Line 199 ⟶ 380:
</html>
</pre>
<langsyntaxhighlight lang="javascript">const SIZE = 400, HS = SIZE >> 1, WAIT = .005, SEEDS = 3000,
TPI = Math.PI * 2, C = (Math.sqrt(10) + 1) / 2;
class Sunflower {
Line 255 ⟶ 436:
const sunflower = new Sunflower();
sunflower.start();
}</langsyntaxhighlight>
 
=={{header|J}}==
 
This (currently draft) task really needs an adequate description. Still, it's straightforward to derive code from another implementation on this page.
 
This implementation assumes a recent J implementation (for example, J903):
 
<syntaxhighlight lang="j">require'format/printf'
 
sunfract=: {{ NB. y: number of "sunflower seeds"
phi=. 0.5*1+%:5
XY=. (y%10)+(2*(I^phi)%y) * +.^j.2*1p1*phi*I=.1+i.y
XY,.(%:I)%13
}}
 
sunfractsvg=: {{
fract=. sunfract x
C=.,'\n<circle cx="%.2f" cy="%.2f" r="%.1f" />' sprintf fract
({{)n
<svg xmlns="http://www.w3.org/2000/svg" width="%d" height="%d" style="stroke:gold">
<rect width="100%%" height="100%%" fill="black" />
%s
</svg>
}} sprintf (;/<.20+}:>./fract),<C) fwrite y}}
</syntaxhighlight>
 
Example use:
 
<syntaxhighlight lang="j">
3000 sunfractsvg '~/sunfract.html'
129147
</syntaxhighlight>
 
(The number displayed is the size of the generated file.)
 
=={{header|jq}}==
'''Adapted from [[#Perl|Perl]]'''
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
 
<syntaxhighlight lang="jq"># SVG headers
def svg(size):
"<svg xmlns='http://www.w3.org/2000/svg' width='\(size)'",
"height='\(size)' style='stroke:gold'>",
"<rect width='100%' height='100%' fill='black'/>";
 
# emit the "<circle />" elements
def sunflower(size):
def rnd: 100*.|round/100;
 
(5 * size) as $seeds
| ((1|atan) * 4) as $pi
| ((1 + (5|sqrt)) / 2) as $phi
| range(1; 1 + $seeds) as $i
| {}
| .r = 2 * pow($i; $phi)/$seeds
| .theta = 2 * $pi * $phi * $i
| .x = .r * (.theta|sin) + size/2
| .y = .r * (.theta|cos) + size/2
| .radius = ($i|sqrt)/13
| "<circle cx='\(.x|rnd)' cy='\(.y|rnd)' r='\(.radius|rnd)' />" ;
 
def end_svg:
"</svg>";
 
svg(600),
sunflower(600),
end_svg</syntaxhighlight>
 
=={{header|Julia}}==
{{trans|R}}
Run from REPL.
<langsyntaxhighlight lang="julia">using MakieGLMakie
 
function sunflowerplot()
Line 270 ⟶ 519:
for i in 2:length(r)
θ[i] = θ[i - 1] + 2π * ϕ
markersizes[i] = div(i, 500) + 39
end
x = r .* cos.(θ)
y = r .* sin.(θ)
scenef = SceneFigure(backgroundcolor=:green)
ax = Axis(f[1, 1], backgroundcolor = :green)
scatter!(scene, x, y, color=:gold, markersize=markersizes, strokewidth=1, fill=false, show_axis=false)
scatter!(ax, x, y, color = :gold, markersize = markersizes, strokewidth = 1)
hidespines!(ax)
hidedecorations!(ax)
return f
end
 
sunflowerplot()
</syntaxhighlight>
</lang>
 
{{output}}
 
[[File:Sunflower-julia.png|center|thumb]]
 
 
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">
<lang lb>
nomainwin
UpperLeftX=1:UpperLeftY=1
Line 312 ⟶ 570:
close #1
end
</syntaxhighlight>
</lang>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">numseeds = 3000;
pts = Table[
i = N[ni];
Line 324 ⟶ 582:
{ni, numseeds}
];
Graphics[pts]</langsyntaxhighlight>
 
=={{header|Microsoft Small Basic}}==
{{trans|Ring}}
<langsyntaxhighlight lang="smallbasic">' Sunflower fractal - 24/07/2018
GraphicsWindow.Width=410
GraphicsWindow.Height=400
Line 339 ⟶ 597:
y=r*Math.Cos(angle)+200
GraphicsWindow.DrawEllipse(x, y, i/numberofseeds*10, i/numberofseeds*10)
EndFor </langsyntaxhighlight>
{{out}}
[https://1drv.ms/u/s!AoFH_AlpH9oZgf5kvtRou1Wuc5lSCg Sunflower fractal]
Line 346 ⟶ 604:
{{trans|Go}}
{{libheader|imageman}}
<langsyntaxhighlight Nimlang="nim">import math
import imageman
 
Line 369 ⟶ 627:
image.drawCircle(x, y, toInt(8 * fi / Fn), Foreground)
 
image.savePNG("sunflower.png", compression = 9)</langsyntaxhighlight>
 
=={{header|Objeck}}==
{{trans|C}}
<langsyntaxhighlight lang="perl">use Game.SDL2;
use Game.Framework;
 
Line 445 ⟶ 703:
SCREEN_HEIGHT := 480
}
</syntaxhighlight>
</lang>
 
=={{header|Perl}}==
{{trans|Sidef}}
<langsyntaxhighlight lang="perl">use utf8;
use constant π => 3.14159265;
use constant φ => (1 + sqrt(5)) / 2;
Line 467 ⟶ 725:
}
 
print "</svg>\n";</langsyntaxhighlight>
See [https://github.com/SqrtNegInf/Rosettacode-Perl5-Smoke/blob/master/ref/sunflower.svg Phi-packing image] (SVG image)
 
=={{header|Phix}}==
{{libheader|Phix/pGUI}}
{{libheader|Phix/online}}
<lang Phix>constant numberofseeds = 3000
You can run this online [http://phix.x10.mx/p2js/SunflowerFractal.htm here].
<!--<syntaxhighlight lang="phix">(phixonline)-->
<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;">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: #0000FF;">,</span> <span style="color: #000000;">cdcanvas</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: #004080;">integer</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;">hw</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">hh</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sq_floor_div</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: #000000;">2</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">min</span><span style="color: #0000FF;">(</span><span style="color: #000000;">hw</span><span style="color: #0000FF;">,</span><span style="color: #000000;">hh</span><span style="color: #0000FF;">)/</span><span style="color: #000000;">150</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">f</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">min</span><span style="color: #0000FF;">(</span><span style="color: #000000;">hw</span><span style="color: #0000FF;">,</span><span style="color: #000000;">hh</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">8</span><span style="color: #0000FF;">/</span><span style="color: #000000;">125</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: #004080;">atom</span> <span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">(</span><span style="color: #7060A8;">sqrt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">5</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: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #000000;">numberofseeds</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">)/</span><span style="color: #000000;">numberofseeds</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">angle</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;">c</span><span style="color: #0000FF;">*</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">x</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">*</span><span style="color: #000000;">r</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">sin</span><span style="color: #0000FF;">(</span><span style="color: #000000;">angle</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">hw</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">y</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">*</span><span style="color: #000000;">r</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">cos</span><span style="color: #0000FF;">(</span><span style="color: #000000;">angle</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">hh</span>
<span style="color: #7060A8;">cdCanvasCircle</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</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;">i</span><span style="color: #0000FF;">/</span><span style="color: #000000;">numberofseeds</span><span style="color: #0000FF;">*</span><span style="color: #000000;">f</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</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: #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_WHITE</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_BLACK</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;">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: #004600;">NULL</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: #008000;">"602x502"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- initial size</span>
<span style="color: #7060A8;">IupSetCallback</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</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: #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;">IupSetAttribute</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;">"Sunflower"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupSetCallback</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</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: #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: #000080;font-style:italic;">-- release the minimum limitation</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}}==
include pGUI.e
{{trans|C}}
<syntaxhighlight lang="java">
//Abhishek Ghosh, 26th June 2022
 
size(1000,1000);
Ihandle dlg, canvas
surface.setTitle("Sunflower...");
cdCanvas cddbuffer, cdcanvas
 
int iter = 3000;
function redraw_cb(Ihandle /*ih*/, integer /*posx*/, integer /*posy*/)
float factor = .5 + sqrt(1.25),r,theta,diskRatio=0.5;
float x = width/2.0, y = height/2.0;
double maxRad = pow(iter,factor)/iter;
int i;
background(#add8e6); //Lightblue background
for(i=0;i<=iter;i++){
r = pow(i,factor)/iter;
 
if(r/maxRad < diskRatio){
integer {hw, hh} = sq_floor_div(IupGetIntInt(canvas, "DRAWSIZE"),2)
stroke(#000000); // Black central disk
atom s = min(hw,hh)/150,
}
f = min(hw,hh)*8/125
else
cdCanvasActivate(cddbuffer)
stroke(#ffff00); // Yellow Petals
cdCanvasClear(cddbuffer)
atom c = (sqrt(5)+1)/2
for i=0 to numberofseeds do
atom r = power(i,c)/numberofseeds,
angle = 2*PI*c*i,
x = s*r*sin(angle)+hw,
y = s*r*cos(angle)+hh
cdCanvasCircle(cddbuffer,x,y,i/numberofseeds*f)
end for
cdCanvasFlush(cddbuffer)
return IUP_DEFAULT
end function
 
theta = 2*PI*factor*i;
function map_cb(Ihandle ih)
ellipse(x + r*sin(theta), y + r*cos(theta), 10 * i/(1.0*iter),10 * i/(1.0*iter));
cdcanvas = cdCreateCanvas(CD_IUP, ih)
}
cddbuffer = cdCreateCanvas(CD_DBUFFER, cdcanvas)
</syntaxhighlight>
cdCanvasSetBackground(cddbuffer, CD_WHITE)
cdCanvasSetForeground(cddbuffer, CD_BLACK)
return IUP_DEFAULT
end function
 
procedure main()
IupOpen()
canvas = IupCanvas(NULL)
IupSetAttribute(canvas, "RASTERSIZE", "602x502") -- initial size
IupSetCallback(canvas, "MAP_CB", Icallback("map_cb"))
 
dlg = IupDialog(canvas)
IupSetAttribute(dlg, "TITLE", "Sunflower")
IupSetCallback(canvas, "ACTION", Icallback("redraw_cb"))
 
IupMap(dlg)
IupSetAttribute(canvas, "RASTERSIZE", NULL) -- release the minimum limitation
IupShowXY(dlg,IUP_CENTER,IUP_CENTER)
IupMainLoop()
IupClose()
end procedure
main()</lang>
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">
from turtle import *
from math import *
Line 577 ⟶ 871:
 
done()
</syntaxhighlight>
</lang>
 
=={{header|R}}==
<syntaxhighlight lang="r">
<lang R>
phi=1/2+sqrt(5)/2
r=seq(0,1,length.out=2000)
Line 597 ⟶ 891:
points(x[i],y[i],cex=size[i],lwd=thick[i],col="goldenrod1")
}
</syntaxhighlight>
</lang>
{{Out}}
[https://raw.githubusercontent.com/schwartstack/sunflower/master/sunflower2.png Sunflower]
Line 605 ⟶ 899:
{{trans|C}}
 
<langsyntaxhighlight lang="racket">#lang racket
 
(require 2htdp/image)
Line 623 ⟶ 917:
(+ (/ WIDTH 2) (* r (sin theta)))
(+ (/ HEIGHT 2) (* r (cos theta)))
image))</langsyntaxhighlight>
 
=={{header|Raku}}==
Line 632 ⟶ 926:
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]
 
<syntaxhighlight lang="raku" perl6line>use SVG;
 
my $seeds = 3000;
Line 651 ⟶ 945:
|@c.map( { :circle[:cx(.[0]), :cy(.[1]), :r(.[2])] } ),
],
);</langsyntaxhighlight>
See: [https://github.com/thundergnat/rc/blob/master/img/phi-packing-perl6.svg Phi packing] (SVG image)
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Sunflower fractal
 
Line 707 ⟶ 1,001:
}
label1 { setpicture(p1) show() }
</syntaxhighlight>
</lang>
Output:
 
Line 714 ⟶ 1,008:
=={{header|Sidef}}==
{{trans|Go}}
<langsyntaxhighlight lang="ruby">require('Imager')
 
func draw_sunflower(seeds=3000) {
Line 735 ⟶ 1,029:
 
var img = draw_sunflower()
img.write(file => "sunflower.png")</langsyntaxhighlight>
Output image: [https://github.com/trizen/rc/blob/master/img/sunflower-sidef.png Sunflower fractal]
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">import gg
import gx
import math
 
fn main() {
mut context := gg.new_context(
bg_color: gx.rgb(255, 255, 255)
width: 400
height: 400
frame_fn: frame
)
context.run()
}
 
fn frame(mut ctx gg.Context) {
ctx.begin()
c := (math.sqrt(5) + 1) / 2
num_of_seeds := 3000
for i := 0; i <= num_of_seeds; i++ {
mut fi := f64(i)
n := f64(num_of_seeds)
r := math.pow(fi, c) / n
angle := 2 * math.pi * c * fi
x := r*math.sin(angle) + 200
y := r*math.cos(angle) + 200
fi /= n / 5
ctx.draw_circle_filled(f32(x), f32(y), f32(fi), gx.black)
}
ctx.end()
}</syntaxhighlight>
 
=={{header|Wren}}==
{{trans|Go}}
{{libheader|DOME}}
<langsyntaxhighlight ecmascriptlang="wren">import "graphics" for Canvas, Color
import "dome" for Window
 
Line 771 ⟶ 1,097:
}
}
}</langsyntaxhighlight>
 
=={{header|XPL0}}==
[[File:SunflowerXPL0.gif|200px|thumb|right]]
<syntaxhighlight lang "XPL0">
 
proc DrawCircle(X0, Y0, R, Color);
int X0, Y0, R, Color;
int X, Y, R2;
[R2:= R*R;
for Y:= -R to +R do
for X:= -R to +R do
if X*X + Y*Y <= R2 then
Point(X+X0, Y+Y0, Color);
];
 
def Seeds = 3000, Color = $0E; \yellow
def ScrW = 800, ScrH = 600;
def Phi = (sqrt(5.)+1.) / 2.; \golden ratio (1.618...)
def Pi = 3.14159265358979323846;
real R, Angle, X, Y;
int I;
[SetVid($103);
for I:= 0 to Seeds-1 do
[R:= Pow(float(I), Phi) / float(Seeds/2);
Angle:= 2. * Pi * Phi * float(I);
X:= R*Sin(Angle);
Y:= R*Cos(Angle);
DrawCircle(ScrW/2+fix(X), ScrH/2-fix(Y), I*7/Seeds, Color);
];
]</syntaxhighlight>
 
=={{header|Yabasic}}==
{{trans|Wren}}
<syntaxhighlight lang="yabasic">// Rosetta Code problem: http://rosettacode.org/wiki/Sunflower_fractal
// Adapted from Wren to Yabasic by Galileo, 01/2022
 
width = 400
height = 400
 
open window width, height
backcolor 0,0,0
clear window
 
color 0,255,0
seeds = 3000
 
c = (sqrt(5) + 1) / 2
for i = 0 to seeds
r = (i ** c) / seeds
angle = 2 * pi * c * i
x = r * sin(angle) + 200
y = r * cos(angle) + 200
circle x, y, i / seeds * 5
next</syntaxhighlight>
 
=={{header|zkl}}==
Line 777 ⟶ 1,157:
Uses Image Magick and
the PPM class from http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#zkl
<langsyntaxhighlight lang="zkl">fcn sunflower(seeds=3000){
img,color := PPM(400,400), 0x00ff00; // green
c:=((5.0).sqrt() + 1)/2;
Line 787 ⟶ 1,167:
}
img.writeJPGFile("sunflower.zkl.jpg");
}();</langsyntaxhighlight>
{{out}}
Image at [http://www.zenkinetic.com/Images/RosettaCode/sunflower.zkl.jpg sunflower fractal]
2,120

edits