简体   繁体   English

求Python中的一条线段与y轴的夹角

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

Ive been trying to figure this out but I just can quite get it right.我一直在努力解决这个问题,但我完全可以做到。 I have the following coordinate plane.我有以下坐标平面。

If I have a segment coming off of the origin like either of the two segments shown below, how can I find that angle between the segment and the Y axis?如果我有一个像下面所示的两个线段中的任何一个那样从原点离开的线段,我怎样才能找到线段和 Y 轴之间的角度? The only input I have is the endpoint (x, y) of one of the segments我唯一的输入是其中一个线段的端点 (x, y)

I will not have two segments in the plane at the same time though, so finding the total angle between the two segments will not work.我不会同时在平面上有两个部分,所以找到两个部分之间的总角度是行不通的。 I will only ever have one segment on the coordinate plane at one time.我一次只会在坐标平面上有一个线段。

在此处输入图像描述

You need the atan2 function from the math module:您需要数学模块中的 atan2 function:

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

Do you know the lengths of any of the sides?你知道任何边的长度吗? If so, you could use the known angle of 90 degrees (because those are right triangles) to deduce the unknown angle.如果是这样,您可以使用已知的 90 度角(因为它们是直角三角形)来推导出未知角。 This could be done with the Law of Sin or Law of Cos depending on the information known.根据已知信息,这可以通过Sin法则或Cos法则来完成。

You need to take the inverse tangent of x ÷ y and import the math module.您需要取x ÷ y的反正切值并导入math模块。 In Python, it would look like this:在 Python 中,它看起来像这样:

import math

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM