【问题标题】:How to extend a line segment in Python?如何在 Python 中延长线段?
【发布时间】:2013-10-12 16:55:24
【问题描述】:

我有一条线段定义为它的起点和终点。

L = [(x1, y1), (x2, y2)] 

所以

               (x1, y1)                       (x2, y2)
L:                A-------------------------------B

我现在希望通过像这样拉开这两点来延长线

              a                                         a
L:      A'--------A-------------------------------B-----------B'

所以我需要更新点AB的坐标。

假设A'A = B'B = a

如何在 Python 中做到这一点?

This question 可能比较相关,但我的主要关注做任务的算法,而不是在图中可视化。

【问题讨论】:

  • 你在 python 中做它就像在任何其他语言中一样 - 通过计算翻译并添加到坐标,这甚至不接近编程问题
  • 我认为这是一道数学题,而不是 python 题。

标签: algorithm


【解决方案1】:

使用向量数学:

B = A + v
where
   v = B - A = (x2-x1, y2-y1)
   ||v|| = sqrt((x2-x1)^2 + (y2-y1)^2)

The normalized vector v^ with ||v^|| = 1 is: v^ = v / ||v||

To get the values of A' and B' you can now use the direction
of v^ and the length of a:
   B' = B + a * v^
   A' = A - a * v^

【讨论】:

    猜你喜欢
    • 2019-02-28
    • 2022-01-24
    • 2016-03-05
    • 1970-01-01
    • 2018-04-19
    • 2012-04-14
    • 2011-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多