Hexagonal Grids, how do you find which hexagon a point is in?

前端 未结 8 719
不知归路
不知归路 2020-12-07 15:14

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,

8条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-07 15:44

    This is an addendum to SebastianTroy's answer. I would leave it as a comment but I don't enough reputation yet.

    If you want to implement an axial coordinate system as described here: http://www.redblobgames.com/grids/hexagons/

    You can make a slight modification to the code.

    Instead of

    // 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);
    

    use this

    float columnOffset = row * halfWidth;
    column = (int)(x + columnOffset)/gridWidth; //switch + to - to align the grid the other way
    

    This will make the coordinate (0, 2) be on the same diagonal column as (0, 0) and (0, 1) instead of being directly below (0, 0).

提交回复
热议问题