Function applyBounds

Calculates a legal position for a cell based on grid size and bounds wrapping logic.