I have a custom image object, which is organised in tiles of fixed size, disposed in a tile grid. Let's assume, for instance, that the fixed tileSize
is 256 pixels (each tile is a subimage of 256x256 pixels), and that my global image is 1000x500 pixels: this results in a tile grid of 4x2 tiles of 256x256 pixels.
Now, given a point in the global image coordinates, this is how I convert to tile coordinates (i.e., the tile position in the tile grid, plus the pixel position inside that pixel):
struct Point {
int x;
int y;
}
Point pGlobal = (...);
// Grid coordinates of the tile containing pGlobal
Point pTileGrid = Point(pGlobal.x / tileSize, pGlobal.y / tileSize);
// Pixel inside that tile that contains pGlobal
Point pTile = Point(pGlobal.x % tileSize, pGlobal.y % tileSize);
The problem is that this is painfully slow. How can I optimize this?
Thank you in advance. If I did not explain myself correctly in any point please ask me to improve my formulation.