六边形网格,你如何找到这六角形点是? [英] Hexagonal Grids, how do you find which hexagon a point is in?

查看:401
本文介绍了六边形网格,你如何找到这六角形点是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有由行和六边形列的地图

I have a map made up of rows and columns of hexagons

这是不是十六进制地图我使用的实际图像,但使用相同的尺寸和形状的六边形

This isn't an actual image of the hex-map I am using, but uses the same size and shape hexagons

我需要能够分辨出哪个鼠标移动,当用户点击,

I need to be able to tell which one the mouse is over when the user clicks,

每个六边形重新被砖类的一个实例psented $ P $,然而这并没有持有任何地点的具体数据,甚至是多边形,所以基本上讲,其中一个特定的六边形是唯一的办法,要知道这是二维数组中的位置。

Each Hexagon is represented by an instance of a "Tile" class, however this doesn't hold any location specific data, or even a polygon, so basically the only way to tell where a particular hexagon is, is to know it's position in the 2D array.

我已经使用了方形网格之前,这是比较容易找出哪些方被选中,是因为像素也是方形的,

I have used a square grid before, and it was relatively easy to figure out which square was selected, because pixels are also square,

        // example where each square is 10 by 10 pixels:

private void getClickedSquare(MouseEvent me)
    {
        int mouseX = me.getX();// e.g. 25
        int mouseY = me.getY();// e.g. 70

        int squareX= (int) (mouseX / 10);// in this case 2
        int squareY= (int) (mouseY / 10);// in this case 7

//then to access the tile I would do

        map.squares[squareX][squareY].whatever();
    }

不过,我什至不知道从哪里开始六边形,没有任何人有任何经验?

But I'm not even sure where to start with Hexagons, does anyone have any experience?

我不能使用多边形(JAVA),因为当我上走动地图在屏幕上,并增加它的大小,我会遇到的问题与更新每一帧大量多边形。虽然那时我可能只是检查点是否包含在任何地图上的瓦片的多边形!

I cannot use polygons (Java), as when I get onto moving the map around on screen, and increasing it's size I'll run into problems with updating vast amounts of polygons each frame. Although then I could just check to see if a point is included in any of the map's tile's polygons!

目前显示的六边形只是BufferedImages。

At the moment the hexagons displayed are just BufferedImages.

如果您想了解更多的信息请咨询, 感谢您的时间:D

If you want to know any more information please ask, Thanks for your time :D

推荐答案

(更新:重构code,使更多的理解和更有效) (更新:减少答案的长度,在code修复的错误,提高了图像质量)

(UPDATED: Refactored code to make more understandable and more efficient) (UPDATED: Reduced answer length, fixed bugs in code, improved quality of images)

此图显示了六边形网格的左上角和叠加是蓝色正方形网格。这是很容易找到的平方其中点是内,这将给予其六边形的粗略近似太。六角形的白色部分显示,其中方和六边形网格共享相同的坐标和六边形的灰色部分表示,他们不知道。

This image shows the top left corner of a hexagonal grid and overlaid is a blue square grid. It is easy to find which of the squares a point is inside and this would give a rough approximation of which hexagon too. The white portions of the hexagons show where the square and hexagonal grid share the same coordinates and the grey portions of the hexagons show where they do not.

的解决方案是,现在简单,只要发现该框的点是在,然后检查是否该点是在任一三角形的,并在必要时校正答案

The solution is now as simple as finding which box a point is in, then checking to see if the point is in either of the triangles, and correcting the answer if necessary.

private final Hexagon getSelectedHexagon(int x, int y)
{
    // Find the row and column of the box that the point falls in.
    int row = (int) (y / gridHeight);
    int column;

    boolean rowIsOdd = row % 2 == 1;

    // Is the row an odd number?
    if (rowIsOdd)// Yes: Offset x to match the indent of the row
        column = (int) ((x - halfWidth) / gridWidth);
    else// No: Calculate normally
        column = (int) (x / gridWidth);

在这一点上,我们有包装盒我们的观点是在的行和列,接下来我们需要测试我们对六边形的两个顶部边缘处,看看我们的一点就在于无论是上述的六边形的:

At this point we have the row and column of the box our point is in, next we need to test our point against the two top edges of the hexagon to see if our point lies in either of the hexagons above:

    // Work out the position of the point relative to the box it is in
    double relY = y - (row * gridHeight);
    double relX;

    if (rowIsOdd)
        relX = (x - (column * gridWidth)) - halfWidth;
    else
        relX = x - (column * gridWidth);

具有相对坐标使得下一步骤更容易。

Having relative coordinates makes the next step easier.

像上面的图片中,如果在我​​们的观点是> MX + C 我们知道,我们的观点在于线之上,并且在我们的情况下,六边形上方和当前行和列的左侧。的注意,在坐标系中的java具有的y屏幕的左上角开始于0并且不左下角如通常在数学,因此用于用于右左边缘和正梯度负梯度

Like in the image above, if the y of our point is > mx + c we know our point lies above the line, and in our case, the hexagon above and to the left of the current row and column. Note that the coordinate system in java has y starting at 0 in the top left of the screen and not the bottom left as is usual in mathematics, hence the negative gradient used for the left edge and the positive gradient used for the right.

    // Work out if the point is above either of the hexagon's top edges
    if (relY < (-m * relX) + c) // LEFT edge
        {
            row--;
            if (!rowIsOdd)
                column--;
        }
    else if (relY < (m * relX) - c) // RIGHT edge
        {
            row--;
            if (rowIsOdd)
                column++;
        }

    return hexagons[column][row];
}


在上面的例子中使用的变量的简单解释:


A quick explanation of the variables used in the above example:

m为梯度,所以 M = C /半角

这篇关于六边形网格,你如何找到这六角形点是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆