7
\$\begingroup\$

Seeing as its St. Patrick's day, I thought I would have a little challenge

Your task is to write a program draws a clover in any way possible. Each leaf has to be symmetrical, and the user has to input the number of leaves that they want on the clover. It then must be drawn. This has to be generated. You cannot just load an image or have ASCII art saved as a variable. However using modules like turtle to generate (and draw is included under generate) are fine.

You can use any language. This is a code-golf so the shortest answer wins.

Begin! Happy St Patrick's Day!

Just for reference, here is an example of a 3 leaf clover http://img.brothersoft.com/screenshots/softimage/t/three_leaf_clover_theme-202679-1230690274.jpeg

Just for DavidCarraher Clover

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Also, you might want to specify a shamrock instead of a clover! \$\endgroup\$ – DavidC Mar 18 '14 at 3:33
12
\$\begingroup\$

Mathematica - 106 bytes

c=RegionPlot[x^2+y^2<Abs@Sin[#x~ArcTan~y/2]||x>0&&y^2<.001,{x,-1,1},{y,-1,1},Frame->0>1,PlotStyle->Green]&

Ungolfed version:

c[leaves_] := (
    angle = ArcTan[x,y];
    RegionPlot[
        x^2 + y^2 < Abs[Sin[leaves*angle/2]]
        || x > 0 && y^2 < .001
      , {x,-1,1}
      , {y,-1,1}
      , Frame -> False, PlotStyle -> Green
    ]);

Here are the outputs for c[3] through c[6].

enter image description here

At the cost of another 7 bytes you can improve the colour (using PlotStyle->Darker@Green or PlotStyle->Hue[.3,1,.7] instead), and for another 15 bytes you can remove some of the sampling artifacts (using an additional option ,PlotPoints->90), giving a total of 128 bytes for these beauties:

enter image description here

The braces and commes in those pictures are not produced by c, but just by how I output them to fit them all in one row.

Lastly, here is an attempt at somewhat neater shading. I didn't even bother golfing this down further, the option names are just too long. I'm not even sure I'm too pleased with the result, but I thought I'd post it anyway. This is 188 bytes as it stands:

c=RegionPlot[x^2+y^2<Abs@Sin[(l=#)x~(a=ArcTan)~y/2]||x>0&&y^2<.001,{x,-1,1},{y,-1,1},Frame->0>1,PlotPoints->90,ColorFunction->(Hue[.3,1,.5+.2Sin[.5l#~a~#2]^8]&),ColorFunctionScaling->0>1]&

enter image description here

\$\endgroup\$
3
  • \$\begingroup\$ Your Abs[] has no closing bracket \$\endgroup\$ – Dr. belisarius Mar 18 '14 at 23:08
  • \$\begingroup\$ let's see a 3d version too? \$\endgroup\$ – Michael Stern Mar 21 '14 at 13:21
  • \$\begingroup\$ @MichaelStern tempting :) ... but maybe some other time... I just spent my entire free time for today on another answer ^^ \$\endgroup\$ – Martin Ender Mar 21 '14 at 13:34
4
\$\begingroup\$

C (145 126)

float d=2/--r,x,y=-1;for(;y<=1;y+=d,puts(""))for(x=-1;x<=1;x+=d)putchar(x*x+y*y<fabs(sin(l*atan2(x,y)))||fabs(x)<d&y>0?35:32);

Draws a clove in ASCII art; the function is passed the number of desired leaves and the resolution in characters. See example here: http://ideone.com/YDGN4H

Note that the result is much nicer when the horizontal resolution is doubled, at the cost of a slightly more verbose code (see http://ideone.com/RYaLz4).

Full version:

#include <math.h>
#include <stdio.h>

void clover(float l, float r)
{
    float d = 2.f / --r;
    float x, y=-1.f;
    for (; y<=1.f; y+=d){
        for (x=-1.f; x<=1.; x+=d){
            printf((x*x + y*y < fabs(sin(l * atan2(x,y))) | (fabs(x) < d & y > 0)) ? "#" : " ");
        }
        printf("\n");
    }
}

int main(int argc, char* argv[])
{
    clove(4.f, 48.f);
    return 0;
}
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Best use of variables names l and r! \$\endgroup\$ – Martin Ender Mar 18 '14 at 23:43

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.