Skip to content

Instantly share code, notes, and snippets.

@chuongmep
Last active January 17, 2022 05:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save chuongmep/c1f1fad0f6d9398481297675c0b836e4 to your computer and use it in GitHub Desktop.
Save chuongmep/c1f1fad0f6d9398481297675c0b836e4 to your computer and use it in GitHub Desktop.
def calculate_distance(p1, p2):
    return ((p1[0] - p2[0])**2 + (p1[1] - p2[1])**2)**0.5
def find_nearest_point(start_point, points):
    min_distance = math.inf
    index = 0
    for i, point in enumerate(points):
        distance = calculate_distance(start_point, point)
        if min_distance > distance:
            min_distance = distance
            index = i
    if points:
        next_point = points.pop(index)
        stack.append(next_point)
        find_nearest_point(next_point, points)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment