import math


def get_neuron_distance_periodic(grid_w, grid_h, pre_pos, post_pos):
    """
    Compute distance between two neurons on a grid with periodic boundaries
    :param grid_w: width of grid
    :param grid_h: height of grid
    :param pre_pos: presynaptic neuron position
    :param post_pos: postsynaptic neuron position
    :return: Euclidean distance
    """
    x1, y1 = pre_pos
    x2, y2 = post_pos
    delta_x = abs(x1 - x2)
    delta_y = abs(y1 - y2)
    return math.sqrt(math.pow(min(delta_x, grid_w - delta_x), 2) +
                     math.pow(min(delta_y, grid_h - delta_y), 2))