EngineerHus123
EngineerHus123

Reputation: 82

How to add distances together?

I'm having problems with adding distances together

def calculate_path(selected_map):
    n = len(generated_map)
    distance = 0.0
    for i in range(n):
        starting_x = selected_map[i][0]
        starting_y = selected_map[i][1]
        if i < n-1:
            destination_x = selected_map[i+1][0]
            destination_y = selected_map[i+1][1]
        else:
            destination_x = selected_map[0][0]
            destination_y = selected_map[0][1]
        dist = calculate_distance(starting_x, starting_y, destination_x, destination_y)
        distance = dist
        print("Total distance", distance)

    return distance

When i run this code i get multiple distances:

Total distance 476.1932380872286
Total distance 297.13969778540195
Total distance 312.9217154497271
Total distance 143.34922392534952
Total distance 296.66479400158016
Total distance 234.68276459936294
Total distance 150.29637387508723
Total distance 394.01142115426046
Total distance 364.6066373504465
Total distance 385.3154551792596
Total distance 72.78049189171504
Total distance 170.42593699317015
Total distance 113.13708498984761
Total distance 345.77593901253454
Total distance 260.2710126003278
Total distance 376.5023240300118
Total distance 182.78949641595932
Total distance 40.718546143004666
Total distance 62.93647591023825
Total distance 189.1718795170149
Total distance 171.23083834403195
Total distance 165.75886099994776
Total distance 287.31341771661135
Total distance 192.26024029944412
Total distance 336.09076155110245
Total distance 287.2107240337658
Total distance 127.1416532848303

These distances are only a snippet, it generates a lot more outputs so my question is how can i add all these distances into 1 line answer which states the total of all these distances.

Upvotes: 0

Views: 169

Answers (2)

Alain T.
Alain T.

Reputation: 42143

You need to add dist to distance instead of assigning the last value (i.e. distance += dist. You should also place your print() before the return rather than inside the loop.

To process elements of a list together with the next element, you can use the zip function:

distance = sum(calculate_distance(x1,y1,x2,y2) 
               for (x1,y1),(x2,y2) in zip(selected_map, selected_map[1:]))

The zip function will return pairs of elements by combining the points from the start of the list with the points starting at the second element.

[EDIT]

In order to join the last point with the first, you can add it to the second parameter of the zip() call:

zip(selected_map, selected_map[1:]+selected_map[:1])

Upvotes: 1

Arty
Arty

Reputation: 16747

Just sum-up all distance values and print total, as in code below.

def calculate_path(selected_map):
    n = len(generated_map)
    distance, tdist = 0.0, 0.0
    for i in range(n):
        starting_x = selected_map[i][0]
        starting_y = selected_map[i][1]
        if i < n-1:
            destination_x = selected_map[i+1][0]
            destination_y = selected_map[i+1][1]
        else:
            destination_x = selected_map[0][0]
            destination_y = selected_map[0][1]
        dist = calculate_distance(starting_x, starting_y, destination_x, destination_y)
        distance = dist
        #print("Total distance", distance)
        tdist += distance

    print("Total distance", tdist)
    return tdist

Upvotes: 1

Related Questions