Actionscript 3 六边形网格绘图问题

Actionscript 3 六边形网格绘图问题,actionscript-3,drawing,hexagonal-tiles,Actionscript 3,Drawing,Hexagonal Tiles,我似乎在绘制正确的十六进制网格时遇到了一些问题: 正如你所看到的,六边形只是稍微有点不对齐,尽管我相信我的数学是正确的(其中一些是经过验证的) 我的画法是从左上角的六边形开始(第一行的第一个瓷砖),然后画那行瓷砖。然后,对于下一行,存在负X偏移和正Y偏移,以此类推,直到到达中间行,此时X偏移增加到0: private function drawHexGrid(inputGraphics:Graphics, inputPos:Point, inputGrid:HexGrid, inputTil

我似乎在绘制正确的十六进制网格时遇到了一些问题:

正如你所看到的,六边形只是稍微有点不对齐,尽管我相信我的数学是正确的(其中一些是经过验证的)

我的画法是从左上角的六边形开始(第一行的第一个瓷砖),然后画那行瓷砖。然后,对于下一行,存在负X偏移和正Y偏移,以此类推,直到到达中间行,此时X偏移增加到0:

private function drawHexGrid(inputGraphics:Graphics, inputPos:Point, inputGrid:HexGrid, inputTileSize:int):void {

    var rootThree:Number = Math.sqrt(3); // seems like this will be used a lot

    // total number of rows and also size of largest row (in tiles)
    var totalRows:int = (2 * inputGrid.size) - 1;

    // the other useful dimension of a hex tile
    var triSize:Number = rootThree * 0.5 * inputTileSize;

    var topLeft:Point = new Point(-(inputGrid.size - 1) * triSize, -(1.5 * (inputGrid.size - 1) * inputTileSize));
    topLeft.x += inputPos.x;
    topLeft.y += inputPos.y;
    var currentPos:Point = topLeft.clone();

    // step size between each tile and row
    var xStep:Number = rootThree * inputTileSize;
    var yStep:Number = triSize * rootThree;

    var offsetDirection:int = -1;
    var rowLimit:int = inputGrid.size;
    var offsetAmount:int = 0; // offsetAmount goes 1 to n, then back to 0 again, used for calculating thw row offsets

    var mazeTiles:Vector.<Tile> = inputGrid.getTiles();
    var tileCounter:int = 0; // index to cycle through mazeTiles

    for(var rowCount:int = 0; rowCount < totalRows; rowCount++){
        currentPos.x = topLeft.x + (offsetAmount * rootThree / -2 * inputTileSize);

        for(var counter:int = 0; counter < rowLimit; counter++){
            drawHexTile(inputGraphics, currentPos.x, currentPos.y, inputTileSize, mazeTiles[tileCounter++]);
            currentPos.x += xStep;
        }

        currentPos.y += yStep;

        if(rowCount == (inputGrid.size - 1)){
            offsetDirection *= -1;
        }
        rowLimit += offsetDirection * -1;
        offsetAmount -= offsetDirection;

    } // end of for loop
} // end of drawHexGrid()

看起来您的drawHexTile中存在浮点问题。您是否尝试过向下舍入图形坐标,以便始终具有圆形像素坐标?差不多

inputGraphics.moveTo(Math.floor(inputX + (Math.cos(degrees * convertToRadians) * inputSize)), Math.floor(inputY + (Math.sin(degrees * convertToRadians) * inputSize)));

inputGraphics.lineTo(Math.floor(inputX + (Math.cos((degrees + 60) * convertToRadians) * inputSize)), Math.floor(inputY + (Math.sin((degrees + 60) * convertToRadians) * inputSize)));

虽然对最终产品不可取,但请尝试将所有int值更改为Num值。看看会发生什么。还要关注处理每个新磁贴的X位置的代码

来自您的评论


……嗯……这很有效。我不知道为什么我刚刚做了一个整数到数字的全局替换,它成功了。嗯,将返回并手动替换,以查看int变量究竟执行了什么操作


哦!!我想出来了。在drawHexTile()中,我将inputX和inputY设置为int类型。但实际上,正在计算的位置是浮动值,因此它们被四舍五入为整数,这隐含地导致了失调。简单的错误(反射),修复过程中没有魔法


虽然对最终产品不可取,但请尝试将所有int值更改为Num值。看看会发生什么。还要关注处理每个新磁贴的X位置的代码……嗯……这很有效。我不知道为什么我刚刚做了一个整数到数字的全局替换,它成功了。嗯,将返回并手动替换,以查看int变量到底做了什么。哦!我想出来了。在drawHexTile()中,我将inputX和inputY设置为int类型。但实际上,正在计算的位置是浮动值,因此它们被四舍五入为整数,这隐含地导致了失调。简单的错误(反射),修复过程中没有魔法。如果你回复你的评论作为答案,我会将其标记为接受。我也尝试过这样做。用于绘制和定位瓷砖的不同点的地板、圆形、天花板等。我似乎也看不到间隔中的任何图案
where n=4

             0       1       2       3
         4       5       6       7       8
     9      10      11      12      13      14   
15      16      17      18      19      20      21
    22      23      24      25      26      27   
        28      29      30      31      32
            33      34      35      36
inputGraphics.moveTo(Math.floor(inputX + (Math.cos(degrees * convertToRadians) * inputSize)), Math.floor(inputY + (Math.sin(degrees * convertToRadians) * inputSize)));

inputGraphics.lineTo(Math.floor(inputX + (Math.cos((degrees + 60) * convertToRadians) * inputSize)), Math.floor(inputY + (Math.sin((degrees + 60) * convertToRadians) * inputSize)));