Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

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

share|improve this question
2  
Also, you might want to specify a shamrock instead of a clover! –  David Carraher Mar 18 at 3:33
    
Changed the title. Commenting from the S4 here, and signal is awful. I'll find a shamrock later! :) –  George H Mar 18 at 16:02

3 Answers 3

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

share|improve this answer
    
Your Abs[] has no closing bracket –  belisarius Mar 18 at 23:08
    
@belisarius Where do I use Abs[]? I only see Abs@ (which is prefix notation for single-argument functions) –  Martin Büttner Mar 18 at 23:09
    
@belisarius Oh in the ungolfed version. Thanks, fixed! –  Martin Büttner Mar 18 at 23:41
    
let's see a 3d version too? –  Michael Stern Mar 21 at 13:21
    
@MichaelStern tempting :) ... but maybe some other time... I just spent my entire free time for today on another answer ^^ –  Martin Büttner Mar 21 at 13:34

Wolfram Alpha, 13 12 18

polar sin ax if a=[[your number here]]

If your number is even, you have to halve it.

test it here

share|improve this answer
    
Haha, this is actually what I started with (in Mathematica though), but then decided that I'd rather deform the leaves a bit more. +1 for it being so concise on WA. –  Martin Büttner Mar 17 at 23:58
    
I think it has a bug...I said I want 2 leaves and it drew four. polar sin 2x...what the heck? ;-) –  Jonathan Van Matre Mar 18 at 0:03
1  
Just so you know, any even number clover draws twice as many leaves at it should –  davidsbro Mar 18 at 0:04
4  
It looks better if you put 3.01x. If you run that to infinity it would give you a solid circle, but Wolfram picks a couple of different ranges for x and gives you some different filled out clovers. –  steveverrill Mar 18 at 0:13
1  
According to the question, user input is required. Quoting: "Each leaf has to be symmetrical, and the user has to input the number of leaves that they want on the clover" –  Ismael Miguel Mar 18 at 0:59

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;
}
share|improve this answer
1  
Best use of variables names l and r! –  Martin Büttner Mar 18 at 23:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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