Star programming!

18

2

The challenge

Write a program or function that prints stars! The program will receive two parameters by standar input: Size of the star and number of points of the star. A star can be any size between 0 and 10 and have 3, 4 or 5 points. The input parameters are separated by a comma, for example: 5,3 means a size 5 star with 3 points. The stars are composed by asterisks *

Consider the program will only receive valid inputs.

The prints must be correctly spaced so we can see pretty stars in the output!

Parameters explanations

Points

Is the number of arms counting from the central asterisk.

3 points stars:

       *
       *
       *
     *   *
    *     *

They are composed by a vertical arms and two diagonal arms. The separation between diagonal arms is 2*size-1

4 points stars

   *     *
    *   *
      *
    *   *
   *     *

They have the form of a X and composed by two diagonal lines. The separation between lines is 2*(size-1)+1

5 points stars

     *
     *
 * * * * *
    * *
   *   *

They are composed by a vertical line and a horizontal line. Each asterisk in the horizontal line is separated by one space. Also they have two diagonal lines, with a separation between themselves of 2*(size-2)+1

Size

Size is the number of asterisks that has each arm of the star counting from the central asterisk (inclusive).

Size 1 stars are composed by a single asterisk

*

Size 2 stars examples

3 points

       *
       *
     *   *

4 points stars

    *   *
      *
    *   *

5 points stars

     *
   * * *
    * *

You have examples of the size 3 stars in the Points parameter explanation.

Winning criteria

Shortest code wins. Code have to be checked with the following input parameters: 3,2 3,3 4,2 4,3 5,2 5,3

Bonus

-25% characters count for creating a size 6 pointed with the criteria explained before (6 arms and the size is the number of asterisks counting from the central asterisk). It can be any form you want while it respect the specifications. A example of a size 6 star with size 3:

      *
      *
  * * * * *
    * * *
   *  *  *

6 points stars must be checked with the following inputs 6,2 6,3.

Averroes

Posted 2012-08-28T21:46:38.273

Reputation: 3 771

1In your first example of a (5,3) star, should the two horizontal points each be one asterisk longer? – PhiNotPi – 2012-08-28T22:43:55.493

Yes, it is. Fixed, Thanks! – Averroes – 2012-08-29T06:15:22.793

3"My God... It's full of stars" – Drake Clarris – 2012-08-29T13:52:42.597

That should be the question title! – luser droog – 2013-04-07T06:53:02.353

Answers

36

Mathematica 80 76 67 chars

Some would question whether this is ASCII art, but I couldn't resist.

Graphics@Table[Text["*", r {Cos@t, Sin@t}], {t,0,2π,2π/#1}, {r,0,#2-1}]&

Usage (setting the font size at 24 to make the stars appear large.)

Graphics@Table[Text["*"~Style~24, r {Cos@t, Sin@t}], {t,0,2π,2π/#1}, {r,0,#2-1}] &[6,4]

six four


Output for the following cases:

{{3, 2}, {3, 3}, {4, 2}, {4, 3},

{5, 2}, {5, 3}, {6, 2}, {6, 3},

{7, 4}, {8, 3}, {9, 2}, {12, 4}}

framed stars


How it works

(a) The first star is at the origin of a coordinate space. Let's display it.

(b) Afterwards, we'll display a point at {1,0}.

(c) Then 5 points at once. We applied a pure function onto each pair of coordinates that follows it.

(d) Use Cos and Sin to determine the coordinates

(e) Coordinates work only on unit circle; 6 is the number of light beams.

(f) Draw radii from 0 to 4 unit.

options = Sequence[Axes -> True, ImageSize -> 225, BaseStyle -> 14];
a = Graphics[Text["*"~Style~{28, Blue}, {0, 0}], PlotLabel -> Style["a", 20], options];

b = Graphics[Text["*"~Style~{28, Blue}, {1, 0}], PlotLabel -> Style["b", 20], options];

c = Graphics[Text["*"~Style~{28, Blue}, {#1, #2}] & @@@ {{0, 0}, {1, 0}, {0, 1}, {-1, 0}, {0, -1}}, PlotLabel -> Style["c", 20], options];

d = Graphics[Text["*"~Style~{28, Blue}, {Cos@#, Sin@#}] & /@ {0, \[Pi]/3, 2 \[Pi]/3, \[Pi], 4 \[Pi]/3, 5 \[Pi]/3}, PlotLabel -> Style["d", 20], options];

e = Graphics@Table[Text["*"~Style~24, {Cos@t, Sin@t}], {t, 0, 2 \[Pi],  2 \[Pi]/#1}] &[6];

f = Graphics@Table[Text["*"~Style~24, r {Cos@t, Sin@t}], {t, 0, 2 \[Pi], 2 \[Pi]/#1}, {r, 0, #2 - 1}] &[6, 4];

GraphicsGrid[{{a, b, c}, {d, e, f}}, Dividers -> All]

explanation

DavidC

Posted 2012-08-28T21:46:38.273

Reputation: 24 524

+1 Very interesting! Could you explain how this works? – Rob – 2012-08-31T21:45:55.620

1Thank you for the explanation, it's starting to make more sense. I just wish Mathematica wasn't so "locked down" and mostly for educational purposes. Do you have any resources that link to a Mathematica IDE demo or examples beyond the ones that Wolfram provides? – Rob – 2012-09-01T17:25:59.093

Mathematica opened up considerably with its Computable Document Format, which allows for free distribution of applets for educational, non-commercial purposes. The source code to Mathematica is largely "locked down", however. As for resources, you may want to look at http:/demonstrations.wolfram.com and also http://mathematica.stackexchange.com/questions/18/where-can-i-find-examples-of-good-mathematica-programming-practice

– DavidC – 2012-09-01T17:47:40.207

7

Ruby, ASCII, 193 score 142 (189 chars - 25 % bonus)

def s r,d
f=->a,n{[n*Math.sin(a),n*Math.cos(a)]}
s=d*6
p=[]
s.times{|k|p<<" "*s}
c=s/2
p[c][c]=?*
r.times{|a|d.times{|l|x,y=f[6.28/r*a,d*l]
p[c+x.round][c+y.round]=?*}}
p.map{|j|puts j}
end

Test it online

I think it qualifies for the 6-rays star bonus.

      *     *     

       *  *       


   *  *  *  *  *  


        *  *      

      *     *     







     * *    

    * * *   

     * *    





      *           *     


        *       *       



          *   *         


*   *   *   *   *   *   *


          *   *         



        *       *       


      *           *   

defhlt

Posted 2012-08-28T21:46:38.273

Reputation: 1 717

3

Mathematica, 65 64

My take on David's method:

f@p_=Graphics@Array[Text["*",{Cos@#,Sin@#}&[2π/p[[1]]#]#2]&,p,0]

Use:

f @ {6,4}

enter image description here

Errors are produced because of the use of = rather than :=, and it isn't the proper way to make a definition, but it does work here.

If one were allowed to use dots instead of asterisks this could be written (52 chars):

f@p_:=Most@ListPolarPlot@Array[{2π/p[[1]]#,#2}&,p,0]

f @ {6, 4}

enter image description here

Mr.Wizard

Posted 2012-08-28T21:46:38.273

Reputation: 2 481

I like the idea of using layers of pure functions. – DavidC – 2013-04-06T16:17:36.253

@David Thanks. :-) – Mr.Wizard – 2013-04-07T14:29:08.653

@Averroes you should accept this answer. – Buffer Over Read – 2016-10-01T12:11:35.100

0

use subs qw /N W E S NW NE SE SW Circler Printer/;
($size,$points)=split(/\,/,$ARGV[0]);
my $arrsize = $size>$points ? $size : $points;
for $my (0...2*$arrsize-2) {
    $starArray[$my]=(); 
}
if($size == 3) {
    @armlist=('N','SW','SE');
}
elsif($size == 4) {
    @armlist=('NE','NW','SW','SE');
}
elsif($size == 5) {
    @armlist=('E','N','W','SW','SE');
}
elsif($size == 6) {
    @armlist=('E','N','W','SW','S','SE');
}
elsif($size == 7) {
    @armlist=('E','N','W','SW','S','SE','NE');
}
elsif($size == 8) {
    @armlist=('E','N','W','SW','S','SE','NE','NW');
}
Circler;
Printer;
sub Circler{
    for (@armlist) {
        &{$_};
    }
}
sub Printer{
    for $my1 (0...2*$arrsize-2) {
        for $my2 (0...2*$arrsize-2) {
            print "$starArray[$my1]->[$my2]"."\t"; 
        }
        print "\n\n";
    }
}
sub N {
    for $my (0...$points-1) {
        $starArray[$arrsize-1-$my]->[$arrsize-1]="*"; 
    }
}
sub E {
    for $my (0...$points-1) {
        $starArray[$arrsize-1]->[$arrsize-1+$my]="*"; 
    }
}
sub W {
    for $my (0...$points-1) {
        $starArray[$arrsize-1]->[$arrsize-1-$my]="*"; 
    }
}
sub S {
    for $my (0...$points-1) {
        $starArray[$arrsize-1+$my]->[$arrsize-1]="*"; 
    }
}
sub NW {
    for $my (0...$points-1) {
        $starArray[$arrsize-1-$my]->[$arrsize-1-$my]="*"; 
    }
}
sub NE {
    for $my (0...$points-1) {
        $starArray[$arrsize-1-$my]->[$arrsize-1+$my]="*"; 
    }
}
sub SE {
    for $my (0...$points-1) {
        $starArray[$arrsize-1+$my]->[$arrsize-1+$my]="*"; 
    }
}
sub SW {
    for $my (0...$points-1) {
        $starArray[$arrsize-1+$my]->[$arrsize-1-$my]="*"; 
    }
}
__END__;

beginnerProg

Posted 2012-08-28T21:46:38.273

Reputation: 51

5What is that? Perl? – luser droog – 2013-04-07T06:54:53.470

7Can't be, I've never seen perl code that long before – jamylak – 2013-04-08T10:03:14.577