【问题标题】:How can I scale the length of a line and obtain the corresponding co-ordinates ((x1, y1), (x2, y2)) in Python?如何在Python中缩放一条线的长度并获得相应的坐标((x1,y1),(x2,y2))?
【发布时间】:2022-12-21 00:35:47
【问题描述】:

我有一条线,有两组坐标 (x1, y1) 和 (x2, y2) 对应于点 A 和 B。 我可以使用以下方法计算这两点之间的欧氏距离(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.

【问题讨论】:

    标签: python numpy vector 2d line


    【解决方案1】:

    为此,你必须像这样用中点来做。

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-02
      • 1970-01-01
      • 2017-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多