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

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

问题描述

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

alt text http://www.freeimagehosting.net/uploads/1448ea8896.jpg

它不能通过获得他们的角度和评估这些条件(伪代码)来完成:

 如果角度(V3 )>角度(V1)&&角度(V3)角度(V2)
printf(V3在V1和V2之间)
else
printf(超出间隔)
pre>

若要查看其缺陷,假设角度函数给出的角度在[-pi pi] 。因此,如果角度(V1)= 120(度),角度(V2)= -130和角度(V3)= 150,则答案(根据上述代码)超出间隔逆时针从V1到V2,它在它们之间。



你可能会建议在角度(V2)或类似的东西上添加2 * pi,但我尝试了这样的事情这是行不通的。



我正在MATLAB编程。

编辑1:它是在2D。

解决方案

因为你在MATLAB中这样做,所以下面是一个应该可以工作的解决方案:$ b $ (1)* V2(2)-V1(2)* V2(1)...
V1(1)* b

  crossProds = [V1 * 2(2)-V1(2)* V3(1)... 
V3(1)* V2(2)-V3(2)* V2(1)]; (所有(crossProds> = 0)|| ...
(crossProds(1)<0)&&〜all(crossProds(2:3)< 0)) ,
disp(V3在V1和V2之间);
else
disp(out of the interval);
end

注释:

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

pre $ all $(crossProds>

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

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

上述逻辑检查应涵盖所有可能的的情况。运营商||和&& MATLAB中的短路操作符:它们将跳过如果他们没有必要的话,可以做第二个陈述例如,如果OR中的第一个语句为真,那么没有理由检查第二个语句,因为OR中只有一个参数需要为真才能使结果为真。


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?

alt text 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")

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.

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

I'm programming in MATLAB.

EDIT 1 : it is in 2D.

解决方案

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

EXPLANATION:

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)

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)

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天全站免登陆