是由水平线大于45度的两个点形成的线

Sco*_*ain 2 c# math

我试图找出由两个点定义的线是否大于或等于90度与水平线相比.这是我使用的代码

bool moreThan90 = false;
double angle = Math.Atan((double)(EndingLocation.Y - Location.Y) / (double)(EndingLocation.X - Location.X));
if (angle >= Math.PI / 2.0 || angle <= -Math.PI / 2.0)
    moreThan90 = true;
Run Code Online (Sandbox Code Playgroud)

我是否正确地执行了此操作,或者.Net中是否有更好的内置函数可以找到它?

编辑 - 实际上我搞砸了我的问题,我想说水平而不是90.然而答案让我到了一个可以弄明白的地方(真的我只需要指向Atan2).

phe*_*cks 7

与水平方向成90度以上的线将使其EndLocation.x的x值小于Location.x.

所以你不需要所有的废话,这应该足够了:

if (EndingLocation.X < Location.X)
    moreThan90 = true;
Run Code Online (Sandbox Code Playgroud)

编辑:

似乎OP意味着45度而不是90度,这意味着上述简化不再成立.为此,使用atan2可能会更好(正如Slaks指出的那样)但是本着不使用tan的精神:

if (Math.Abs(EndingLocation.X - Location.X) > Math.Abs(EndingLocation.Y - Location.Y) && 
    EndingLocation.X < Location.X)
    moreThan45 = true;
Run Code Online (Sandbox Code Playgroud)

请注意,如果您只想要指向右侧的线,则只需要进行第二次检查