Sunflower fractal: Difference between revisions
m
→{{header|Fōrmulæ}}
(with credit to homme_chauve_souris. The 2*pi typo is corrected with their solution to use 8*ATN(1)) |
|||
(13 intermediate revisions by 8 users not shown) | |||
Line 9:
{{trans|Perl}}
<
phi = (1 + sqrt(5)) / 2
size = 600
Line 23:
r * cos(t) + size / 2, sqrt(i) / 13))
print(‘</svg>’)</
=={{header|Action!}}==
Line 29:
{{libheader|Action! Tool Kit}}
{{libheader|Action! Real Math}}
<
INT ARRAY SinTab=[
Line 144:
DO UNTIL CH#$FF OD
CH=$FF
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Sunflower_fractal.png Screenshot from Atari 8-bit computer]
=={{header|Applesoft BASIC}}==
<
=={{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">
/*Abhishek Ghosh, 14th September 2018*/
Line 193:
return 0;
}
</syntaxhighlight>
=={{header|C++}}==
{{trans|Perl}}
<
#include <fstream>
#include <iostream>
Line 237:
}
return EXIT_SUCCESS;
}</
{{out}}
[[Media:Sunflower cpp.svg]]
=={{header|FreeBASIC}}==
<
Const PI As Double = 4 * Atn(1)
Const ancho = 400
Line 269:
Sleep
End
</syntaxhighlight>
=={{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 276 ⟶ 331:
<br>
The image produced, when viewed with (for example) EOG, is similar to the Ring entry.
<
import (
Line 303 ⟶ 358:
dc.Stroke()
dc.SavePNG("sunflower_fractal.png")
}</
=={{header|javascript}}==
Line 325 ⟶ 380:
</html>
</pre>
<
TPI = Math.PI * 2, C = (Math.sqrt(10) + 1) / 2;
class Sunflower {
Line 381 ⟶ 436:
const sunflower = new Sunflower();
sunflower.start();
}</
=={{header|J}}==
Line 389 ⟶ 444:
This implementation assumes a recent J implementation (for example, J903):
<
sunfract=: {{ NB. y: number of "sunflower seeds"
Line 406 ⟶ 461:
</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.)
Line 422 ⟶ 477:
'''Works with gojq, the Go implementation of jq'''
<
def svg(size):
"<svg xmlns='http://www.w3.org/2000/svg' width='\(size)'",
Line 449 ⟶ 504:
svg(600),
sunflower(600),
end_svg</
=={{header|Julia}}==
{{trans|R}}
Run from REPL.
<
function sunflowerplot()
Line 464 ⟶ 519:
for i in 2:length(r)
θ[i] = θ[i - 1] + 2π * ϕ
markersizes[i] = div(i, 500) +
end
x = r .* cos.(θ)
y = r .* sin.(θ)
ax = Axis(f[1, 1], backgroundcolor = :green)
scatter!(ax, x, y, color = :gold, markersize = markersizes, strokewidth = 1)
hidespines!(ax)
hidedecorations!(ax)
return f
end
sunflowerplot()
</syntaxhighlight>
{{output}}
[[File:Sunflower-julia.png|center|thumb]]
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">
nomainwin
UpperLeftX=1:UpperLeftY=1
Line 506 ⟶ 570:
close #1
end
</syntaxhighlight>
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
pts = Table[
i = N[ni];
Line 518 ⟶ 582:
{ni, numseeds}
];
Graphics[pts]</
=={{header|Microsoft Small Basic}}==
{{trans|Ring}}
<
GraphicsWindow.Width=410
GraphicsWindow.Height=400
Line 533 ⟶ 597:
y=r*Math.Cos(angle)+200
GraphicsWindow.DrawEllipse(x, y, i/numberofseeds*10, i/numberofseeds*10)
EndFor </
{{out}}
[https://1drv.ms/u/s!AoFH_AlpH9oZgf5kvtRou1Wuc5lSCg Sunflower fractal]
Line 540 ⟶ 604:
{{trans|Go}}
{{libheader|imageman}}
<
import imageman
Line 563 ⟶ 627:
image.drawCircle(x, y, toInt(8 * fi / Fn), Foreground)
image.savePNG("sunflower.png", compression = 9)</
=={{header|Objeck}}==
{{trans|C}}
<
use Game.Framework;
Line 639 ⟶ 703:
SCREEN_HEIGHT := 480
}
</syntaxhighlight>
=={{header|Perl}}==
{{trans|Sidef}}
<
use constant π => 3.14159265;
use constant φ => (1 + sqrt(5)) / 2;
Line 661 ⟶ 725:
}
print "</svg>\n";</
See [https://github.com/SqrtNegInf/Rosettacode-Perl5-Smoke/blob/master/ref/sunflower.svg Phi-packing image] (SVG image)
Line 668 ⟶ 732:
{{libheader|Phix/online}}
You can run this online [http://phix.x10.mx/p2js/SunflowerFractal.htm here].
<!--<
<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>
Line 723 ⟶ 787:
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</
=={{header|Processing}}==
{{trans|C}}
<
//Abhishek Ghosh, 26th June 2022
Line 753 ⟶ 817:
ellipse(x + r*sin(theta), y + r*cos(theta), 10 * i/(1.0*iter),10 * i/(1.0*iter));
}
</syntaxhighlight>
=={{header|Python}}==
<
from turtle import *
from math import *
Line 807 ⟶ 871:
done()
</syntaxhighlight>
=={{header|R}}==
<syntaxhighlight lang="r">
phi=1/2+sqrt(5)/2
r=seq(0,1,length.out=2000)
Line 827 ⟶ 891:
points(x[i],y[i],cex=size[i],lwd=thick[i],col="goldenrod1")
}
</syntaxhighlight>
{{Out}}
[https://raw.githubusercontent.com/schwartstack/sunflower/master/sunflower2.png Sunflower]
Line 835 ⟶ 899:
{{trans|C}}
<
(require 2htdp/image)
Line 853 ⟶ 917:
(+ (/ WIDTH 2) (* r (sin theta)))
(+ (/ HEIGHT 2) (* r (cos theta)))
image))</
=={{header|Raku}}==
Line 862 ⟶ 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"
my $seeds = 3000;
Line 881 ⟶ 945:
|@c.map( { :circle[:cx(.[0]), :cy(.[1]), :r(.[2])] } ),
],
);</
See: [https://github.com/thundergnat/rc/blob/master/img/phi-packing-perl6.svg Phi packing] (SVG image)
=={{header|Ring}}==
<
# Project : Sunflower fractal
Line 937 ⟶ 1,001:
}
label1 { setpicture(p1) show() }
</syntaxhighlight>
Output:
Line 944 ⟶ 1,008:
=={{header|Sidef}}==
{{trans|Go}}
<
func draw_sunflower(seeds=3000) {
Line 965 ⟶ 1,029:
var img = draw_sunflower()
img.write(file => "sunflower.png")</
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
Line 998 ⟶ 1,062:
}
ctx.end()
}</
=={{header|Wren}}==
{{trans|Go}}
{{libheader|DOME}}
<
import "dome" for Window
Line 1,033 ⟶ 1,097:
}
}
}</
=={{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}}
<
// Adapted from Wren to Yabasic by Galileo, 01/2022
Line 1,057 ⟶ 1,151:
y = r * cos(angle) + 200
circle x, y, i / seeds * 5
next</
=={{header|zkl}}==
Line 1,063 ⟶ 1,157:
Uses Image Magick and
the PPM class from http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#zkl
<
img,color := PPM(400,400), 0x00ff00; // green
c:=((5.0).sqrt() + 1)/2;
Line 1,073 ⟶ 1,167:
}
img.writeJPGFile("sunflower.zkl.jpg");
}();</
{{out}}
Image at [http://www.zenkinetic.com/Images/RosettaCode/sunflower.zkl.jpg sunflower fractal]
|