53
11
Today, we're going to make an ASCII hexagon. You must write a program or function that takes a positive integer n, and outputs a hexagon grid of size n, made up of asterisks. For example, a hexagon of size 2 looks like this:
* *
* * *
* *
While a hexagon of size 3 looks like this:
* * *
* * * *
* * * * *
* * * *
* * *
You may use any of the default input and output methods, for example STDIO/STDOUT, function arguments and return values or reading/writing a file.
You may assume that input is always valid, so if it's not a positive integer, your program may do whatever you want. You do however have to handle the special case of a size 1 hexagon, which happens to be a single asterisk:
*
Leading and trailing whitespace is allowed as long as the output is visually the same.
Examples:
1:
*
2:
* *
* * *
* *
3:
* * *
* * * *
* * * * *
* * * *
* * *
4:
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
5:
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
6:
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * *
* * * * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
12:
* * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * * *
As usual, this is code-golf, so standard loopholes apply, and you should try to write the shortest possible program measured in bytes. Of course, some languages are inherently shorter or longer than others, so remember that the goal is not necessarily to have the shortest overall byte count, but to beat submissions in the same or similar languages.
May the best golfer win!
15Why do we even have a hexagonal-grid tag? – Pavel – 2016-12-26T07:25:54.747
13Also, someone needs to write a hexagony solution. – Pavel – 2016-12-26T07:40:41.423
If anyone wants to go for the bounty, you can probably reuse the output loop of my Hexagony answer over here.
– Martin Ender – 2017-01-19T09:01:08.8876
"Could you make me a hexagon please?" - sure, here you go: http://i.imgur.com/1emYIia.png
– aditsu quit because SE is EVIL – 2017-01-28T18:45:29.660@Pavel because a lot of operations on a hexagonal grid are distinct from on the more standard square grid, and portable between solutions to different problems. Such operations as coordinate manipulation, rotation, output layout, etc. – Sparr – 2018-06-28T05:25:12.307