How to draw an endless hexagon spiral

I am trying to create a structure as shown in the screenshot below. Is there a way to build an Algorithm for this in JavaScript to get the X and Y coordinates of each red point in chronological order to create an infinite spiral depending on a specific amount?

Screenshot of how it should look and work

This code generates me a regular hexagon:

function hexagon(centerX, centerY) {
    var ctx = canvas.getContext('2d');
    var x = centerX + Math.cos(Math.PI * 2 / 6) * 50;
    var y = centerY + Math.sin(Math.PI * 2 / 6) * 50;

    ctx.beginPath();
    ctx.moveTo(x, y);

    for (var i = 1; i < 7; i++) {
        x = centerX + Math.cos(Math.PI * 2 / 6 * i) * 50;
        y = centerY + Math.sin(Math.PI * 2 / 6 * i) * 50;

        ctx.lineTo(x, y);
    }

    ctx.closePath();
    ctx.stroke();
}

      

And this is the cluster function so far:

function cluster(centerX, centerY, count) {
    var ctx = canvas.getContext('2d');

    for (var i = 0; i < count; i++) {
        if (i == 0) {
            var x = centerX;
            var y = centerY;
        } else {
            var x = centerX + Math.cos(-Math.PI / 2) * (100 * (Math.sqrt(3) / 2));
            var y = centerY + Math.sin(-Math.PI / 2) * (100 * (Math.sqrt(3) / 2));
        }

        hexagon(x, y);
    }
}

      

Thank!

+3


source to share


1 answer


Your cluster function could be like this:

function cluster(centerX, centerY, count) {
    var x = centerX,
        y = centerY,
        angle = Math.PI / 3,
        dist = Math.sin(angle) * 100,
        i = 1,
        side = 0;

    hexagon(x, y);
    count--;
    while (count > 0) {
        for (var t = 0; t < Math.floor((side+4)/6)+(side%6==0) && count; t++) {
            y = y - dist * Math.cos(side * angle);
            x = x - dist * Math.sin(side * angle);
            hexagon(x, y);
            count--;
        }
        side++;
    }
}

      



function hexagon(centerX, centerY) {
    var ctx = canvas.getContext('2d');
    var x = centerX + Math.cos(Math.PI * 2 / 6) * 50;
    var y = centerY + Math.sin(Math.PI * 2 / 6) * 50;

    ctx.beginPath();
    ctx.moveTo(x, y);

    for (var i = 1; i < 7; i++) {
        x = centerX + Math.cos(Math.PI * 2 / 6 * i) * 50;
        y = centerY + Math.sin(Math.PI * 2 / 6 * i) * 50;

        ctx.lineTo(x, y);
    }

    ctx.closePath();
    ctx.stroke();
}

function cluster(centerX, centerY, count) {
    var x = centerX,
        y = centerY,
        angle = Math.PI / 3,
        dist = Math.sin(angle) * 100,
        i = 1,
        side = 0;

    hexagon(x, y);
    count--;
    while (count > 0) {
        for (var t = 0; t < Math.floor((side+4)/6)+(side%6==0) && count; t++) {
            y = y - dist * Math.cos(side * angle);
            x = x - dist * Math.sin(side * angle);
            hexagon(x, y);
            count--;
        }
        side++;
    }
}

cluster(200,230,9);
      

<canvas id="canvas" width="400" height="400"></canvas>
      

Run codeHide result


+1


source







All Articles