OrthoMap.coordAtPoint

Get the grid location corresponding to a given pixel coordinate.

If the point is out of map bounds, the returned coord will also be out of bounds. Use the containsPoint method to check if a point is in bounds.

struct OrthoMap(Tile)
coordAtPoint
(
T
)
(
T pos
)

Examples

struct Vec { float x, y; }

// 5x3 map, rows from 0 to 4, cols from 0 to 2
auto tiles = [
  [ 00, 01, 02, 03, 04, ],
  [ 10, 11, 12, 13, 14, ],
  [ 20, 21, 22, 23, 24, ],
];
auto map = OrthoMap!int(tiles, 32, 32);

assert(map.coordAtPoint(Vec(0   , 0  )) == RowCol(0  , 0 ));
assert(map.coordAtPoint(Vec(16  , 16 )) == RowCol(0  , 0 ));
assert(map.coordAtPoint(Vec(32  , 0  )) == RowCol(0  , 1 ));
assert(map.coordAtPoint(Vec(0   , 45 )) == RowCol(1  , 0 ));
assert(map.coordAtPoint(Vec(105 , 170)) == RowCol(5  , 3 ));
assert(map.coordAtPoint(Vec(-10 , 0  )) == RowCol(0  , -1));
assert(map.coordAtPoint(Vec(-32 , -33)) == RowCol(-2 , -1));

Meta