如何确定一个向量是否在其他两个向量之间? [英] How to determine if a vector is between two other vectors?

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

问题描述

我正在寻找一种快速有效的方法来确定向量B是否在向量A和向量C的小角度之间.通常,我会使用垂直点积来确定每条线B的哪一边,但是在这种情况下由于以下原因,情况并非如此简单:

I am looking for a fast and effective way to determine if vector B is Between the small angle of vector A and vector C. Normally I would use the perpendicular dot product to determine which sides of each line B lies on but in this case is not so simple because of the following:

  • 可以认为没有一个向量可以归一化,将它们归一化是我宁愿避免的额外步骤.
  • 我不清楚哪一侧是最小角度,所以很难说这条线的哪一侧是好.
  • A和B可能共线或正好相隔180度,在这种情况下我想返回false.
  • 当我在3D环境中工作时,如果将事情简化,更重要的是更快,我很容易将其简化为2D.该测试将用于需要尽快运行的算法中.

如果有一种简便有效的方法来确定我的垂直向量应该指向哪个方向,则可以使用两个点积进行测试.

If there is some easy and efficient method to determine which direction my perpendicular vectors should both point I could use the two dot products for my test.

到目前为止,我一直在考虑的另一种没有成功的方法是使用矩阵.从理论上讲,根据我对矩阵变换的理解,我应该能够使用A和C作为基向量.然后,将B乘以矩阵,我应该能够通过X和Y是否都为正来测试B所在的象限.如果我可以采用这种方法,那将是最好的方法,因为一个矩阵乘法应该比两个点积快,而且我不必担心哪一侧的角度最小.

Another approach I have been considering without much success so far is using a matrix. In theory from what I understand of matrix transforms I should be able to use A and C as basis vectors. Then multiplying B by the matrix I should be able to test what quadrant B then lies in by whether X and Y are both positive. If I could get this approach to work it would likely be the best since one matrix multiplication should be faster than two dot products and I should not have to worry about which side has the smallest angle on it.

问题出在我的测试中,我不能简单地使用A和C作为底数并正常乘以并得到正确的行为.我真的不确定在这里我做错了什么.我遇到过几次向量空间"一词,据我所知,这似乎与矩阵变换非常相似,不需要正交基或正交基.它和矩阵一样吗?如果没有,是否会有更好的方法,我将如何使用?

The problem is from my tests I cannot simply use A and C as bases and multiply it normally and get correct behavior. I am really not sure what i am doing wrong here. I have run across the term "Vector spaces" a few times which as near as I can figure seems to be a very similar concept to matrix transforms without any requirements for orthogonal bases or orthonormal bases. Is it the same thing as matrix? If not, might there be a better approach and how would I use that?

只需对我在说的内容进行更直观的说明即可

Just to give a more visual explanation of what I am talking about:

@Aki Suihkonen 我似乎无法正常工作.编写了一个我可以运行的模拟案例,看看是否无法弄清楚东西

@Aki Suihkonen I can't seem to get it working. Coded up a mock case I could run through and see if I can't figure somthing out

在这种情况下,使用

Ax 2.9579773 Ay 3.315979

Ax 2.9579773 Ay 3.315979

Cx 2.5879822 Cy 5.1630249

Cx 2.5879822 Cy 5.1630249

对于B,我绕着四个象限旋转,矢量将空间分成几个部分.

For B I rotated around the four quadrants the vectors divide the space up into.

我得到的迹象: -对于Q1 -- -对于第二季度+- -对于Q3 +- -对于第四季度--

The signs I got: - For Q1 -- - For Q2 +- - For Q3 +- - For Q4 --

假设我在环境中旋转的方向与我确信可以做到的图像相同.

Assuming I rotated around in the enviroment the same direction as the image I am fairly sure I did.

推荐答案

我认为Aki的解决方案很接近,但是在某些情况下它不起作用:

I think Aki's solution is close, but there are cases where it doesn't work:

从他的解决方案开始:

return (ay * bx - ax * by) * (ay * cx - ax * cy) < 0;

这等效于检查B和A之间的叉积与C和A之间的叉积是否具有相同的符号.

This is equivalent to checking whether the cross product between B and A has the same sign as the cross product between C and A.

叉积的符号(U x V)告诉您V是位于U的一侧还是位于另一侧(板外,板内).在大多数坐标系中,如果U需要逆时针旋转(板外),则符号为正.

The sign of the cross product (U x V) tells you whether V lies on one side of U or the other (out of the board, into the board). In most coordinate systems, if U needs to rotate counter-clockwise (out of the board), then the sign will be positive.

因此,Aki的解决方案将检查B是否需要朝一个方向旋转才能到达A,而C是否需要朝另一个方向旋转.在这种情况下,B不在A和C内.当您不知道A和C的顺序"时,此解决方案将不起作用,如下所示:

So Aki's solution checks to see if B needs to rotate in one direction to get to A, while C needs to rotate in the other direction. If this is the case, B is not within A and C. This solution doesn't work when you don't know the 'order' of A and C, as follows:

要确定B是否在A和C之内,您需要同时进行两种检查.也就是说,从A到B的旋转方向应与从A到C的旋转方向相同,从C到B的旋转方向应与从C到A的旋转方向相同.

To know for certain whether B is within A and C you need to check both ways. That is, the rotation direction from A to B should be the same as from A to C, and the rotation direction from C to B should be the same as from C to A.

这减少为:

if (AxB * AxC >= 0 && CxB * CxA >= 0)

// then B is definitely inside A and C

这篇关于如何确定一个向量是否在其他两个向量之间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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