六角网格绘图问题

时间:2016-10-24 03:37:55

标签: actionscript-3 drawing hexagonal-tiles

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

seems ok misaligned

正如你所看到的,六边形只是略微错位,但我相信我的数学是正确的(其中一些是通过http://www.redblobgames.com/grids/hexagons/验证的。)

我的绘图方法是从左上角六边形(第一行中的第一个图块)开始绘制并绘制该行图块。然后对于下一行,有一个负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()

每个六边形的实际绘图都在这个循环中:

private function drawHexTile(inputGraphics:Graphics, inputX:int, inputY:int, inputSize:int, inputTile:Tile):void {
    inputGraphics.lineStyle(0.1, 0, 1);

    var convertToRadians:Number = Math.PI / 180;

    // easier to draw by wall, since need to look up each wall and can set a starting degree without having to worry about the 'end degree'
    // (since the end may not be 360 degrees if the starting degree is in the negatives or a high degree)
    var degrees:int = -150; // starting wall is the top left wall of the hexagon tile
    for(var counter:int = 0; counter < 6; counter++){

        if(inputTile.walls[counter] == true){
            inputGraphics.moveTo(inputX + (Math.cos(degrees * convertToRadians) * inputSize), 
                                    inputY + (Math.sin(degrees * convertToRadians) * inputSize));

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

} // end of drawHexTile() method

乍一看,我认为这个问题是由于浮点数学的准确性,但我不确定如何解决这个问题。

有什么想法吗?

我知道我的绘图方法最终会有很多重叠的线条,目前还不错。如果它有帮助,整个代码用于六边形迷宫生成器,它工作正常,因此它可以被忽略,因为它不是问题的一部分。

如果它有用,我如何存储六边形瓷砖只是在一个长数组中,索引如下:

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

2 个答案:

答案 0 :(得分:1)

虽然不适合您的最终产品,但请尝试将所有int值更改为Num值。走着瞧吧。还要关注处理每个新磁贴的X位置的代码。

来自您的评论

  

......呃......这很有效。我不知道为什么。 = |我刚刚对Numbers进行了全局替换,并且有效。嗯,将返回并手动替换以查看int变量的确切内容。

     

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

答案 1 :(得分:-1)

看起来像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)));