我正在使用Isomer JS库创建3d网格,并且需要帮助来确定增量循环的逻辑。这是Codepen:

https://codepen.io/anon/pen/wqwrzL

Javascript:

function draw() {
    var iso = new Isomer(document.getElementById("grid"));

    var Shape = Isomer.Shape;
    var Point = Isomer.Point;
    var Path = Isomer.Path;
    var Color = Isomer.Color;
    var cube = Shape.Prism(Point.ORIGIN);
    var white = new Color(255, 255, 255, 0.4);

    makeGrid(8, 8, 0, new Color(100, 100, 100, 0.6));

    for (x = 0; x < 8; x++) {
        iso.add(Shape.Prism(new Point(x, 0, 0), 1, 1, .5), white);
    }

    // GridMaker
    function makeGrid(xSize, ySize, zHeight, gridColor) {
        for (x = 0; x < xSize + 1; x++) {
            iso.add(new Path([
            new Point(x, 0, zHeight),
            new Point(x, xSize, zHeight),
            new Point(x, 0, zHeight)
            ]), gridColor);
        }
        for (y = 0; y < ySize + 1; y++) {
            iso.add(new Path([
            new Point(0, y, zHeight),
            new Point(ySize, y, zHeight),
            new Point(0, y, zHeight)
            ]), gridColor);
        }
    }
}


该代码段创建了实体块:

for (x = 0; x < 8; x++) {
            iso.add(Shape.Prism(new Point(x, 0, 0), 1, 1, .5), white);
        }


new Point()值是有序的:x,y,z。在第8次迭代之后,我需要将y值增加1,这将开始在新行上放置块。这也应该发生8次,有效地填充网格。

最佳答案

function draw() {
    var iso = new Isomer(document.getElementById("grid"));

    var Shape = Isomer.Shape;
    var Point = Isomer.Point;
    var Path = Isomer.Path;
    var Color = Isomer.Color;
    var cube = Shape.Prism(Point.ORIGIN);
    var white = new Color(255, 255, 255, 0.4);

    makeGrid(8, 8, 0, new Color(100, 100, 100, 0.6));

    for (x = 0; x < 8; x++) {
      for (y = 0; y < 8; y++) {
        iso.add(Shape.Prism(new Point(x, y, 0), 1, 1, .5), white);
      }
    }

    // GridMaker
    function makeGrid(xSize, ySize, zHeight, gridColor) {
        for (x = 0; x < xSize + 1; x++) {
            iso.add(new Path([
            new Point(x, 0, zHeight),
            new Point(x, xSize, zHeight),
            new Point(x, 0, zHeight)
            ]), gridColor);
        }
        for (y = 0; y < ySize + 1; y++) {
            iso.add(new Path([
            new Point(0, y, zHeight),
            new Point(ySize, y, zHeight),
            new Point(0, y, zHeight)
            ]), gridColor);
        }
    }
}

关于javascript - JavaScript增量循环,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45250503/

10-11 14:05