20
\$\begingroup\$

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.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ In your first example of a (5,3) star, should the two horizontal points each be one asterisk longer? \$\endgroup\$
    – PhiNotPi
    Aug 28, 2012 at 22:43
  • 3
    \$\begingroup\$ "My God... It's full of stars" \$\endgroup\$ Aug 29, 2012 at 13:52
  • \$\begingroup\$ That should be the question title! \$\endgroup\$ Apr 7, 2013 at 6:53

4 Answers 4

37
\$\begingroup\$

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

\$\endgroup\$
3
  • \$\begingroup\$ +1 Very interesting! Could you explain how this works? \$\endgroup\$
    – anon
    Aug 31, 2012 at 21:45
  • 1
    \$\begingroup\$ Thank 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? \$\endgroup\$
    – anon
    Sep 1, 2012 at 17:25
  • \$\begingroup\$ 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 mathematica.stackexchange.com/questions/18/… \$\endgroup\$
    – DavidC
    Sep 1, 2012 at 17:47
7
\$\begingroup\$

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.

      *     *     
                  
       *  *       
                  
                  
   *  *  *  *  *  
                  
                  
        *  *      
                  
      *     *     
                  
                  
                  
            
            
            
            
     * *    
            
    * * *   
            
     * *    
            
            
            
                        
                        
      *           *     
                        
                        
        *       *       
                        
                        
                        
          *   *         
                        
                        
*   *   *   *   *   *   *
                        
                        
          *   *         
                        
                        
                        
        *       *       
                        
                        
      *           *   
                  
\$\endgroup\$
3
\$\begingroup\$

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

\$\endgroup\$
2
  • \$\begingroup\$ I like the idea of using layers of pure functions. \$\endgroup\$
    – DavidC
    Apr 6, 2013 at 16:17
  • \$\begingroup\$ @Averroes you should accept this answer. \$\endgroup\$ Oct 1, 2016 at 12:11
1
\$\begingroup\$
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__;
\$\endgroup\$
2
  • 5
    \$\begingroup\$ What is that? Perl? \$\endgroup\$ Apr 7, 2013 at 6:54
  • 7
    \$\begingroup\$ Can't be, I've never seen perl code that long before \$\endgroup\$
    – jamylak
    Apr 8, 2013 at 10:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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