Optimized getGridCoordinates method.