答案 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()