Creating algorithm for grid generation