当我们从V1逆时针走到V2时,如何判断V3是否在V1和V2之间? [英] How to determine whether V3 is between V1 and V2 when we go from V1 to V2 counterclockwise?

查看:34
本文介绍了当我们从V1逆时针走到V2时,如何判断V3是否在V1和V2之间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个向量 V1、V2 和 V3.它们的原点在轴的原点上.当我从V1逆时针移动到V2时,如何确定V3是否在V1和V2之间?

I have three vectors V1, V2, and V3. Their origin points are on the axes' origin. How could I determine whether V3 is between V1 and V2 when I move around counterclockwise from V1 to V2?

替代文字 http://www.freeimagehosting.net/uploads/1448ea8896.jpg

无法通过获取它们的角度并评估这些条件(伪代码)来完成:

It can't be done with obtaining their angles and evaluating these kind of conditions (pseudo-code):

if angle(V3) > angle(V1) && angle(V3) < angle(V2) 
   printf("V3 is between V1 and V2") 
else 
   printf("out of the interval")

要查看它的缺陷,假设 angle 函数给出了 [-pi pi] 范围内的角度.所以,如果angle(V1) = 120 (in degree),angle(V2) = -130 and angle(V3) = 150 那么答案(根据上面的代码)是超出区间",尽管如果你四处走动从V1逆时针到V2,在它们之间.

To see its defect, suppose that the angle function gives angles in the range of [-pi pi]. So, if angle(V1) = 120 (in degree), angle(V2) = -130 and angle(V3) = 150 then the answer (according to the above code) is "out of the interval" although if you move around counterclockwise from V1 to V2, it is between them.

您可能会建议将 2*pi 添加到angle(V2) 或类似的东西,但我已经尝试过这样的事情,但它不起作用.

You may suggest adding 2*pi to angle(V2) or something like that, but I've tried such things and it doesn't work.

我在 MATLAB 中编程.

I'm programming in MATLAB.

编辑 1:它是二维的.

EDIT 1 : it is in 2D.

推荐答案

由于您是在 MATLAB 中执行此操作,因此这里有一个可行的解决方案:

Since you are doing this in MATLAB, here is one solution that should work:

crossProds = [V1(1)*V2(2)-V1(2)*V2(1) ...
              V1(1)*V3(2)-V1(2)*V3(1) ...
              V3(1)*V2(2)-V3(2)*V2(1)];
if (all(crossProds >= 0) || ...
    (crossProds(1) < 0) && ~all(crossProds(2:3) < 0)),
  disp("V3 is between V1 and V2");
else
  disp("out of the interval");
end

解释:

二维向量V1V2 之间的叉积存储在crossProds 的第一个元素中.如果 V1V2 之间的逆时针角度介于 0 到 180 度(包括 0 度和 180 度)之间,则此值将大于或等于 0.在这种情况下,当 V3 在逆时针方向上位于 V1V2 之间时,则叉积 (V1,V3)(V3,V2) 也大于或等于零.这解释了第一个逻辑检查:

The cross product between the 2-D vectors V1 and V2 is stored in the first element of crossProds. This value will be greater than or equal to zero if the counter-clockwise angle between V1 and V2 is between 0 and 180 degrees, inclusive. In this case, when V3 is between V1 and V2 in the counter-clockwise direction then the cross products (V1,V3) and (V3,V2) are also greater than or equal to zero. This explains the first logical check:

all(crossProds >= 0)

如果V1V2之间的逆时针角度大于180度,那么这两个向量的叉积将小于零.在这种情况下,当V3顺时针方向上介于V1V2之间时,则叉积(V1,V3)(V3,V2) 也小于零.因此,如果这些叉积都小于零,那么 V3 必须在 V1V2 之间逆时针方向.这解释了接下来的两个逻辑检查:

If the counter-clockwise angle between V1 and V2 is greater than 180 degrees, then the cross product of these two vectors will be less than zero. In this case, when V3 is between V1 and V2 in the clockwise direction then the cross products (V1,V3) and (V3,V2) are also less than zero. Therefore, if these cross products are not both less than zero then V3 must be between V1 and V2 in the counter-clockwise direction. This explains the next two logical checks:

(crossProds(1) < 0) && ~all(crossProds(2:3) < 0)

上述逻辑检查应涵盖所有可能的情况.运算符 ||和&&是 MATLAB 中的短路运算符:它们将跳过如果不需要,则第二个语句.例如,如果 OR 中的第一个语句为真,则没有理由检查第二个语句,因为 OR 中只有一个参数需要为真,结果才能为真.

The above logical checks should cover all possible situations. The operators || and && are short circuit operators in MATLAB: they will skip the second statements if they are not necessary. For example, if the first statement in an OR is true, there is no reason to check the second statement since only one argument in an OR needs to be true for the result to be true.

这篇关于当我们从V1逆时针走到V2时,如何判断V3是否在V1和V2之间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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