如何将 atan2() 映射到 0-360 度 [英] How to map atan2() to degrees 0-360

查看:41
本文介绍了如何将 atan2() 映射到 0-360 度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

atan2(y, x) 在 180° 处具有不连续性,在顺时针方向切换到 -180°..0°.

atan2(y, x) has that discontinuity at 180° where it switches to -180°..0° going clockwise.

如何将值的范围映射到 0°..360°?

How do I map the range of values to 0°..360°?

这是我的代码:

CGSize deltaPoint = CGSizeMake(endPoint.x - startPoint.x, endPoint.y - startPoint.y);
float swipeBearing = atan2f(deltaPoint.height, deltaPoint.width);

我正在计算给定 startPointendPoint 两个 XY 点结构的滑动触摸事件的方向.该代码适用于 iPhone,但任何支持 atan2f() 的语言都可以.

I'm calculating the direction of a swiping touch event given the startPoint and endPoint, both XY point structs. The code is for the iPhone but any language that supports atan2f() will do.

推荐答案

(x > 0 ? x : (2*PI + x)) * 360 / (2*PI)

这篇关于如何将 atan2() 映射到 0-360 度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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