如何从两个坐标计算的角度? [英] How do I calculate angle from two coordinates?

查看:436
本文介绍了如何从两个坐标计算的角度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作与3D基于对象的项目,并通过我的程序操纵他们。我现在有一个文本框,让我投入度航向和一个按钮,将计算所需的值,使我的主要对象改变其航向。这是这个函数的代码:

I'm working on a project with 3D based objects and manipulating them via my program. I currently have a textbox that allows me to put a heading in degrees and a button that will calculate the required values to make my main object change its heading. This is the code for that function:

    private void btnSetHeading_Click(object sender, EventArgs e)
    {
        if (this.textBoxHeading.Text.Length == 0)
            return;

        float heading = (float)0;
        try
        {
            heading = float.Parse(this.textBoxHeading.Text);
        }
        catch (FormatException ex)
        {
            MessageBox.Show(ex.Message);
            return;
        }

        if (heading < (float)0 || heading > (float)360)
        {
            MessageBox.Show("Invalid heading parameter. Acceptable range: 0 - 360");
            return;
        }

        float tempCosine = (float)Math.Cos(((heading * Math.PI) / (float)360.0));
        float tempSine = -((float)Math.Sin(((heading * Math.PI) / (float)360.0)));
        try
        {
            ProgramInterop.CreateInstance.SetHeading(tempCosine, tempSine);
        }
        catch (Exception ex)
        {
            MessageBox.Show("Caught: " + ex.Message);

        }
    }

如果我公司供应90作为标题面对,结果是tempCosine = 0.7071068和tempSine = -0.7071068,然后让我的主要对象面对90度或正东。

If I supply 90 as the heading to face, the results are tempCosine=0.7071068 and tempSine=-0.7071068, which then makes my main object face 90 degrees or due east.

该程序需要的标题是在两个不同的值(tempCosine和tempSine),我不太熟悉的几何形状足以给予明白我为什么会用360代替180乘以但这是如何工作的要求。

The program requires the heading to be given in two seperate values(tempCosine and tempSine) which I'm not familiar with geometry enough to understand why I would multiply by 360 instead of 180 but this is how its required to work.

现在为我的项目的下一部分涉及到让我的主要目的,给出面对他们两个(X,Y)的另一目的坐标。例如,如果我的主要目标是在(9112.94,22088.74)和新对象我要面对的是在(9127.04,22088.88),它需要几乎完全90度的航向,使其所面临的新对象。

Now for the next part of my project involves making my main object face another object given both of their (x,y) coordinates. If for example my main object is at (9112.94, 22088.74) and the new object I want to face is at (9127.04, 22088.88), it would require almost exactly 90 degrees heading to make it face the new object.

我如何计算tempCosine和tempSine来自这两个坐标?

How can I calculate the tempCosine and tempSine from those two coordinates?

推荐答案

我是能够用达尼的建议,使用ATAN2制定出答案(Y,X)。感谢丹妮

I was able to work out the answer using Dani's suggestion to use Atan2(y, x). Thanks Dani.

float x1 = 9112.94f;
float y1 = 22088.74f;

float x2 = 9127.04f;
float y2 = 22088.88f;

float angleRadians;

float diffX = x2 - x1;
float diffY = y2 - y1;

float atan2Result = (float)Math.Atan2(diffX, diffY);
angleRadians = atan2Result / 2;
if (angleRadians < 0.0f)
    angleRadians += (float)Math.PI;

float tempCosine = (float)Math.Cos(angleRadians);
float tempSine = -((float)Math.Sin(angleRadians));

这篇关于如何从两个坐标计算的角度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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