Hackerss.com

hackerss
hackerss

Posted on

Python Calculate the distance of two points


#create a function that calculates the distance between two points
def distance(x1, y1, x2, y2):
    """
    Calculate the distance between two points.

    Parameters
    ----------
    x1 : int
        X coordinate of the first point.
    y1 : int
        Y coordinate of the first point.
    x2 : int
        X coordinate of the second point.
    y2 : int
        Y coordinate of the second point.

    Returns
    -------
    float
        The distance between the two points.
    """

    #calculate the distance between the two points
    return  ((x2 - x1)**2 + (y2 - y1)**2)**0.5

#example
result = distance(0,0,3,4)
#output
print(result)  #5.0
Enter fullscreen mode Exit fullscreen mode

Top comments (0)