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:
[[[623 284]] [[526 256]] [[532 189]] [[504 166]] [[323 175]] [[276 219]] [[119 221]] [[ 1 272]] [[ 0 473]] [[615 479]]]
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:
import numpy as np x.append(x[0]) y.append(y[0]) d = [np.sqrt((x[i + 1] - x[i])**2 + (y[i + 1] - y[i])**2) for i in range(len(x) - 1)]
The append
is required in order to close the polygon and calculate all edges length. With points you provided above, the edges distances are:
[100.96038827183659, 67.26812023536856, 36.235341863986875, 181.22361876973983, 64.38167441127949, 157.01273833673497, 128.5496013218244, 201.0024875467963, 615.0292675962665, 195.1640335717624]