class HexFlower {
constructor(x, y, radius, rotation = 0) {
this.x = x;
this.y = y;
this.radius = radius;
this.rotation = rotation;
this.cellRadius = radius / Math.sqrt(7);
this.cellHeight = 2 * this.cellRadius * Math.sqrt(3) / 2;
const directions = [
[Math.cos(Math.PI * 1/6 - rotation), Math.sin(Math.PI * 1/6 - rotation)],
[Math.cos(Math.PI * 3/6 - rotation), Math.sin(Math.PI * 3/6 - rotation)],
[Math.cos(Math.PI * 5/6 - rotation), Math.sin(Math.PI * 5/6 - rotation)],
[Math.cos(Math.PI * 7/6 - rotation), Math.sin(Math.PI * 7/6 - rotation)],
[Math.cos(Math.PI * 9/6 - rotation), Math.sin(Math.PI * 9/6 - rotation)],
[Math.cos(Math.PI * 11/6 - rotation), Math.sin(Math.PI * 11/6 - rotation)]
];
this.petals = directions.map(
([x1, y1]) => new Hex(x + this.cellHeight * x1, y + this.cellHeight * y1, this.cellRadius, rotation));
this.center = new Hex(x, y, this.cellRadius, rotation);
}
render(svg, petalStroke, centerStroke = petalStroke) {
for (const petal of this.petals) {
petal.render(svg, petalStroke);
}
this.center.render(svg, centerStroke);
}
renderFractal(renderer, colors, level) {
if (level <= 0) {
return;
}
renderer.render(this, colors.getPetalColor(level), colors.getCenterColor(level));
const flowerRotation = -Math.asin(Math.sqrt(3/7)/2);
const flowers = [this.center].concat(this.petals).map(
h => new HexFlower(h.x, h.y, this.cellRadius, this.rotation + flowerRotation));
for (const flower of flowers) {
flower.renderFractal(renderer, colors, level - 1);
}
}
}