在 Python 中找到线段与 y 轴之间的角度

Find the angle between a line segment and the y axis in Python

我一直在想办法解决这个问题,但我总能搞定。我有以下坐标平面。

如果我有一个像下面所示的两个线段中的任何一个那样从原点离开的线段,我怎样才能找到线段和 Y 轴之间的角度?我唯一的输入是其中一个段的端点 (x, y)

虽然我不会同时在平面上有两个线段,所以求出这两个线段之间的总角度是行不通的。我一次只会在坐标平面上有一个线段。

你知道任何一条边的长度吗?如果是这样,您可以使用已知的 90 度角(因为它们是直角三角形)来推导出未知角。根据已知的信息,这可以用 Law of Sin or Law of Cos 来完成。

您需要数学模块中的 atan2 函数:

from math import atan2,pi

atan2(y,x)*180/pi # in degrees, positive angles are counter clockwise

for x in range (-1,2):
    for y in range(-1,2):
        print((x,y),atan2(y,x)*180/pi)

(-1, -1) -135.0
(-1, 0)   180.0
(-1, 1)   135.0
(0, -1)   -90.0
(0, 0)      0.0
(0, 1)     90.0
(1, -1)   -45.0
(1, 0)      0.0
(1, 1)     45.0

您需要取x ÷ y的反正切并导入math模块。在 Python 中,它看起来像这样:

import math

theta = math.degrees(math.atan(x / y)) # Also convert the answer from radians to degrees