gpt4 book ai didi

android - 指南针 - 完整 360 度旋转的轨道数

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:00:58 25 4
gpt4 key购买 nike

假设一个人正在使用这个指南针,他们从 90 度开始顺时针或逆时针旋转。记录他们完成了多少次完整的 360 度旋转的最佳方法是什么?假设它们从头到尾只能顺时针或逆时针旋转。

我不断提出解决方案,例如,如果起始方位角为 90 度,我会在传感器数据发生变化时不断检查下一个方位角,如果它一直在一个方向上移动,我就知道它们在旋转。如果他们继续沿该方向旋转并返回 90 度,则算作旋转一圈。我的方法似乎非常复杂且效率低下,我很难想出更好的方法。

在这种情况下,我会期待多次完整轮换。

如果有任何帮助,我将不胜感激。谢谢!

我找到了 this相关的答案,我正在尝试为此整理一个代码示例。如果有人已经做过类似的事情,请发布!

@Override
public void onSensorChanged(SensorEvent event)
{
switch(event.sensor.getType())
{
case Sensor.TYPE_GRAVITY:
{
mValuesAccelerometer = lowPass(event.values.clone(), mValuesAccelerometer);
break;
}
case Sensor.TYPE_MAGNETIC_FIELD:
{
mValuesMagneticField = lowPass(event.values.clone(), mValuesMagneticField);
break;
}
}

boolean success = SensorManager.getRotationMatrix(
mMatrixR,
mMatrixI,
mValuesAccelerometer,
mValuesMagneticField);

if (success)
{
SensorManager.getOrientation(mMatrixR, mMatrixValues);

float azimuth = toDegrees(mMatrixValues[0]);
float pitch = toDegrees(mMatrixValues[1]);
float roll = toDegrees(mMatrixValues[2]);

if (azimuth < 0.0d)
{
//The bearing in degrees
azimuth += 360.0d;
}
}
}

最佳答案

如果您确定它们只会朝一个方向移动,为了优化您的代码,您可以设置度数检查点,而不是持续监控它们是否仍在朝正确的方向移动。

这是一个粗略的算法来做到这一点

//You noted 90 degree as starting point
// checkpoint 1 will be 180 keep it as a boolean
// now you've reached 180 if the meter gets to 180 before going to next checkpoint
// which is 270 then make 180 false. it means they turned back.
// if they make it to 270 then wait for 0 degrees and do the same.
// if they make it back to 90 like that. You got a rotation and hopefully
// a bit of complexity is reduced as you're just checking for 4 checkpoints

我手边没有任何代码。

关于android - 指南针 - 完整 360 度旋转的轨道数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44489934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com