Search code examples
pythonpygamecoordinatespixel

Compute coordinates to pixels from a square grid


I am trying to compute square grid coordinates to pixel coordinates, but since I can't really understand this things, I thought I might ask for some help. I have to convert from an hexagonal grid to a square grid, something like this: enter image description here

The grid that I was working with before was this: enter image description here

I have the code from the function that computes the hexagonal grid coordinates to pixels:

def hex_to_pix(t):
   x = t[1] * dx + (t[0] % 2) * dxh + dxh
   y = t[0] * dy + hh / 2
   return (x, y)

This code was already given. The function receives a tuple, then calculates the x and the y and return a tuple. This function is called for each grid coordinate. dx is the horizontal distance distance from two adjacent hexagons, dxh is the half of that distance, dy is the vertical distance from two adjacent hexagons and hh is the hexagon's height. How could I convert this function to something that computes from a square grid?


Solution

  • The coordinates of the corners in a square grid are just the index of the cell multiplied by the size of a cell. In the following, dx and dy are the size of a cell and t is a tuple that contains the row and column:

    def squre_to_pix(t):
       x = t[1] * dx
       y = t[0] * dy
       return (x, y)