numpy 在Python中,如何缩放一条线的长度并获得相应的坐标((x1,y1),(x2,y2))?

abithluo  于 2022-12-23  发布在  Python
关注(0)|答案(1)|浏览(165)

我有一条线,该线具有对应于点A和B的两组坐标(x1,y1)和(x2,y2)。我可以使用下式计算这两个点之间的欧几里得距离(L2范数):

point_a = (189, 45)
point_b = (387, 614)
line= (point_a, point_b)
point_array = np.array(line)
distance = np.linalg.norm(point_array)
print('Euclidean distance = ', distance)```

How is it possible to obtain the co-ordinates for the line scaled about it's midpoint?
i.e. I would like to scale the length of the line but keep the angle.
x4shl7ld

x4shl7ld1#

为此,你必须这样做的中点。

import numpy as np

# Define the two points as a NumPy array
points = np.array([[189, 45], [387, 614]])

# Calculate the midpoint of the line
midpoint = points.mean(axis=0)

# Calculate the scaling factor
scale_factor = 2

# Scale the coordinates of the two points about the midpoint
scaled_points = midpoint + (points - midpoint) * scale_factor

# Print the scaled points
print(scaled_points)

相关问题