Skip to content

Instantly share code, notes, and snippets.

@gabraganca
Created May 17, 2018 02:19
Show Gist options
  • Save gabraganca/eb8fe8ebd0c6ce3205587f280294ec4f to your computer and use it in GitHub Desktop.
Save gabraganca/eb8fe8ebd0c6ce3205587f280294ec4f to your computer and use it in GitHub Desktop.
Get the average distance of three points in the cartesian plane.
from math import sqrt
def get_distance(first_coord, second_coord):
"""
Get the distance between two points in the cartesian plane.
Parameters
----------
first_coord: tuple
Position x and y of first point.
second_coord: tuple
Position x and y of second point.
Returns
-------
The distance.
"""
x1, y1 = first_coord
x2, y2 = second_coord
distance = sqrt((y2-y1)**2 + (x2-x1)**2)
return distance
def avg_distance(first_coord, second_coord, third_coord):
"""
Get the average distance between three points.
Parameters
----------
first_coord: tuple
Position x and y of first point.
second_coord: tuple
Position x and y of second point.
third_coord: tuple
Position x and y of second point.
Returns
-------
The average distance.
"""
# Get distances
dist_1 = get_distance(first_coord, second_cord)
dist_2 = get_distance(first_coord, third_cord)
dist_3 = get_distance(second_coord, third_cord)
avg = (dist_1 + dist_2 + dist_3)/3
return avg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment