   Coordinates in Hexagon-Based Tile Maps
  Comments for the article Coordinates in Hexagon-Based Tile Maps
  Âîáùåì àõóåííåéøàÿ ñòàòüÿ.   ;  There is an error in this article. The B-section right top should not subtract one from the x coordinate.//pseudo Code!!!// right sideif SectPxlX  >= HexagonR then begin  if sectPxlY < (2 * HexagonH - SectPxlX * m) then begin    ArrayY := SectY - 1;    // ArrayX := SectX - 1; <-- Remove this line  end else begin    ArrayY := SectY;    ArrayX := SectX;  end;end;// left side...;  Is it me or is the calculation for the values r and h the wrong way round. Consider a hex of side 100, h would evaluate to 15.x and r 98.x. r is clearly the longer distance - is it a simple typo or have I been looking at it too long ?;In the pseud-code:PixelX := ArrayX * 2 * TileR + (FieldX AND 1) * TileRWhat fieldX means? Shouldn't it be ArrayX? :S   ;  I don't understand what he means by this code inConverting pixel coordinates to array coordinates//pseudo Code!!!SectX := PixelX div (2 * TileR);SectY := PixelY div (HexagonH + HexagonS);SectPxlX := PixelX mod (2 * TileR);SectPxlY := PixelY mod (HexagonH + HexagonS);If (SectY AND 1 = 0) then SectTyp := A else SectTyp := B;Particularly this line here, >> If (SectY AND 1 = 0) then SectTyp := A else SectTyp := B;I am thinking SectTyp is a boolean, but his If arguement is really confusing me(SectY AND 1 = 0)if that was written in C# I would write it likeif (SectY && 1 = 0)but 1 can never = 0 so what is going on there?   ;  You seem to be familiar with C#, so I 'converted' it to that language.Explanation is in the comments surrounding the code.// Take a pixel location and divide its x coordinate by the width of a hexagon (= twice the radius).// This will give us an x coordinate in section 'units' rather than pixels.int sectX = pixelX / (2 * TileR);// Take the same pixel location and divide it y coordinate by the 'height' of a hexagon (= hexagonH + hexagonS).// Note that this isn't the real height, but roughly 3/4th of it, taking into account the overlap over hexagons in vertical direction.// Again we now have a y coordinate in sections.int sectY = pixelY / (hexagonH + hexagonS);// To further clarify the above two lines, consider a chess board where every square measures 2 by 2 inches.// To find out what row and column of location x = 5 inches, y = 3 inches is, we divide this by the size of a square://   x//   ------------------------------------//   |  |  |  |  |//   | (0,0) | (0,1) | (0,2) | (0,3) |//   |  |  |  |  |//   ------------------------------------//   |  |  |  |  |// y | (1,0) | (1,1) | (1,2) | (1,3) |//   |  |  |  |  |//   ------------------------------------//   |  |  |  |  |//// column = (x / 2) = (5 / 2) = 2// row = (y / 2) = (3 / 2) = 1// Find the pixel location *relative* to its section by dividing the x coordinate by the width of a hexagon and taking the remainder.// Using the chess board example again, we use 5 inches % 2 inches = 1 inch; the distance of the location to the left side of the square.int sectPxlX = pixelX % (2 * tileR);// Again, same thing for y.int sectPxlY = pixelY % (hexagonH + hexagonS);// The author seems to use some sort of constants named A and B. We could use a boolean value, because only two states are possible.// To make it more c-sharpish, I've chosen to use an enum here:enum SectionType{  TypeA, TypeB};// And we will store the result in this variable:SectionType sectionType;// To understand the following, you need to know about bitwise operations. // Explaining it here would be  too much, but take a look http://en.wikipedia.org/wiki/Bitwise_operation#AND, or google for more information.if ((sectY & 1) == 0)  sectionType = SectionType.TypeA;  // sectY is an even numberelse  sectionType = SectionType.TypeB;  // sectY is an odd number// If you are more familiar with modulo operations, you might understand this alternative better:if ((sectY % 2) == 0)  sectionType = SectionType.TypeA;  // sectY is an even numberelse  sectionType = SectionType.TypeB;  // sectY is an odd number;  Thanks for the description - I've already converted into C# code after I learned about bit-wise operations. There are still some stuff I'm not fully understanding and I was wondering for some clarification. Here is the re-created method   private void FindHex(int x, int y)   {  mapSection.X = x / hex.Width; //convert pixel to section units!  mapSection.Y = y / (hex.SideHeightLarge + hex.SideHeightSmall); //convert pixel to section units!  mapSectionPixel.X = (x - mapSection.X) * hex.Width; //Find the pixel location *relative* to its section  mapSectionPixel.Y = (y - mapSection.Y) * (hex.SideHeightLarge + hex.SideHeightSmall);  if (((int)mapSection.Y & 1) == 0)  { RowOdd = false;  }  else RowOdd = true;  float m = hex.SideHeightSmall / hex.HalfWidth;  ///  /// * If the pixel position in question lies within the big bottom area the array coordinate of the   ///tile is the same as the coordinate of our section.  /// * If the position lies within the top left edge we have to subtract one from the horizontal (x)  ///and the vertical (y) component of our section coordinate.  /// * If the position lies within the top right edge we reduce only the vertical component.   ///  if (RowOdd == true)  { // left Edge if (mapSectionPixel.Y < (hex.SideHeightSmall - (mapSectionPixel.X * m))) {x = (int)mapSection.X - 1;y = (int)mapSection.Y - 1; } // right Edge if (mapSectionPixel.Y < (-hex.SideHeightSmall + (mapSectionPixel.X * m))) {x = (int)mapSection.X;y = (int)mapSection.Y - 1; } else {// middlex = (int)mapSection.X;y = (int)mapSection.Y; }  }  if (RowOdd == false)  { // left Edge if (mapSectionPixel.X >= hex.HalfWidth) {if (mapSectionPixel.Y < ((2 * hex.SideHeightSmall) - (mapSectionPixel.X * m))){    x = (int)mapSection.X - 1;    y = (int)mapSection.Y - 1;}else{    x = (int)mapSection.X;    y = (int)mapSection.Y;} } // right Edge if (mapSectionPixel.X < hex.HalfWidth) {if (mapSectionPixel.Y < (mapSectionPixel.X * m)){    x = (int)mapSection.X;    y = (int)mapSection.Y - 1;}else{    x = (int)mapSection.X - 1;    y = (int)mapSection.Y;} }  }   }I have another method called GetMouseOverlay, that gets called in the update what I think that needs to be done is that I plug in the mouse coordinates into FindHex(x, y), like this  MouseState previousMouseState = Mouse.GetState();  int mouseX = previousMouseState.X;  int mouseY = previousMouseState.Y;  FindHex(mouseX, mouseY);what is the output? is it Hex.X and Hex.Y?? if that is so - then I'm good. But I tend to get lost in all those conversions...   ;  actually no - the output would not be Hex.X and Hex.Y because that is the origin of Hex. So I am confused what the output is. I've created a list and added in all the hexes as I loaded them. How do I check for a Hex in the list against this output?   ;  Nevermind all that stuff I posted!!!!!I got it partially solved. I can now select the hexes, but the selection is off just a little. Somewhere in the logic is incorrect.   