Golfing Flower of Life

19

0

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.

Benjamin Spector

Posted 2016-11-14T03:23:55.640

Reputation: 193

10Welcome 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. – James – 2016-11-14T05:26:14.083

2

"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.

– Mego – 2016-11-14T09:37:42.690

In addition to such abstract rule like “recognizable”, would be better to specify a minimum size. – manatwork – 2016-11-14T11:03:12.240

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: http://meta.codegolf.stackexchange.com/questions/2140/sandbox-for-proposed-challenges

– tuskiomi – 2016-11-14T16:13:11.747

1It'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) – Martin Ender – 2016-11-14T19:27:32.717

@MartinEnder Yeah, I just reached the point of completing the circles, then I realized I was missing this. It's making me rethink whether it's worth the effort. – mbomb007 – 2016-11-14T19:39:34.240

Answers

23

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).

Martin Ender

Posted 2016-11-14T03:23:55.640

Reputation: 184 808

Use √3 to save 2 characters & 0 bytes, while eliminating a source of numerical error. – Kelly Lowder – 2016-11-23T07:23:22.990

@KellyLowder Good point, fixed. – Martin Ender – 2016-11-23T10:51:04.893

8

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.

Henry Wildermuth

Posted 2016-11-14T03:23:55.640

Reputation: 81

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 ';' – HyperNeutrino – 2016-11-14T04:01:52.410

@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? – Henry Wildermuth – 2016-11-14T04:09:08.053

1Worked for me on desktop version (Ubuntu 14.04 LTS). – Benjamin Spector – 2016-11-14T04:20:00.103

1I 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. – Benjamin Spector – 2016-11-14T04:23:11.323

6

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.

Kelly Lowder

Posted 2016-11-14T03:23:55.640

Reputation: 3 225

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. – Martin Ender – 2016-11-22T09:27:28.913

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.

– Martin Ender – 2016-11-22T09:34:11.863

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. – Kelly Lowder – 2016-11-22T17:22:44.420

3

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.

Neil

Posted 2016-11-14T03:23:55.640

Reputation: 95 035