19
\$\begingroup\$

The challenge here is to accurately depict the flower of life (which is a sacred geometrical figure according to some) in the language of your choice.

flower of life

The design consists an arrangement of circles and partial circles of radius 1 as shown whose centres arranged on a triangular grid of pitch 1, plus one larger circle of radius 3 surrounding them.

The design can be scaled as you like, but a max error of 2% from mathematically correct is permitted. If using raster graphics, this effectively limits the diameter of the small circles to at least about 100 pixels.

Since this is code-golf, shortest code (bytes) wins.

\$\endgroup\$
  • 10
    \$\begingroup\$ Welcome to the site! Just so you know, it's generally encouraged to wait a while before accepting an answer, that way other users won't see the contest as "over", and there will be more participation and competition. \$\endgroup\$ – James Nov 14 '16 at 5:26
  • 2
    \$\begingroup\$ "visual, recognizable, and correct" is neither clear nor objective. It's impossible to determine whether or not a submission is valid without objective validity criteria. We encourage use of the Sandbox to work out any potential issues with challenges before posting them to the main site. \$\endgroup\$ – Mego Nov 14 '16 at 9:37
  • \$\begingroup\$ In addition to such abstract rule like “recognizable”, would be better to specify a minimum size. \$\endgroup\$ – manatwork Nov 14 '16 at 11:03
  • \$\begingroup\$ Since nobody's plugged it yet, we have a sandbox over at the meta that is designed to help new challenges get feedback. You can find it here: meta.codegolf.stackexchange.com/questions/2140/… \$\endgroup\$ – tuskiomi Nov 14 '16 at 16:13
  • 1
    \$\begingroup\$ It's actually not just 19 circles. There are some circular arcs at the edges as well. (6 of them covering an angle of 2π/3, 12 covering π, 18 covering π/6) \$\endgroup\$ – Martin Ender Nov 14 '16 at 19:27
23
\$\begingroup\$

Mathematica, 177 173 128 124 120 bytes

c=Circle;Graphics@{{0,0}~c~3,Rotate[Table[If[-3<x-y<4,c[{√3x,-x+2y}/2,1,Pi/{6,2}]],{x,-3,2},{y,-4,2}],Pi/3#]&~Array~6}

enter image description here

The main idea is to compose the result from six rotated versions of this:

enter image description here

This in turn is a rectangular table of identical circle arcs with two corners cut off. If we remove the shearing and represent each circle center with a #, we basically want to distribute the circles in this pattern:

####
#####
######
######
 #####
  ####

These edges are cut off by imposing the condition -3 < x-y < 4 on the 2D indices (since the value of x-y is constant along diagonals) and the shearing comes from multiplying these x and y by non-orthogonal basis vectors which span the grid we're looking for.

This particular orientation of the unrotated arcs turns out to be the shortest since both ends of the arc evenly divide Pi so that the arc can be expressed as Pi/{6,2} (all other arcs would either require and additional minus sign or integers in the numerator).

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Use √3 to save 2 characters & 0 bytes, while eliminating a source of numerical error. \$\endgroup\$ – Kelly Lowder Nov 23 '16 at 7:23
  • \$\begingroup\$ @KellyLowder Good point, fixed. \$\endgroup\$ – Martin Ender Nov 23 '16 at 10:51
8
\$\begingroup\$

OpenSCAD, 228 bytes

$fn=99;module o(a=9){difference(){circle(a);circle(a-1);}}function x(n)=9*[sin(n*60),cos(n*60)];module q(g){for(i=[1:6])if(g>0){translate(x(i))union(){o();q(g-1);}}else{intersection(){translate(x(i))o();circle(9);}}}q(2);o(27);

The below is a version allowing someone to set parameters r (radius) and w (width of rings).

r=1;w=.1;$fn=99;module o(n){difference(){circle(n);circle(n-w);}}function x(n)=(r-w/2)*[sin(n*60),cos(n*60)];module q(g){for(i=[1:6])if(g>0){translate(x(i))union(){o(r);q(g-1);}}else{intersection(){translate(x(i))o(r);circle(r);}}}q(2);o(3*r-w);

This version is xactly 246 characters.
Some of this code is technically unnecessary but makes it look more like the picture.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I'm getting this error on OpenSCAD.net (Firefox 47): Error: Error: Parse error on line 1: ...x(k))o();circle(9);};}}}q(2);o(27); -----------------------^ Expecting 'TOK_ID', '}', 'TOK_IF', '!', '#', '%', '*', got ';' \$\endgroup\$ – HyperNeutrino Nov 14 '16 at 4:01
  • \$\begingroup\$ @AlexL. I am not getting that error when I use the actual OpenSCAD client on my mac. However I was able to replicate that error on OpenSCAD.net using chrome, which is disappointing. It think it is an issue on with their service. Could you try the desktop application? \$\endgroup\$ – Henry Wildermuth Nov 14 '16 at 4:09
  • 1
    \$\begingroup\$ Worked for me on desktop version (Ubuntu 14.04 LTS). \$\endgroup\$ – Benjamin Spector Nov 14 '16 at 4:20
  • 1
    \$\begingroup\$ I noticed the pattern };} in your code. I think you could safely replace that with }} which would shave a character for both versions. Tested it myself and seemed to work. \$\endgroup\$ – Benjamin Spector Nov 14 '16 at 4:23
6
\$\begingroup\$

Mathematica 263 Bytes

Not really competitive with @MartinEnder's submission but I had fun with this nonetheless. I let the petals do a random walk! The petal walks by rotating 60 degrees randomly about one of the endpoints which is also randomly chosen. I test to see if the rotating end of the petal falls outside the large disk, and if so, the rotation goes the other way.

c=Circle;a=√3;v={e=0{,},{0,2}};f=RandomChoice;Graphics@{e~c~6,Table[q=f@{1,2};t=f@{p=Pi/3,-p};r=RotationTransform[#,v[[q]]]&;v=r[If[r[t]@v[[-q]]∈e~Disk~6,t,-t]]@v;Translate[Rotate[{c[{1,a},2,p{4,5}],c[{1,-a},2,p{1,2}]},ArcTan@@(#-#2)&@@v,e],v[[2]]],{5^5}]}

Here is the subsequent code I used for the animation.

Export[NotebookDirectory[]<>"flower.gif", Table[Graphics[Join[{c[e,6]},(List@@%)[[1,2,1;;n-1]],{Thick,Red,(List@@%)[[1,2,n]]}]],{n,1,3^4,1}]]

Random Petal Walk

I read somewhere that 2-dimensional random walks must eventually return to the origin. It seems like a few thousand steps guarantee filling the large disk.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ This is a really nice idea, but with a finite number of iterations there's also a non-zero probability of not filling up the entire flower. You should probably add a termination condition once you've drawn 30 petals (that seems like the shortest way to determine whether you're actually done). Then you'd know the drawing is done when you terminate, and the probability of not terminating would be zero. \$\endgroup\$ – Martin Ender Nov 22 '16 at 9:27
  • \$\begingroup\$ Independently of that, there's a lot of bytes you can save: Sqrt[3] is 3^.5. You don't need to define e until its first use and you can use this trick to save a byte on it, so v={e=0{,},{0,2}}. You can also use some infix notation like e~c~6 or e~Disk~6, and ArcTan@@Subtract@@v should be equivalent to ArcTan@@(#-#2)&@@v. \$\endgroup\$ – Martin Ender Nov 22 '16 at 9:34
  • \$\begingroup\$ Used infix and also the √ operator to shave off 15 bytes. Thanks for the tips @MartinEnder. Got rid of /2 and 2* which cancelled. There are 180 petals so 4^4 iterations typically covers about half the disk, and 5^5 does the job. The probability of 9^9 not covering the disk is ~ 1 out of 10^400,000 which is much less than 2% error, so not worth the wasted bytes to check. \$\endgroup\$ – Kelly Lowder Nov 22 '16 at 17:22
3
\$\begingroup\$

JavaScript (ES6)/SVG, 299 bytes

with(document){write(`<svg height=250 width=250><circle${b=` fill=none stroke=black `}cx=125 cy=125 r=120 />`);for(i=0;i<24;i++)write(`<path${b}d=M5,125${`${a=`a60,60,0,0,1,`}40,0`.repeat(i%4+3)+`${a}-40,0`.repeat(i%4+3)} transform=${`rotate(60,125,125)`.repeat(i>>2)}rotate(-60,${i%4*4}5,125) />`)}

Works by generating multiple arc pairs of various lengths then rotating them into place.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.