Your challenge is generate a 5 pointed star like below. The star doesn't need to be filled. Shortest code wins.
-
13\$\begingroup\$ Your image shows a Five-pointed-star, a pentagram has the connecting lines. \$\endgroup\$– Johannes KuhnDec 2, 2013 at 9:06
-
2\$\begingroup\$ @Iszi Looks like shortest question wins. \$\endgroup\$– Johannes KuhnDec 2, 2013 at 18:05
-
2\$\begingroup\$ wingdings, 1: « (character 0xAB), or unicode: Pentagram: ⛤ U+26E4 ⛤ New in Unicode 6 \$\endgroup\$– SeanCDec 2, 2013 at 19:40
-
2\$\begingroup\$ @SeanC They're standard. That means you don't have to specifically state they are disallowed. \$\endgroup\$– mbomb007Mar 29, 2017 at 15:32
-
2\$\begingroup\$ So? Meta discussions are often applied retroactively. The entire site is held to the same standards and cleaned up after the fact, rather than only new questions having to abide by new rules. This is because old questions can always be answered later. \$\endgroup\$– mbomb007Mar 30, 2017 at 16:35
28 Answers
PHP, 1 character
Although very short it will probably not become the accepted answer.
☆
(this is U+2606). May work also in other languages (untested).
-
3\$\begingroup\$ Also, this answer is invalid, because the challenge is [graphical-output], not [ascii-art]. \$\endgroup\$– mbomb007Mar 29, 2017 at 15:45
-
2\$\begingroup\$ @mbomb007 Character glyphs are hardly ASCII art. \$\endgroup\$– DennisMar 29, 2017 at 16:00
-
2\$\begingroup\$ So it's unicode. Either way, it's not valid. \$\endgroup\$– mbomb007Mar 29, 2017 at 16:03
-
2\$\begingroup\$ @mbomb007 I'm not nitpicking because you said ASCII. ASCII art uses several characters to resemble a shape; this is different. Provided that a proper font is used, this prints the desired output in a terminal or a web browser, so I fail to see why it would be invalid. \$\endgroup\$– DennisMar 29, 2017 at 16:49
-
2\$\begingroup\$ Because that's not in the spirit of the challenge. If every answer used
print"☆"
or some variant because it's the shortest, this wouldn't be a challenge. \$\endgroup\$– mbomb007Mar 29, 2017 at 17:36
VBA PowerPoint, 92 bytes
Sub s()
ActivePresentation.Slides(1).Shapes.AddShape msoShape5pointStar, 0, 0, 99, 99
End Sub
Because everything looks better in PowerPoint:
-
3\$\begingroup\$ If you convert this to an immediates window function, convert
msoShape5pointStar
to its constant value (92
) and remove the whitespace between the commas and values (ActivePresentation.Slides(1).Shapes.AddShape 92,0,0,99,99
) you can condense the byte count to57
bytes \$\endgroup\$ Mar 28, 2017 at 21:55
Mathematica, 19 18 bytes
This is easy, using CirculantGraph
.
I drew a pentagram as defined in the link the OP used.
5~CirculantGraph~2
A slightly longer way, but equally straightforward, is to use the curated "Lamina" entity. This returns a filled pentagram.
"FilledPentagram"~LaminaData~"Image"
or
EntityValue[Entity["Lamina","FilledPentagram"],"Image"]
-
\$\begingroup\$ Is the space actually necessary? I don't speak Mathematica, but I know many other languages would be forgiving of omitting the space in a circumstance such as this. \$\endgroup\$– IsziDec 2, 2013 at 17:18
-
\$\begingroup\$ No, the space is not necessary. That's why I didn't count the space. Just about the only time space matters is inside a string and in the case of implicit multiplication (e.g. 3 5 == 15). "==" is equals in Mathematica. \$\endgroup\$– DavidCDec 2, 2013 at 17:28
-
\$\begingroup\$ One char less
5~CirculantGraph~2
\$\endgroup\$ Dec 3, 2013 at 16:58
Python 3, 137 bytes
Polar plot.
import pylab as P,numpy as N
S=N.sin
p=N.pi
a=.9*p
d=8e-4*p
A=lambda t:N.arange(0,t,d)
P.polar(A(4*p),list(S(a)/S(a-A(.8*p)))*5)
P.show()
-
4\$\begingroup\$
import pylab as P,math as M;α=[4*M.pi*i/5for i in range(6)];A=[1]*6;P.polar(α,A);P.show()
(89 chars) paints similar figure \$\endgroup\$– AMKDec 6, 2013 at 21:00 -
\$\begingroup\$ @AMK nice, I see the trick. You could write it as a new answer. \$\endgroup\$– RayDec 11, 2013 at 1:27
-
\$\begingroup\$ Non-ASCII characters are not a single byte. Edited to make ASCII-only in order for the byte count to be correct. \$\endgroup\$– mbomb007Mar 29, 2017 at 15:43
Small Basic, 50 bytes
For i=1To 5
Turtle.Move(9)
Turtle.Turn(144)
EndFor
A verbose language, but it has built in turtle functionality. Slowly draws a very, very tiny pentagram.
Scratch, 4-6 blocks (52-79 scratchblocks code)
This produces this
If anyone's interested, here's the project.
EDIT: To golf it, remove the pen down. You could remove the then flag clicked, too but it would only run if you clicked it. To golf the scratch blocks version, change "100" to "1", although you get a microscopic pentagram.
Nongolfed scratchblocks:
when gf clicked
clear
pen down
repeat (5
move (100) steps
turn cw (144) degrees
-
\$\begingroup\$ If I was to be pedantic, I'd count the bytes of each block. Interesting choice, though. \$\endgroup\$– WolfishOct 20, 2015 at 9:25
-
\$\begingroup\$ Consensus on meta is that Scratch answers be scored by the length of the corresponding
scratchblocks
code. \$\endgroup\$ Oct 24, 2015 at 12:57 -
\$\begingroup\$ @ev3commander: I wouldn't consider that question to be proper competitive code golf \$\endgroup\$ Oct 26, 2015 at 0:44
HTML & CSS: 270 269 bytes
<p id=a/><p id=b></p><p id=c></p><p id=d></p><p id=e></p><style>p{position:absolute;width:300px;border-top:solid #000;margin:100px;transform-origin:50% 1666%}#b{transform:rotate(72deg)}#c{transform:rotate(144deg)}#d{transform:rotate(216deg)}#e{transform:rotate(288deg)}
Sample run:
- http://jsfiddle.net/fm6Hu/2/ (Explorer, Firefox, Opera)
- http://jsfiddle.net/fm6Hu/3/ (Chrome, Opera)
HTML & CSS: 139 bytes
Impressing rewrite by Neil.
div{height:20px;width:300px;border-bottom:solid #000;margin:100px;transform-origin:33% 5%;transform:rotate(72deg)}
<div><div><div><div><div>
(Since first posted, all desktope browsers dropped vendor prefixes, so now one CSS code fits all.)
-
1\$\begingroup\$ I just get a single horizontal line... Chrome 31.0.1650.57 on Win7 \$\endgroup\$– DoorknobDec 2, 2013 at 21:50
-
2\$\begingroup\$ never mind, adding the
-webkit-
vendor prefix totransform-origin
andtransform
fixed it. updated fiddle \$\endgroup\$– DoorknobDec 2, 2013 at 21:53 -
-
\$\begingroup\$ @lszi, not me, its Google Prettify. I just give it the language hint by adding a special HTML comment in front of the code block. See Syntax highlighting for code. \$\endgroup\$ Dec 3, 2013 at 7:37
-
2\$\begingroup\$ You can save a bunch of bytes by nesting your tags. I came up with
<div><div><div><div><div>
anddiv{height:20px;width:300px;border-bottom:solid #000;margin:100px;transform-origin:33% 5%;transform:rotate(72deg)}
just through trial and error. \$\endgroup\$– NeilApr 4, 2017 at 7:51
HTML5, 37 bytes
<svg><path d="M4,0 7,8 0,3 8,3 1,8"/>
Tcl/Tk, 57 bytes
pack [canvas .c]
.c cr po 72 0 114 130 3 49 140 49 29 130
-
\$\begingroup\$ This actually draws a pentagram, but you don't see the connecting lines because it is filled with black. \$\endgroup\$ Dec 2, 2013 at 9:42
-
1\$\begingroup\$ I feel like by that logic you could paint the whole canvas black, and say: "This paints the pentagram, but it's the same colour as the background". So long as we only care about the output in determining correctness, these are indistinguishable(painting the whole canvas vs. painting black pentagram on a black canvas). \$\endgroup\$– CruncherDec 2, 2013 at 13:49
-
1\$\begingroup\$ @Cruncher
the star don't need filled
-> but I can fill it. And the question asked for a pentagram, so I draw a filled pentagram that looks like the one in the question, only filled. \$\endgroup\$ Dec 2, 2013 at 14:36
TurtleGolf, 70 24 22 bytes
Note: This is non-competing, as this language was made after this challenge was posted (though specifically for this challenge).
0;
aa*^aa*4b*+>1+:5=?;
Explaination:
0; - Initialize the loop counter and go to the next line.
aa* - Get the value 100
^ - Move forward by 100px
aa*4b*+ - Get the value 144
> - Rotate 144 degrees to the right
1+ - Increment the loop counter by 1
: - Duplicate it cause we need a copy of it.
5= - Check if the loop counter equals 5, push 1 if it is.
?; - If the top of the stack equals 1, terminate the line.
Old, loopless version.
aa*^aa*4a*+4+>aa*^aa*4a*+4+>aa*^aa*4a*+4+>aa*^aa*4a*+4+>aa*^aa*4a*+4+>
I didn't add loops yet, so the code is kind of repetitive right now.
Explaination
aa*^ Move aa*(100) pixels forward
aa*4a*+a+ Get the value 144
> Turn 144 pixels to the right
EDIT: Shaved off lots of bytes.
EDIT2: Shaved off two more bytes.
-
-
1\$\begingroup\$ @CᴏɴᴏʀO'Bʀɪᴇɴ I just added them :) Every single line is now a loop that executes until a ; is reached. \$\endgroup\$ Oct 20, 2015 at 6:58
-
\$\begingroup\$ What is the behaviour when the stack is not one? It just loops back to the beginning of the line? \$\endgroup\$ Oct 20, 2015 at 10:39
-
1\$\begingroup\$ @CᴏɴᴏʀO'Bʀɪᴇɴ If the stack is exactly zero when you pass a zero, it skips the next instruction. It loops back to the beginning if there isnt any instruction to skip and skips the first instruction instead. \$\endgroup\$ Oct 20, 2015 at 11:08
-
\$\begingroup\$ @CᴏɴᴏʀO'Bʀɪᴇɴ When you pass a ?, not when you pass a zero. Sorry. \$\endgroup\$ Oct 20, 2015 at 11:26
-
\$\begingroup\$ What would happen if you used a smaller number like 9 instead of 99? \$\endgroup\$– CruncherDec 2, 2013 at 13:51
-
\$\begingroup\$ @Cruncher The star would be off-screen and you would not be able to see it. \$\endgroup\$– DoorknobDec 2, 2013 at 13:57
-
\$\begingroup\$ Interesting concept. I should learn it. \$\endgroup\$ Dec 2, 2013 at 18:08
-
\$\begingroup\$ Is this valid? It's unclear what the angles have to be, but it doesn't match the image in the question. \$\endgroup\$– mbomb007Mar 29, 2017 at 16:05
[C (msvc)], 88 bytes
#include<Windows.h>
main(){int p[]={50,1,20,91,97,35,2,35,79,91};Polygon(GetDC(0),p,5);}
Desmos, 46 characters
r=\sec\left(.4\arccos\left(\cos2.5\theta\right)\right)
Here is a longer version that allows you to choose some n
:
r=\sec\left(\frac{2}{n}\arccos\left(\cos\frac{n\theta}{2}\right)\right)
-
\$\begingroup\$ Does this work the same way as the octave answer? \$\endgroup\$ Mar 28, 2017 at 22:56
-
\$\begingroup\$ @GenericDisplayName Frankly, I have no idea. Probably, yeah, they both use polar things \$\endgroup\$ Mar 28, 2017 at 23:03
PostScript, 27 bytes
$ hexdump -C pentagram_binary.ps
00000000 39 20 39 92 ad 30 20 39 92 6b 36 7b 30 20 39 92 |9 9..0 9.k6{0 9.|
00000010 63 31 34 34 92 88 7d 92 83 92 a7 |c144..}....|
0000001b
Output:
Un-golfed ASCII version:
9 9 translate
0 9 moveto
6{
0 9 lineto
144 rotate
}repeat
stroke
Python, 47 bytes
from turtle import*
for i in[144]*5:fd(i);rt(i)
The following (42 bytes) also works (Python 2 only), but Trinket doesn't allow exec
, so you can't run it online. Also, the result is much smaller.
from turtle import*
exec'fd(9);rt(144);'*5
Google Blockly, 3 blocks
In response to the Scratch answer
Blockly turtle doesn't need "so much" boilerplate code.
This "code" can be ran here and produces this pentagram
Five pointed star, 5 blocks
The five pointed star can be tested here
-
\$\begingroup\$ Well, I didn't need "so much" code either, if you remove the flag block then it runs if you click it. Also, after the initialization, you wouldn't need "pen down", but whatever. \$\endgroup\$– user46167Oct 20, 2015 at 20:07
Shell, 60 bytes
(Coordinates shamelessly stolen from user1455003's SVG answer. For artistic merits upvote his answer.)
convert -size 9x9 xc: -draw 'polygon 4,0 7,8 0,3 8,3 1,8' x:
Sample output:
-
1
-
\$\begingroup\$ @freekvd it's just a problem of aspect ratio. See edit \$\endgroup\$ Oct 20, 2015 at 12:30
Swift 3, 394 370 bytes
import UIKit
UIGraphicsBeginImageContextWithOptions(CGSize(width:2,height:2),false,100)
let c1=0.3,c2=0.8,s1=1.0,s2=0.6,o=(0.0,1.0),p=[o,(s2,-c2),(-s1,c1),(s1,c1),(-s2,-c2),o].map{CGPoint( x:$0.0,y:$0.1)},c=UIGraphicsGetCurrentContext()!
c.translateBy(x:1,y:1)
c.setLineWidth(1/100)
c.move(to:p[0])
p.forEach{c.addLine(to:$0)}
c.fillPath()
UIImage(cgImage:c.makeImage()!)
Octave, 24 bytes
polar(t=[1:.8:5]*pi,~~t)
Output:
Or, for 36 bytes you can have just the outline:
polar(t=[0:.2:2]*pi,1-mod(t,pi/2.5))
-
\$\begingroup\$ An alternative without
polar
(but same length) would beplot(e.^(i*pi*[0:.8:4]))
\$\endgroup\$– flawrOct 19, 2015 at 21:03 -
\$\begingroup\$ @flawr Yes it would! I was actually working on the second plot first and couldn't figure out a good way to generate the
rho
vector, so the first plot here is really just a chopped down version of the second. \$\endgroup\$– beakerOct 19, 2015 at 21:07
Tikz, 126 120 125 bytes
5 bytes saved thanks to Julian Wolf
\documentclass[tikz]{standalone}\usetikzlibrary{shapes}\begin{document}\tikz{\node[draw=red,star]{};}\end{document}
This draws a red star shaped node at (0,0)
.
-
1\$\begingroup\$ I think
\node
can be substituted for\draw node
to save a few bytes \$\endgroup\$ Apr 18, 2017 at 23:04 -
\$\begingroup\$ Make it 110:
\documentclass[tikz]{standalone}\usetikzlibrary{shapes}\def~{document}\begin~\tikz\node[draw=red,star]{};\end~
Nice approach! \$\endgroup\$– Joe85ACJun 2, 2020 at 13:54
Excel VBA, 37 35 Bytes
Anonymous VBE immediates window function that takes no input and outputs a five pointed star to the top left of the Sheet1
(default sheet) object
Sheet1.Shapes.AddShape 92,0,0,99,99
Sample Output
JavaFX / FXML, 138 bytes
<?import javafx.scene.layout.*?><?import javafx.scene.shape.*?><Pane><children><SVGPath content="M4,0 7,8 0,3 8,3 1,8"/></children></Pane>
I haven't tested it yet, though
<?import javafx.scene.layout.*?>
<?import javafx.scene.shape.*?>
<Pane>
<children>
<SVGPath content="M4,0 7,8 0,3 8,3 1,8" stroke="BLACK"/>
</children>
</Pane>
Python + Pylab, 48-byte copout answer
from pylab import*
plot(0,0,marker=(5,1))
show()
Plots a single point using a five-pointed star as a marker.
TI-83/84 Basic, 32 bytes
Line(0,1,1,1:Line(0,0,.5,1.5:Line(1,0,.5,1.5
Note I haven't actually tested this out but I'm assuming it's going to show up as a low resolution, distorted star-shaped object.
- Line( command is one byte
-
\$\begingroup\$ You don't have to count the three instances of
:
, so your character count would be 42. \$\endgroup\$ Oct 19, 2015 at 13:59 -
1\$\begingroup\$ Thanks for updating me on this @CᴏɴᴏʀO'Bʀɪᴇɴ since then we have agreed that TI-Basic is counted in bytes not chars anyway. \$\endgroup\$– TimtechOct 20, 2015 at 12:34
-
2\$\begingroup\$ -1; this prints a barely-visible A-shaped figure, not a star. \$\endgroup\$ Oct 26, 2015 at 1:35