Answers:
首先找到起点和终点之间的差异(在这里,这更多的是有向线段,而不是“线”,因为线无限延伸且不在特定点处开始)。
deltaY = P2_y - P1_y
deltaX = P2_x - P1_x
然后计算角度(从的正X轴P1
到的正Y轴延伸P1
)。
angleInDegrees = arctan(deltaY / deltaX) * 180 / PI
但是arctan
可能并不理想,因为用这种方式划分差异将消除区分角度所在象限所需的区分(请参见下文)。如果您的语言包含atan2
函数,请改用以下代码:
angleInDegrees = atan2(deltaY, deltaX) * 180 / PI
编辑(2017年2月22日):但是,总的来说,打电话atan2(deltaY,deltaX)
只是为了获得适当的角度cos
,sin
可能不太优雅。在这种情况下,通常可以执行以下操作:
(deltaX, deltaY)
作为载体。deltaX
和deltaY
由向量的长度(sqrt(deltaX*deltaX+deltaY*deltaY)
),除非长度为0。deltaX
现在将是矢量和水平轴之间的角度(在处从正X轴到正Y轴的方向P1
)的余弦值。deltaY
现在将是角度的正弦值。编辑(2017年2月28日):即使未规范化(deltaX, deltaY)
:
deltaX
将告诉您步骤3中描述的余弦是正还是负。deltaY
将告诉您步骤4中描述的正弦是正还是负。deltaX
,并deltaY
会告诉你哪个象限的角度是,相对于在正X轴P1
:
+deltaX
,+deltaY
:0至90度。-deltaX
,+deltaY
:90至180度。-deltaX
,-deltaY
180〜270度(-180到-90度)。+deltaX
,-deltaY
:270至360度(-90至0度)。使用弧度的Python实现(2015年7月19日,由Eric Leschinski提供,他编辑了我的答案):
from math import *
def angle_trunc(a):
while a < 0.0:
a += pi * 2
return a
def getAngleBetweenPoints(x_orig, y_orig, x_landmark, y_landmark):
deltaY = y_landmark - y_orig
deltaX = x_landmark - x_orig
return angle_trunc(atan2(deltaY, deltaX))
angle = getAngleBetweenPoints(5, 2, 1,4)
assert angle >= 0, "angle must be >= 0"
angle = getAngleBetweenPoints(1, 1, 2, 1)
assert angle == 0, "expecting angle to be 0"
angle = getAngleBetweenPoints(2, 1, 1, 1)
assert abs(pi - angle) <= 0.01, "expecting angle to be pi, it is: " + str(angle)
angle = getAngleBetweenPoints(2, 1, 2, 3)
assert abs(angle - pi/2) <= 0.01, "expecting angle to be pi/2, it is: " + str(angle)
angle = getAngleBetweenPoints(2, 1, 2, 0)
assert abs(angle - (pi+pi/2)) <= 0.01, "expecting angle to be pi+pi/2, it is: " + str(angle)
angle = getAngleBetweenPoints(1, 1, 2, 2)
assert abs(angle - (pi/4)) <= 0.01, "expecting angle to be pi/4, it is: " + str(angle)
angle = getAngleBetweenPoints(-1, -1, -2, -2)
assert abs(angle - (pi+pi/4)) <= 0.01, "expecting angle to be pi+pi/4, it is: " + str(angle)
angle = getAngleBetweenPoints(-1, -1, -1, 2)
assert abs(angle - (pi/2)) <= 0.01, "expecting angle to be pi/2, it is: " + str(angle)
90 - angleInDegrees
?
抱歉,但是我很确定Peter的回答是错误的。请注意,y轴位于页面下方(在图形中常见)。因此,必须逆转deltaY计算,否则您将得到错误的答案。
考虑:
System.out.println (Math.toDegrees(Math.atan2(1,1)));
System.out.println (Math.toDegrees(Math.atan2(-1,1)));
System.out.println (Math.toDegrees(Math.atan2(1,-1)));
System.out.println (Math.toDegrees(Math.atan2(-1,-1)));
给
45.0
-45.0
135.0
-135.0
因此,如果在上面的示例中P1为(1,1),P2为(2,2)[因为Y向下扩展页面],则上面的代码将为所示示例给出45.0度,这是错误的。更改deltaY计算的顺序,即可正常工作。
double arc = Math.atan2(mouse.y - obj.getPy(), mouse.x - obj.getPx()); degrees = Math.toDegrees(arc); if (degrees < 0) degrees += 360; else if (degrees > 360) degrees -= 360;
我已经找到了运行良好的Python解决方案!
from math import atan2,degrees
def GetAngleOfLineBetweenTwoPoints(p1, p2):
return degrees(atan2(p2 - p1, 1))
print GetAngleOfLineBetweenTwoPoints(1,3)
考虑到确切的问题,将我们置于一个“特殊”坐标系中,其中正轴表示向下移动(例如屏幕或界面视图),您需要像这样调整该功能,而将Y坐标设为负:
Swift 2.0中的示例
func angle_between_two_points(pa:CGPoint,pb:CGPoint)->Double{
let deltaY:Double = (Double(-pb.y) - Double(-pa.y))
let deltaX:Double = (Double(pb.x) - Double(pa.x))
var a = atan2(deltaY,deltaX)
while a < 0.0 {
a = a + M_PI*2
}
return a
}
此功能可为问题提供正确答案。答案以弧度为单位,因此,以度为单位查看角度的用法是:
let p1 = CGPoint(x: 1.5, y: 2) //estimated coords of p1 in question
let p2 = CGPoint(x: 2, y : 3) //estimated coords of p2 in question
print(angle_between_two_points(p1, pb: p2) / (M_PI/180))
//returns 296.56
deltaY = Math.Abs(P2.y - P1.y);
deltaX = Math.Abs(P2.x - P1.x);
angleInDegrees = Math.atan2(deltaY, deltaX) * 180 / PI
if(p2.y > p1.y) // Second point is lower than first, angle goes down (180-360)
{
if(p2.x < p1.x)//Second point is to the left of first (180-270)
angleInDegrees += 180;
else //(270-360)
angleInDegrees += 270;
}
else if (p2.x < p1.x) //Second point is top left of first (90-180)
angleInDegrees += 90;
import math
from collections import namedtuple
Point = namedtuple("Point", ["x", "y"])
def get_angle(p1: Point, p2: Point) -> float:
"""Get the angle of this line with the horizontal axis."""
dx = p2.x - p1.x
dy = p2.y - p1.y
theta = math.atan2(dy, dx)
angle = math.degrees(theta) # angle is in (-180, 180]
if angle < 0:
angle = 360 + angle
return angle
为了进行测试,我让假设生成了测试案例。
import hypothesis.strategies as s
from hypothesis import given
@given(s.floats(min_value=0.0, max_value=360.0))
def test_angle(angle: float):
epsilon = 0.0001
x = math.cos(math.radians(angle))
y = math.sin(math.radians(angle))
p1 = Point(0, 0)
p2 = Point(x, y)
assert abs(get_angle(p1, p2) - angle) < epsilon