【发布时间】:2013-09-17 10:17:19
【问题描述】:
我成功地检测到手机绕轴旋转 0-360 度(滚动),但现在我很难设计一种有效的算法来检测一整圈。我的工作,但我认为没有我想要的优雅和有效的算法是:
private boolean detectRoll;
private boolean[] checkpointsR = new boolean[4];
private boolean fullRollTurn;
public void detectingRoll() {
setDetectRoll(true);
checkpointsR[0] = true;
for (int i = 1; i < 4; i++) {
if (roll > 90 * i && roll < 90 * (i + 1)
&& checkpointsR[i - 1] == true) {
checkpointsR[i] = true;
}
}
if (areAllTrue(checkpointsR) && roll > 0 && roll < 45) {
fullRollTurn = true;
// reset rollCheckpoints
for (int i = 1; i < 4; i++) {
checkpointsR[i] = false;
}
}
}
public static boolean areAllTrue(boolean[] array) {
for (boolean b : array)
if (!b)
return false;
return true;
}
public void setDetectRoll(boolean detectRoll) {
this.detectRoll = detectRoll;
}
任何帮助将不胜感激。
【问题讨论】:
-
此代码具有恒定的时间复杂度 (O(1)),因为无论发生什么情况,您都只会遍历 4 个检查点,所以除非您进行了一些分析告诉您这是一个瓶颈,否则我认为收益优化这一点可以忽略不计。
-
感谢您的回复。我正在寻找任何新的建议,而不是优化我的算法,因为我知道它不是应该的。
-
如果它工作正常,但您不想优化,您想要什么做? “建议”范围很广,很可能会被关闭。
-
问题说它在全 360 度转弯时还没有效果。不过,期望是为什么你不能把更小的转弯加起来?
标签: java android algorithm rotation accelerometer