Home > OS >  Calculating total distance between points
Calculating total distance between points

Time:03-06

I wrote this function which finds the distance between two points. Now my next task is to write a function that takes in a list of points which are tuples and calculates the total distance by using the distance function to find the distance of each pair of points and I am stuck I don't know how to go about it any help please? This is the first function:

def distance(point1, point2):
    dist = math.sqrt((point2[0] - point1[0])** 2   (point2[1] - point1[1])** 2) 
    return dist

CodePudding user response:

Assuming that you have an even list and each pair you want to calculate:

list_points = [(2,1),(50,1), (30,1), (30,5)]
for i in range(0,len(list_points),2):
    pair = list_points[i:i 2]
    dist(pair[0], pair[1])

The third parameter in range is a way to batch. Then if you want to make it a function:

def compute_distance(list_points):
    """compute distance of points give a list of tuples"""
    computed_list = []
    for i in range(0,len(list_points),2):
        pair = list_points[i:i 2]
        computed_list.append(dist(pair[0], pair[1]))
    return computed_list

CodePudding user response:

Try this.

import math

def calc_total_distance(point_list):
    total_dist = 0
    for i in range(len(point_list)-1):
        dist = math.sqrt((point_list[i][0] - point_list[i 1][0])** 2   (point_list[i][1] - point_list[i 1][1])** 2)
        total_dist  = dist
    return(total_dist)

point_list = [(1,2), (2,2), (2,10)]
print(calc_total_distance(point_list))

CodePudding user response:

From what I understand, your distance function takes in two tuples as argument. So for the new function you'd have a list containing nested tuples of the sort:

list_points = [((2, 1), (50, 1)), ((30, 1), (30, 5))]

Note that each tuple contains two tuples.

If this is the case, here's how I'll write the new function:

    `def total_distance(list_distances):
        result = [distance(i[0], i[1]) for i in list_distances]
        return sum(result)`

The second line here uses list comprehension to loop through the list and map the items to your distance function. The sum of all the distances obtained is returned

CodePudding user response:

You need to sum() the distanced between each pair of points. A good way to get the pairs is by using a generator function like the one named pairwise() shown below. You're also reinventing the wheel with regards to calculating the distance between two points — because there is a math.hypot() function.

The code below shows using them together to do what you want:

import math

def pairwise(iterable):
    """s -> (s0,s1), (s1,s2), (s2, s3), ..."""
    a, b = iter(iterable), iter(iterable)
    next(b, None)
    return zip(a, b)

def distance(point1, point2):
    return math.hypot(point2[0]-point1[0], point2[1]-point1[1])


if __name__ == '__main__':
    points = [(1,2), (2,2), (2,10)]
    print(f'total distance:', sum(distance(a, b) for a,b in pairwise(points)))

  • Related