数学计算来检索两点之间的角度? [英] Math Calculation to retrieve angle between two points?

查看:28
本文介绍了数学计算来检索两点之间的角度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
如何计算相对于水平轴的两点?

我一直在寻找这个,这真的让我很烦,所以我决定问问......

I've been looking for this for ages and it's just really annoying me so I've decided to just ask...

假设我有两个点(即 x1, y1 和 x2, y2),我想计算这两个点之间的角度,假设当 y1 == y2 且 x1 > x2 时,角度是 180 度...

Provided I have two points (namely x1, y1, and x2, y2), I would like to calculate the angle between these two points, presuming that when y1 == y2 and x1 > x2 the angle is 180 degrees...

我一直在使用以下代码(使用高中的知识),但我似乎无法产生所需的结果.

I have the below code that I have been working with (using knowledge from high school) and I just can't seem to produce the desired result.

float xDiff = x1 - x2;
float yDiff = y1 - y2;
return (float)Math.Atan2(yDiff, xDiff) * (float)(180 / Math.PI);

提前致谢,我很沮丧...

Thanks in advance, I'm getting so frustrated...

推荐答案

根据我收集的信息,您希望以下内容:

From what I've gathered, you want the following to hold:

  • 水平线:P1 -------- P2 => 0°
  • 水平线:P2 -------- P1 => 180°
  • Horizontal line: P1 -------- P2 => 0°
  • Horizontal line: P2 -------- P1 => 180°

你说,你想让角度顺时针增加.

You said, you want the angle to increase in clockwise direction.

旋转这条线 P1 -------- P2 使得 P1 高于 P2,因此角度必须为 90°.

Rotating this line P1 -------- P2 such that P1 is above P2, the angle must thus be 90°.

然而,如果我们向相反的方向旋转,P1 将低于 P2 并且角度为 -90° 或 270°.

If, however, we rotated in the opposite direction, P1 would be below P2 and the angle is -90° or 270°.

Basis:以P1为原点,测量P2相对于原点的角度,然后P1——------ P2 将正确产生 0.

Basis: Considering P1 to be the origin and measuring the angle of P2 relative to the origin, then P1 -------- P2 will correctly yield 0.

float xDiff = x2 - x1;
float yDiff = y2 - y1;
return Math.Atan2(yDiff, xDiff) * 180.0 / Math.PI;

然而,atan2 让我们在逆时针方向增加角度.绕原点逆时针方向旋转,y 经历以下值:

However, atan2 let's the angle increase in CCW direction. Rotating in CCW direction around the origin, y goes through the following values:

  • y = 0
  • y > 0
  • y = 0
  • y <0
  • y = 0

这意味着,我们可以简单地反转 y 的符号来反转方向.但是因为C#的坐标是从上到下递增的,所以在计算yDiff时,符号已经反转了.

This means, that we can simply invert the sign of y to flip the direction. But because C#'s coordinates increase from top to bottom, the sign is already reversed when computing yDiff.

这篇关于数学计算来检索两点之间的角度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆