从x,y坐标检测左转或右转的算法 [英] Algorithm to detect left or right turn from x,y co-ordinates

查看:453
本文介绍了从x,y坐标检测左转或右转的算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从原点开始每秒记录的x,y坐标数据集。我可以检测距离,速度,加速度,位移模量。是否有任何算法可以检测到是左转还是右转?

I have a data set of x,y co-ordinates, starting from origin, recorded each second. I can detect distance, speed,acceleration, modulus of displacement . Is there any algorithm to detect whether a left or right turn ?

如果位移大约等于距离,我目前每10秒计算一次位移的距离和模数,那么车辆就在笔直的路径上,但是值发生变化,然后涉及转弯。

I am currently calculating distance and modulus of displacement for every 10 seconds, if the displacement is approximately equal to distance, then the vehicle is on straight path, but of the values change then there is a turn involved.

是否有一种算法来确定转弯是左转还是右转?我的数据看起来像这样

IS there an algorithm to decide whether the turn was left or right ? My data looks like this

Time   x     y
 0     0     0
 1    -0.2  -0.1
 2    -0.7   0.9
 3    -0.8   0.9
 4    -1     0.8
 5    -1.1   0.8
 6    -1.2   0.7
 7    -1.4   0.7
 8    -1.9   1.7
 9    -2     1.7
10    -2.2   1.6
11    -2.3   1.6
12    -2.5   1.5
13    -2.6   1.5
14    -2.7   1.5
15    -2.9   1.4
16    -3.6   1.2
17    -4.1  -0.1
18    -4.7  -1.5
19    -4.7  -2.6
20    -4.3  -3.7
21    -4.3  -3.7
22    -4.7  -3.8
23    -6.2  -3.1
24    -9.9  -1.9
25   -13.7  -1.9
26   -17.9  -2
27   -21.8  -0.8
28   -25.1  -0.6
29   -28.6   1.8


推荐答案

看3点 p0 p1 p2 ,您可以查看两个向量 p1-p0 p2-p1 。一种简单的方法是计算两个向量之间的叉积。叉积的x和y分量为0,因为两个向量都在xy平面上。因此,只需要计算叉积的z分量。

Looking at 3 points p0, p1 and p2, you can look at the relative orientation of the two vectors p1 - p0 and p2 - p1. An easy way to do this is to calculate the cross product between the two vectors. The x- and y-components of the cross product are 0 because both vectors are in the xy-plane. So only the z-component of the cross product needs to be calculated.

如果叉积的z分量为正,则您知道第二个矢量指向左相对于第一个向量,因为第一个向量,第二个向量和z轴正方向的向量都是右旋的。如果叉积为负,则第二个向量指向相对于第一个向量的右边。

If the z-component of the cross product is positive, you know that the second vector points left relative to the first one, because the first vector, second vector, and a vector in the positive z-direction are right handed. If the cross product is negative, the second vector points to the right relative to the first one.

我使用了疯狂的Python技巧(我大约每年使用一次Python。 ..)放入下面的代码中。有一点逻辑,以便左/右指定可以打印在中间点,即使只能在读取下一个点之后才能计算出来。为此,之前的几行内容被保存下来,并且打印延迟。实际计算在 calcDir()函数中。

I used my mad Python skills (I use Python about once a year...) to put this into the code below. There's a little logic so that the Left/Right designation can be printed at the middle point, even though it can only be calculated after the next point was read. To enable that, a couple of previous lines are saved away, with their printing delayed. The actual calculation is in the calcDir() function.

import sys

fileName = sys.argv[1]
dataFile = open(fileName, 'r')

def calcDir(p0, p1, p2):
    v1x = float(p1[0]) - float(p0[0])
    v1y = float(p1[1]) - float(p0[1])
    v2x = float(p2[0]) - float(p1[0])
    v2y = float(p2[1]) - float(p1[1])
    if v1x * v2y - v1y * v2x > 0.0:
        return 'Left'
    else:
        return 'Right'

lineIdx = 0
for line in dataFile:
    line = line.rstrip()
    lineIdx += 1
    if lineIdx == 1:
        print line
    elif lineIdx == 2:
        line0 = line
        print line0
    elif lineIdx == 3:
        line1 = line
    else:
        line2 = line
        dir = calcDir(line0.split()[1:], line1.split()[1:], line2.split()[1:])
        print line1 + ' ' + dir
        line0 = line1
        line1 = line2

print line2

这篇关于从x,y坐标检测左转或右转的算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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