绘制 xy 平面上两点之间的航向

发布于 2024-11-26 21:26:23 字数 771 浏览 0 评论 0原文

我正在尝试实现一个小型雷达,它根据纬度和经度坐标绘制目标,类似于 Layar AR iPhone 应用程序中的雷达。我有指南针和位置管理器来获取纬度/经度、航向和两点之间的距离。但是我在将点绘制到 xy 平面上时遇到问题。你能指出我正确的方向吗(可以这么说)?

这是我用来绘图的方法,但结果不正确:

-(void) addTargetIndicatorWithHeading:(float)heading andDistance:(float)distance{
    //draw target indicators
    //need to convert radians and distance to cartesian coordinates
    float radius = 50;
    float x0 = 0.0; 
    float y0 = 0.0;

    //convert heading from radians to degrees
    float angle = heading * (180/M_PI);

    //x-y coordinates
    float x1 = (x0 + radius * sin(angle)); 
    float y1 = (y0 + radius * cos(angle)); 

    TargetIndicator *ti = [[TargetIndicator alloc] initWithFrame:CGRectMake(x1, y1, 5, 5)];
    [self addSubview:ti];
    [ti release];

}

I'm trying to implement a small radar that plots targets based on latitude and longitude coordinates similar to the radar in the Layar AR iPhone app. I have the compass and locationManager working to get the lat/lon, heading and distance between two points. However I'm having trouble plotting the points onto the x-y plane. Could you point me in the right direction(so-to-speak)?

This is the method that I am using to plot but the results are not correct:

-(void) addTargetIndicatorWithHeading:(float)heading andDistance:(float)distance{
    //draw target indicators
    //need to convert radians and distance to cartesian coordinates
    float radius = 50;
    float x0 = 0.0; 
    float y0 = 0.0;

    //convert heading from radians to degrees
    float angle = heading * (180/M_PI);

    //x-y coordinates
    float x1 = (x0 + radius * sin(angle)); 
    float y1 = (y0 + radius * cos(angle)); 

    TargetIndicator *ti = [[TargetIndicator alloc] initWithFrame:CGRectMake(x1, y1, 5, 5)];
    [self addSubview:ti];
    [ti release];

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

墟烟 2024-12-03 21:26:23

我猜问题在于当前视图的原点坐标未添加到您的坐标中。
只需通过添加当前视图(将 ti 添加为子视图)的 origin.x 和 origin.y 来修改 x1 和 y1 。

I guess the problem lies within the present view's origin coordinate not being added to ur coordinate.
just modify your x1 and y1 by adding the origin.x and origin.y of the current view to which you add ti as a subview.

分开我的手 2024-12-03 21:26:23

我弄清楚出了什么问题,但我不知道其背后的原因。首先,我不应该将弧度转换为度数。这给了我正确的定位,但它旋转了 180 度。因此,为了解决这个问题,我从 PI 中减去弧度。

这是解决方案:

-(void) addTargetIndicatorWithHeading:(float)heading andDistance:(float)distance{
    //draw target indicators
    //need to convert radians and distance to cartesian coordinates
    float radius = 50;

    //origin offset
    float x0 = 50.0; 
    float y0 = 50.0;

    //convert heading from radians to degrees and rotate by 180 deg
    float angle = M_PI - heading; 

    float x1 = (x0 + radius * sin(angle));   
    float y1 = (y0 + radius * cos(angle)); 

    TargetIndicator *ti = [[TargetIndicator alloc] initWithFrame:CGRectMake(x1, y1, 5, 5)];
    [self addSubview:ti];
    [ti release];


}

I figured out what was wrong but I'm don't know the reasoning behind it. First I should not have been converting the radians to degrees. This gives me the correct positioning but it is rotated 180 degrees. So to fix it, I subtract the radians from PI.

Here is the solution:

-(void) addTargetIndicatorWithHeading:(float)heading andDistance:(float)distance{
    //draw target indicators
    //need to convert radians and distance to cartesian coordinates
    float radius = 50;

    //origin offset
    float x0 = 50.0; 
    float y0 = 50.0;

    //convert heading from radians to degrees and rotate by 180 deg
    float angle = M_PI - heading; 

    float x1 = (x0 + radius * sin(angle));   
    float y1 = (y0 + radius * cos(angle)); 

    TargetIndicator *ti = [[TargetIndicator alloc] initWithFrame:CGRectMake(x1, y1, 5, 5)];
    [self addSubview:ti];
    [ti release];


}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文