| Bytes | Lang | Time | Link |
|---|---|---|---|
| 399 | C++ using SFML | 230215T010018Z | HatsuPoi |
| 152 | JavaScript ES | 230214T010623Z | Neil |
| 222 | Wolfram Language Mathematica | 230214T014116Z | ZaMoC |
| 119 | Wolfram Language Mathematica | 230214T021740Z | alephalp |
C++ using SFML : 722 703 683 527 411 399 bytes
-19 bytes thanks to NoLongerBreathedIn, using __builtin_popcount removes needing a bitsum function. Might work only for GCC.
-20 bytes, I used the using keyword for namespaces and components, and stopped using .f for magic numbers
-156 bytes thanks to pan
-116 bytes again thanks to pan
-12 bytes thanks to ceilingcat and pan
pan's proposed version approximates the value of cos(210) to around -.86, which will produce a slightly different graphic output, but very close to what's required by the challenge. This makes the cmath include useless. He also put the whole code into one function, and used a number of golfy techniques to reduce more the byte count, which makes the result very nice. His second optimization renders the use of vector useless, making an include and object usage useless
pan's code :
#include<SFML/Graphics.hpp>
#define B+__builtin_popcount(
void v(int n){sf::CircleShape h(4,6);for(sf::RenderWindow w({1200,1000},"");w.isOpen();w.display()){for(sf::Event e;w.pollEvent(e);)e.type?h.setOrigin(-600,-650):w.close();w.clear();for(int x=0,y,z=1<<n;x<z;)for(y=x++-z;y++;)h.setPosition(4.3*(2*y+z-x),2.5*(2+z-3*x)),h.setFillColor(sf::Color(B-x)B-y)B z+y-x)-n&1?65535:-65281)),w.draw(h);}}
SFML is a library that helps with the development of graphical / audio / network applications.
Previous 683 bytes answer (with the "exact" value or cos(210°)) :
#include <cmath>
#include <vector>
#include <SFML/Graphics.hpp>
#define B(c) __builtin_popcount(c)
using namespace sf;struct t{double x,y;bool b;};using V=std::vector<t>;V d(int n){double r=5;int x,y,z,w=pow(2,n);V o;for(x=0;x<w;++x)for(y=0;y<w-x;++y){z=w-1-x-y;o.push_back({-cos(7*M_PI/6)*(r*y-r*z),r*x-(r*y+r*z)/2,(B(x)+B(y)+B(z))%2==n%2});}return o;}int v(int n){RenderWindow w(VideoMode(1200,1000),"");CircleShape h(4,6);;auto v=d(n);while(w.isOpen()){Event event;while(w.pollEvent(event))if(event.type==Event::Closed)w.close();w.clear();for(auto&a:v){h.setPosition(a.x,-a.y);h.setOrigin(-600,-650);h.setFillColor(a.b?Color::Yellow:Color::Blue);w.draw(h);}w.display();}return 0;}
Function to call in the main is v(int) with the parameter being n, the number n
int main() {
return v(6);
}
Compile with : g++ source.cpp -lsfml-graphics -lsfml-window -lsfml-system
Run with : ./a.out
Due to the screen size and the fact that hexagons have to be visible, the size of a n=10 triangle can't be seen fully in a 'normal' sized screen, triangle is fully visible in the screen at n<=7. Size of hexagon and space between them can be configured in the d(int) function with variable r and v(int) function by modifying the first parameter of the constructor of the h variable (the CircleShape object), that value being the radius of the circle. Be free to modify those parameters to draw bigger hexagons on the screens, r should be slightly bigger that the circle's radius
Here is an image of the result for n=6
JavaScript (ES76), 191 187 180 174 172 168 152 bytes
f=
n=>eval('s="<center style=line-height:.7>";for(i=-1;++i<1<<n;s+="<br>")for(j=i+1;j--;)s+=`<font color=#f${(g=n=>n?9-g(n&n-1):9)(i^j^i-j)}0>⬢</font>`')
<input type=number min=0 max=10 onchange=o.innerHTML=f(+this.value)><div id=o style=font-size:5px>
Output format is HTML. Snippet limited to n=10 because it triggers my browser's slow script warning if you go any higher. (But the output is too big for my screen anyway.) Edit: Saved 4* 11 bytes by simplifying the parity calculation. Saved 6 8 23 bytes thanks to @Ausername. Saved 4* 5 bytes thanks to @EzioMercer.
*crossed out 4 is still regular 4
Wolfram Language (Mathematica), 222 bytes
n=6;c=Graphics[{#,Rotate[Polygon@CirclePoints@6,30Degree]},ImageSize->9]&;Column[Row/@TakeList[(Tr@DigitCount[#,2,1]~Mod~2&/@Reverse@Select[Range[0,h=2^n-1]~Tuples~3,Tr@#==h&])/.{1->c@Red,0->c@Blue},Range[2^n]],Center,.01]
Wolfram Language (Mathematica), 119 bytes
-1 byte thanks to @att.
Graphics@Table[{Hue[Tr[ThueMorse@{x,y,s-x-y}+#+1]/2],RegularPolygon[{2x+y,√3y},{1,Pi/2},6]},{x,0,s=2^#-1},{y,0,s-x}]&


