def squared_toroidal_dist(p1, p2, world_size=(60, 60)):
    """
    Separated out because sqrt has a lot of overhead
    """
    halfx = world_size[0]/2.0
    if world_size[0] == world_size[1]:
        halfy = halfx
    else:
        halfy = world_size[1]/2.0

    deltax = p1[0] - p2[0]
    if deltax < -halfx:
        deltax += world_size[0]
    elif deltax > halfx:
        deltax -= world_size[0]

    deltay = p1[1] - p2[1]
    if deltay < -halfy:
        deltay += world_size[1]
    elif deltay > halfy:
        deltay -= world_size[1]

    return deltax*deltax + deltay*deltay
Separated out because sqrt has a lot of overhead