k_p
k_p

Reputation: 313

find length between each point from array python

suppose we have 8 point whose x,y coordinates are given as

[[[224  64]]
 [[ 62 381]]
 [[224 661]]
 [[568 661]]
 [[733 348]]
 [[650 205]]
 [[509 204]]
 [[509  64]]]

Suppose these are 8 points of a polygon and want to find the length of each side. For two points, I am able to find the length as

dx = math.abs(x2 - x1)
dy = math.abs(y2 - y1)
dist = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)

How to find length of each side of polygon with above x, y coordinates?

Upvotes: 1

Views: 868

Answers (2)

Tim Roberts
Tim Roberts

Reputation: 54747

import math
points = [
 [224,  64],
 [ 62, 381],
 [224, 661],
 [568, 661],
 [733, 348],
 [650, 205],
 [509, 204],
 [509,  64]]

for x,y in zip(points,points[1:]):
    d = math.sqrt((x[1]-y[1])*(x[1]-y[1]) + (x[0]-y[0])*(x[0]-y[0]))
    print(x, y, d)

Output:

[224, 64] [62, 381] 355.99578649191903
[62, 381] [224, 661] 323.4872485894923
[224, 661] [568, 661] 344.0
[568, 661] [733, 348] 353.82764165621654
[733, 348] [650, 205] 165.3420696616563
[650, 205] [509, 204] 141.00354605470034
[509, 204] [509, 64] 140.0

Here's what it looks like: enter image description here

FOLLOWUP

Here's the code using your exact structure:

import numpy as np
import math
points = [
 [224,  64],
 [ 62, 381],
 [224, 661],
 [568, 661],
 [733, 348],
 [650, 205],
 [509, 204],
 [509,  64]]
points = np.array(points).reshape(8,1,2)
print(points)
for pt in zip(points,points[1:]):
    print( pt )
    x = pt[0][0]
    y = pt[1][0]
    d = math.sqrt((x[1]-y[1])*(x[1]-y[1]) + (x[0]-y[0])*(x[0]-y[0]))
    print(x, y, d)

Output:

[[[224  64]]
 [[ 62 381]]
 [[224 661]]
 [[568 661]]
 [[733 348]]
 [[650 205]]
 [[509 204]]
 [[509  64]]]
(array([[224,  64]]), array([[ 62, 381]]))
[224  64] [ 62 381] 355.99578649191903
(array([[ 62, 381]]), array([[224, 661]]))
[ 62 381] [224 661] 323.4872485894923
(array([[224, 661]]), array([[568, 661]]))
[224 661] [568 661] 344.0
(array([[568, 661]]), array([[733, 348]]))
[568 661] [733 348] 353.82764165621654
(array([[733, 348]]), array([[650, 205]]))
[733 348] [650 205] 165.3420696616563
(array([[650, 205]]), array([[509, 204]]))
[650 205] [509 204] 141.00354605470034
(array([[509, 204]]), array([[509,  64]]))
[509 204] [509  64] 140.0

Upvotes: 1

Mad Physicist
Mad Physicist

Reputation: 114478

Let's say your list is called coords. You have a numpy tag, so I am assuming you want a fast solution using numpy.

coords = [
 [224,  64],
 [ 62, 381],
 [224, 661],
 [568, 661],
 [733, 348],
 [650, 205],
 [509, 204],
 [509,  64]]

You will want to call np.diff on the successive elements, so to get the last side, you will want to replicate the first point at the end. You can do this as the same time as you convert your array to numpy:

vertices = np.concatenate((coords, coords[:1]), axis=0)

Now find the lengths of the sides:

sides = np.linalg.norm(np.diff(vertices, axis=0), axis=-1)

For your 2D case, you could also use np.hypot instead of np.linalg.norm:

sides = np.hypot(*np.diff(vertices, axis=0).T)

Upvotes: 2

Related Questions