如何更改多边形边的长度以确保生成的多边形形状相似

时间:2021-03-30 05:44:56

标签: math geometry

多边形 enter image description here

我有一个只有 90 度和 45 度角的多边形,现在我想改变任意边的长度并调整多边形,以便结果应该是一个相似的多边形。

像这样: 换边后的形状 enter image description here

1 个答案:

答案 0 :(得分:0)

import numpy as np
import matplotlib.pyplot as plt

# an arbitrary polygon, for example a pentagon:
P = np.array([[0.2, 0.1], [1, -0.2], [3, 1], [2, 0.7], [0.5, 1], [0.2, 0.1]])
P[-1, ] = P[0, ]

# pick a side between, say the one between vertices P[i,] and P[i+1,],
# and assume you want to make it of length a = 3:
i=2
a=3 
# pick a point V0 in the plane, where a vertex P[j,] of the scaled polygon should be places:
j=1
v0 = [1,1] 

# calculate the scale, which is a / (length of edge P[i,] P[i+1,]):
scale = a / np.linalg.norm(P[i+1, ] - P[i,])

# scale and position the polygon where you want it to be:
P_scaled = scale * P  +  (v0 - P[j,])

# plot polygon P: 
plt.figure()
plt.plot(P[:,0], P[:,1]) 
for k in range(P.shape[0]):
  plt.plot(P[k,0], P[k,1], 'ro')

# plot polygon P_scaled:
plt.plot(P_scaled[:,0], P_scaled[:,1]) 
for k in range(P_scaled.shape[0]):
  plt.plot(P_scaled[k,0], P_scaled[k,1], 'ro')
  
axx = plt.gca()
axx.set_aspect('equal')
plt.show()