How do I calculate angle from two coordinates?

2019-05-26 03:36发布

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);

        }
    }

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.

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.

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.

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

标签: c# geometry
2条回答
等我变得足够好
2楼-- · 2019-05-26 03:42

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));
查看更多
仙女界的扛把子
3楼-- · 2019-05-26 03:51

Regarding 180, that's true. I get used to have an extension class like this for working with radians and degrees.

public static class Extension
{

    public static double ToRadians(this double degree)
    {
        return degree * Math.PI / 180;
    }

    public static double ToDegrees(this double val)
    {
        return val * 180 / Math.PI;
    }
}

Regarding sine and cosine (I'm not sure I understood evetything right) but if I use the code below

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

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

float r = (float) Math.Pow((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1), 0.5);
float cosine = (x2 - x1) /r;
float sine = (y2 - y1) /r;

I'll get the angle 0.5712978 (not equal to 90).

Sorry if I misunderstood the problem.

查看更多
登录 后发表回答