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
$
, so it'll be\$n\$
\$\endgroup\$