如何在MatLab中计算两个2D向量之间的方向角? [英] How to compute directional angle between two 2D vectors in MatLab?

查看:1316
本文介绍了如何在MatLab中计算两个2D向量之间的方向角?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一个脚本来分析我的一些数据.我有三个点(p1,p2,p3)的位置信息.我想从矢量p1p2中找到点p3的角位移,如下图:

p3a,p3b,p3c,p3d显示了p3的可能相对位置.如图所示,我希望输出角度的符号描述它相对于向量p1p2的相对位置.

我正在使用的代码如下(适应该图):

v1 = p2 - p1;
x1 = v1(1);
y1 = v1(2);
v2 =  p1 - p3;
x2 = v2(1);
y2 = v2(2);
angle = atan2d(x1*y2-y1*2,x1*x2+y1*y2);

当p3在p3a处时,这可以按期望的方式工作,从而给出正确大小的负角(-77度).但是,当p3处于p3d时,它将输出一个较大的正角(+150度),而不是所需的较大的负角.

解决方案

首先,考虑带有坐标的两个2D向量之间的角度的一种简单方法是将轴与坐标向量对齐并考虑它们之间的关系.两个向量.使用下图,我们可以看到可以通过从另一个角度减去一个角度来找到相对角度.

来源: http://almaer.com/blog/uploads/atan2.png

弄清楚我们可以说的这张图并不难

angle = atan2d(y2,x2) - atan2d(y1,x1)

但是,由于您的向量都不沿坐标轴对齐,因此可能会出现上述差异不在(-180,180)范围之内的情况.这意味着我们需要进行编码以增加或减少360度以获得所需的角度:

if abs(angle) > 180
  angle = angle - 360*sign(angle)
end

请注意,您使用的是一种反向符号(CW正号),因此最终代码如下:

v1 = p1 - p2;
x1 = v1(1);
y1 = v1(2);
v2 = p3 - p1;
x2 = v2(1);
y2 = v2(2);
angle = atan2d(y1,x1) - atan2d(y2,x2)

if abs(angle) > 180
    angle = angle - 360*sign(angle)
end

v1和v2进行了更改以匹配您的图形.

I am trying to implement a script to analyze some of my data. I have position information for three points (p1,p2,p3). I would like to find the angular displacement of point p3 from the vector p1p2, as in this picture:

p3a, p3b, p3c, p3d show possible relative positions for p3. As shown, I would like the sign of the output angle to describe its relative position to vector p1p2.

The code I am using is as follows (adapted to the diagram):

v1 = p2 - p1;
x1 = v1(1);
y1 = v1(2);
v2 =  p1 - p3;
x2 = v2(1);
y2 = v2(2);
angle = atan2d(x1*y2-y1*2,x1*x2+y1*y2);

This works as desired when p3 is at p3a, giving a negative angle of the right size (-77 degrees). However, when p3 is at p3d, it outputs a large positive angle (+150 degrees), as opposed to the desired large negative angle.

解决方案

To start with, an easier way to think about the angle between two 2D vectors with coordinates is to align an axis with your coordinate vectors and think about the relationship between two vectors. Using the drawing below, we can see that a relative angle can be found by subtracting one angle from the other.

Source: http://almaer.com/blog/uploads/atan2.png

It is not too hard to figure out looking at this graph that we can say

angle = atan2d(y2,x2) - atan2d(y1,x1)

However, since neither of your vectors are known to be aligned along the coordinate axis, the case can arise where the difference above is not in the range (-180, 180). This means we need to code in a check to add or subtract 360 degrees to get our desired angle:

if abs(angle) > 180
  angle = angle - 360*sign(angle)
end

Note, you are using a kind of reverse notation (CW positive) so the final code would look like:

v1 = p1 - p2;
x1 = v1(1);
y1 = v1(2);
v2 = p3 - p1;
x2 = v2(1);
y2 = v2(2);
angle = atan2d(y1,x1) - atan2d(y2,x2)

if abs(angle) > 180
    angle = angle - 360*sign(angle)
end

Where v1 and v2 have been changed to match your drawing.

这篇关于如何在MatLab中计算两个2D向量之间的方向角?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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