我正在开发一个基于 3D 对象的项目,并通过我的程序操作它们。我目前有一个文本框,允许我输入以度为单位的标题和一个按钮,该按钮将计算使我的主要对象更改其标题所需的值。这是该函数的代码:
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 度或正东。
该程序要求标题以两个单独的值(tempCosine 和 tempSine)给出,我对几何不太熟悉,无法理解为什么我要乘以 360 而不是 180,但这就是它的工作方式。
现在,我的项目的下一部分涉及使我的主要对象面对另一个对象(给定两个对象的 (x,y) 坐标)。例如,如果我的主要对象位于 (9112.94, 22088.74),而我想要面对的新对象位于 (9127.04, 22088.88),则需要几乎完全 90 度的航向才能使其面向新对象。
如何从这两个坐标计算 tempCosine 和 tempSine?
对于180来说,确实如此。我习惯了像这样的扩展类来处理弧度和度数。
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;
}
}
关于正弦和余弦(我不确定我是否理解正确),但如果我使用下面的代码
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;
我将得到角度 0.5712978(不等于 90)。
抱歉,如果我误解了这个问题。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)