检测360度转弯算法 [英] Detect 360 degrees turn algorithm
问题描述
我已经成功检测到手机绕轴旋转0-360度(滚动),但是现在我很难设计一种有效的算法来检测一整圈。我的工作,但我认为不是一种优雅而有效的算法,就是:
I'm successfully detecting 0-360 degrees rotation (roll) of phone around an axis, but now I am having hard times designing an effective algorithm to detect one full turn. My working but I think not elegant and as effective algorithm as I would like is:
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;
}
任何帮助将不胜感激。
推荐答案
好吧,您的代码仅检测到增加 roll
所导致的转弯。在另一个方向上,即由 roll
下降导致的转弯,
Well, your code only detects turns that result from increasing roll
. In the other direction, ie a turn resulting from roll
falling,
if (checkpointsR[i - 1] == true)
{
checkpointsR[i] = true;
}
永远不会触发。
虽然很容易解决,但如果您仅有的传感器输入是 roll
,则固定检查点就是方法总是会出现问题。可视化的最佳方法是检查点是圆上的小红点,并且电话的旋转对应于蚂蚁在圆的边缘上爬行。假设当蚂蚁通过一个点时,它变成绿色,这表示检查点被设置为 true
。如果蚂蚁在两个检查点 A
和 B
之间开始,它可以通过在<$ c $之间爬行来愚弄它们。 c> B ,一直到 A
,然后转身再次朝另一方向前进。所有检查点将变为绿色,但蚂蚁将不会完成一个完整的循环。
While that's easily fixable, if the only sensor input you've got is roll
, the fixed-checkpoint is approach always going to have problems. The best way to visualize it is that your checkpoints are little red points on a circle, and the rotation of the phone corresponds to an ant crawling around the edge of the circle. Suppose that when the ant passes a point, it turns green, and that this represents the checkpoint being set to true
. If the ant starts between two checkpoints A
and B
, it can "fool" them by crawling through B
, all the way round to A
, and then turning around and heading the other way again. All the checkpoints will be green, but the ant won't have completed a full circle.
解决此问题的方法是更改两件事:首先,指定检查点三种状态:未访问
,顺时针
和逆时针
。其次,在检查点上启动蚂蚁。
The way to resolve this is to change two things: first, give the checkpoints three states: unvisited
, clockwise
and anticlockwise
. Second, start the ant on top of a checkpoint.
以下是新规则:
- 每个点都以
未访问
(红点)开头。 - 如果蚂蚁沿顺时针方向通过检查点,请将其状态设置为
顺时针
(从该点开始顺时针指向的绿色小箭头) - 如果蚂蚁在逆时针方向通过了检查点,请将其状态设置为
逆时针(逆时针指向绿色的小箭头)
- 如果所有检查点都是
顺时针
或它们都<$ c $,则表示转弯已经完成c>逆时针。
- Every point starts as
unvisited
(a red point). - If the ant passes a checkpoint in a clockwise direction, set it's state to
clockwise
(a little green arrow pointing clockwise from the point) - If the ant passes a checkpoint in the anticlockwise direction, set it's state to
anticlockwise
(a little green arrow pointing on the anticlockwise direction from the point) - A turn has been completed if all the checkpoints are
clockwise
or if they're allanticlockwise
.
这可以通过三个检查点实现,并将其放置在圆上的任意位置只要第一个在蚂蚁的初始位置以下。我还是建议 {initial,inital + 120,initial + 240}
,但为了对称起见。
This can be achieved with three checkpoints, and placed wherever you want on the circle as long as the first one is under the ant's initial position. I'd recommend {initial, inital + 120, initial + 240}
though for symmetry's sake.
(蚂蚁思维实验可用于两个检查点,但是当您有两个检查点时,问题是存在一对区域,它们之间具有非唯一的检查点,这会使检测蚂蚁实际通过哪个检查点时感到困惑从一个区域到另一个区域)
(The ant thought-experiment works with two checkpoints, but when you've got two checkpoints the problem is there are a pair a regions with a non-unique checkpoint between them, which confuses the detection of which checkpoint the ant actually passed through when the ant goes from one region to the other)
这篇关于检测360度转弯算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!