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

前端 未结 8 692
不知归路
不知归路 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).

    0 讨论(0)
  • 2020-12-07 15:44

    I dont know if it's going to help anyone but i have come up with a much simpler solution. When i create my Hexagon im just giving them a middle point and by finding the closest middle point with the mouse coordonate i can find wich one im on !

    0 讨论(0)
提交回复
热议问题