def get_dx(x1, x2, y1, y2, L=1.0, periodic=True):
    """
    Compute difference of vectors [x1 - x2, y1 - y2] while
    checking for periodicity if necessary
    L:          boxsize
    periodic:   whether to assume periodic boundaries
    """
    dx = x1 - x2
    dy = y1 - y2
    if periodic:
        if hasattr(L, '__len__'):
            Lxhalf = L[0] / 2.0
            Lyhalf = L[1] / 2.0
        else:
            Lxhalf = L / 2.0
            Lyhalf = L / 2.0
            L = [L, L]
        if dx > Lxhalf:
            dx -= L[0]
        elif dx < -Lxhalf:
            dx += L[0]
        if dy > Lyhalf:
            dy -= L[1]
        elif dy < -Lyhalf:
            dy += L[1]
    return dx, dy
