1

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?

2
  • 1
    Um, you just repeat that computation 8 times, presumably in a loop. And that's not an octagon. It's 5 sides of a hexagon. Commented Jun 7, 2021 at 17:38
  • yes m facing difficulty in looping only @TimRoberts Commented Jun 7, 2021 at 17:41

2 Answers 2

2

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)
Sign up to request clarification or add additional context in comments.

Comments

1
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

8 Comments

hii thanks for detailed solution but i m facing problem while looping the array which i have shown in question, my program is returning array like that from where i have to calculate the length of side ..hat you have shown is on list .
i have tried to convert the array to list by array.tolist() but its giving error as IndexError: list index out of range
What you have is am 8x1x2 array. So, each element is a 1-element list that contains a 2-element point. You'll need to handle that extra level. I'll edit the answer.
for x,y in zip(approx,approx[1:]): # print(x[0],y[0]) d = math.sqrt((x[0][1]-y[0][1])*(x[0][1]-y[0][1]) + (x[0][0]-y[0][0])*(x[0][0]-y[0][0])) print('length between point:',x[0],'and', y[0],'is', d) using this it was done thanks
Ah, because the + is doing vector addition, not list concat. I'm a little disappointed you couldn't look this up. Use for pt in zip(points,np.roll(points,-1,0)):
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.