Generating triangular/hexagonal coordinates (xyz)

后端 未结 5 1334
时光取名叫无心
时光取名叫无心 2020-12-02 07:50

I\'m trying to come up with an iterative function that generates xyz coordinates for a hexagonal grid. With a starting hex position (say 0,0,0 for simplicity), I want to cal

5条回答
  •  忘掉有多难
    2020-12-02 08:19

    Not only is x + y + z = 0, but the absolute values of x, y and z are equal to twice the radius of the ring. This should be sufficient to identify every hexagon on each successive ring:

    var radius = 4;
    for(var i = 0; i < radius; i++)
    {
        for(var j = -i; j <= i; j++)
        for(var k = -i; k <= i; k++)
        for(var l = -i; l <= i; l++)
            if(Math.abs(j) + Math.abs(k) + Math.abs(l) == i*2 && j + k + l == 0)
                console.log(j + "," + k + "," + l);
        console.log("");
    }

提交回复
热议问题