[英]Calculate point/coordinate from distance and bearing
Maths is not my strong suit and I think I have something mixed up here but I cannot figure out what. 数学不是我的强项,我认为我在这里有些困惑,但我不知道是什么。
I'm just trying to populate 2 new coordinates given a number of variables and constants. 我只是想在给定许多变量和常量的情况下填充2个新坐标。
if I make Origin coordinate 5,5 and Destination coordinate 10,5, I can work out that distance =5 and that the bearing from Origin to Destination is 90 using these two functions: 如果我将原点坐标设为5,5,将目标坐标设为10,5,则可以使用以下两个函数计算出距离= 5,并且从原点到目标的方位角为90:
private static double GetDistance(PointF point1, PointF point2)
{
double a = (double)(point2.X - point1.X);
double b = (double)(point2.Y - point1.Y);
return Math.Sqrt(a * a + b * b);
}
public static double GetBearing(PointF coord1, PointF coord2)
{
double result = 0.0;
result = Math.Atan2(coord2.X - coord1.X, coord2.Y - coord1.Y) * (180 / Math.PI); //- Math.Atan2(coord4.y - coord3.y, coord4.x - coord3.x))
if (result < 0)
{
result = result + 360;
}
return result;
}
What I want to be able to do given an offset Distance of xd=1 and an offset bearing of 180(ie directly opposite direction to the destination) is plot the location 4,5. 给定xd = 1的偏移距离和180的偏移方位(即与目的地直接相反的方向),我想要做的是绘制位置4,5。 I'd also like to be able to feed a different offset bearing in of say 90 and plot 5,6. 我还希望能够输入不同的偏心轴承,比如说90和图5,6。
Here's what I've tried but I get completely nonsensical values. 这是我尝试过的方法,但我得到的完全是荒谬的价值观。
public static PointF CalculateCoordinate(double Angle, double Distance)
{
PointF coord = new PointF(Convert.ToSingle(Distance * Math.Cos(Angle)), Convert.ToSingle(Distance * Math.Sin(Angle)));
return coord;
}
and CalculateCoordinate(GetBearing(Destination, Origin),1)
to reverse the bearing directly 180. I've tried this CalculateCoordinate(90,1)
to calculate an offset to the side but that's not working either. 和CalculateCoordinate(GetBearing(Destination, Origin),1)
直接将轴承反向CalculateCoordinate(GetBearing(Destination, Origin),1)
180。我已经尝试过CalculateCoordinate(90,1)
来计算侧面的偏移量,但这也不起作用。
Where have I gone wrong, I'm sure it's something pretty stupid and simple. 我哪里出错了,我敢肯定这是非常愚蠢和简单的。
There's two mistakes that I can see. 我可以看到两个错误。 First, Atan2 takes the Y value for the first parameter and the X value for the second: 首先,Atan2将第一个参数的Y值和第二个参数的X值:
Math.Atan2(coord2.Y - coord1.Y, coord2.X - coord1.X) * (180 / Math.PI);
Secondly, you're converting from radians to degrees in GetBearing
, but you're not converting Angle from degrees to radians inside CalculateCoordinate
eg: 其次,您要在GetBearing
弧度从度转换为度,但不要在CalculateCoordinate
内部将角度从度转换为弧度,例如:
Math.Cos(Angle * (Math.PI / 180))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.