Daniel
Daniel

Reputation: 69

How to print the points as an array?

How can I improve following formula that works for a list of array. I do not want to enter x and y manually, and I want to know this distance is related to which points

For example: dis: (2,4) & (-4,2) = ?? dis: (-4,2) & (10,-5) = ??

import math
array = [(2, 4), (-4, 2),(10, -5)]


def Distance(x1, y1, x2, y2):

    dist= math.sqrt((x2-x1)**2 +(y2-y1)**2)
    return dist

Distance(array)

Upvotes: 0

Views: 88

Answers (2)

Spherical Cowboy
Spherical Cowboy

Reputation: 566

This will return a list containing the (Euclidean) distance between each two consecutive points entered as tuples (x, y). These code snippets assume Python 3.x, so small changes might be necessary if you are using Python 2.7. Your tags are not clear about this.

import math


def distances(lst):
    ln = len(lst)
    dists = []
    if ln >= 2:  # if array contains no or only 1 tuple, return []
        for index, tup in enumerate(lst[:ln - 1]):
            x1, y1 = tup
            x2, y2 = lst[index + 1]  # gets x and y of next tuple in the array
            dists.append(math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2))
    return dists


array0 = []
array1 = [(2, 4), (-4, 2)]
array2 = [(5, 2), (-1, 3), (6, -6)]
array3 = [(2, 4), (-4, 2), (10, -5), (9, -2)]

print(distances(array0))
print(distances(array1))
print(distances(array2))
print(distances(array3))

Output:

[]
[6.324555320336759]
[6.082762530298219, 11.40175425099138]
[6.324555320336759, 15.652475842498529, 3.1622776601683795]

If you want to print the distances the way you mentioned:

import math


def distances(lst):
    ln = len(lst)
    if ln >= 2:
        for index, tup in enumerate(lst[:ln - 1]):
            x1, y1 = tup
            x2, y2 = lst[index + 1]
            dist = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
            print(lst[index:index + 2], "=", dist)


array = [(5, 2), (-1, 3), (6, -6)]

distances(array)

Output:

[(5, 2), (-1, 3)] = 6.082762530298219
[(-1, 3), (6, -6)] = 11.40175425099138

The same functionality as before can be achieved using the numpy module instead of math:

import numpy as np


def distances(lst):
    ln = len(lst)
    dists = []
    if ln >= 2:
        for index, tup in enumerate(lst[:ln - 1]):
            dists.append(np.linalg.norm(lst[index + 1] - tup))
    return dists


array0 = np.array([])
array1 = np.array([(2, 4), (-4, 2)])
array2 = np.array([(5, 2), (-1, 3), (6, -6)])
array3 = np.array([(2, 4), (-4, 2), (10, -5), (9, -2)])

print(distances(array0))
print(distances(array1))
print(distances(array2))
print(distances(array3))

Output:

[]
[6.324555320336759]
[6.0827625302982193, 11.401754250991379]
[6.324555320336759, 15.652475842498529, 3.1622776601683795]

If you only want the distance between two tuples contained in a list:

import math


def distance(lst):
    return math.sqrt((lst[1][0] - lst[0][0]) ** 2 + (lst[1][1] - lst[
        0][1]) ** 2)


array = [(2, 4), (-4, 2)]

print(distance(array))

Output:

6.324555320336759

Upvotes: 1

asongtoruin
asongtoruin

Reputation: 10359

Assuming you're using Python 2.7 as it's unclear:

import math


def Distance(x1, y1, x2, y2):
    return math.sqrt((x2-x1)**2 +(y2-y1)**2)

points = [(5, 2), (-1, 3), (6, -6)]

pairs = [(points[i], points[i+1]) for i in range(len(points)-1)]

for val in pairs:
    print 'Distance between {0} and {1}: {2}'.format(val[0], val[1], Distance(*val[0]+val[1]))

We use a list comprehension to work out each pair of points (assuming you only want consecutive values in the list for each pair), and then use tuple unpacking to feed these as inputs to the Distance method.

If you are in Python 3.x, this last line should be:

print('Distance between {0} and {1}: {2}'.format(val[0], val[1], Distance(*val[0], *val[1])))

With your given example, this prints:

Distance between (5, 2) and (-1, 3): 6.0827625303
Distance between (-1, 3) and (6, -6): 11.401754251

Upvotes: 0

Related Questions