I found a lot of posts on how to calculate distance between two points or from one point to polygon but I simply can’t find how to calculate distance of each edge. I have a polygon, where the coordinates are these:
JavaScript
x
20
20
1
[[[623 284]]
2
3
[[526 256]]
4
5
[[532 189]]
6
7
[[504 166]]
8
9
[[323 175]]
10
11
[[276 219]]
12
13
[[119 221]]
14
15
[[ 1 272]]
16
17
[[ 0 473]]
18
19
[[615 479]]]
20
I simply want to calculate length of each edge. Maybe I should use (math.dist(p, q))
with for loop
or something?
Advertisement
Answer
If points coordinates are stored in two python lists x
and y
, then you can calculate each edge length with:
JavaScript
1
7
1
import numpy as np
2
3
x.append(x[0])
4
y.append(y[0])
5
6
d = [np.sqrt((x[i + 1] - x[i])**2 + (y[i + 1] - y[i])**2) for i in range(len(x) - 1)]
7
The append
is required in order to close the polygon and calculate all edges length. With points you provided above, the edges distances are:
JavaScript
1
2
1
[100.96038827183659, 67.26812023536856, 36.235341863986875, 181.22361876973983, 64.38167441127949, 157.01273833673497, 128.5496013218244, 201.0024875467963, 615.0292675962665, 195.1640335717624]
2