计算二维矢量的叉积

来自维基百科:

交叉二元运算是 三维的欧几里德空间中两个向量的交叉乘积,它导致了另一个垂直于包含两个输入向量的平面的向量。

鉴于定义只定义在三个维度(或者七,一,零) ,如何计算两个二维矢量的交叉乘积?

我已经看到了两个实现。一个返回一个新向量(但只接受一个向量) ,另一个返回一个标量(但是是两个向量之间的计算)。

实现1(返回一个标量) :

float CrossProduct(const Vector2D & v1, const Vector2D & v2) const
{
return (v1.X*v2.Y) - (v1.Y*v2.X);
}

实现2(返回一个向量) :

Vector2D CrossProduct(const Vector2D & v) const
{
return Vector2D(v.Y, -v.X);
}

为什么要使用不同的实现? 我将使用标量实现做什么? 我将使用向量实现做什么?

我问这个问题的原因是因为我自己正在编写一个 Vector2D 类,不知道使用哪个方法。

205110 次浏览

Implementation 1 returns the magnitude of the vector that would result from a regular 3D cross product of the input vectors, taking their Z values implicitly as 0 (i.e. treating the 2D space as a plane in the 3D space). The 3D cross product will be perpendicular to that plane, and thus have 0 X & Y components (thus the scalar returned is the Z value of the 3D cross product vector).

Note that the magnitude of the vector resulting from 3D cross product is also equal to the area of the parallelogram between the two vectors, which gives Implementation 1 another purpose. In addition, this area is signed and can be used to determine whether rotating from V1 to V2 moves in an counter clockwise or clockwise direction. It should also be noted that implementation 1 is the determinant of the 2x2 matrix built from these two vectors.

Implementation 2 returns a vector perpendicular to the input vector still in the same 2D plane. Not a cross product in the classical sense but consistent in the "give me a perpendicular vector" sense.

Note that 3D euclidean space is closed under the cross product operation--that is, a cross product of two 3D vectors returns another 3D vector. Both of the above 2D implementations are inconsistent with that in one way or another.

Hope this helps...

In short: It's a shorthand notation for a mathematical hack.

Long explanation:

You can't do a cross product with vectors in 2D space. The operation is not defined there.

However, often it is interesting to evaluate the cross product of two vectors assuming that the 2D vectors are extended to 3D by setting their z-coordinate to zero. This is the same as working with 3D vectors on the xy-plane.

If you extend the vectors that way and calculate the cross product of such an extended vector pair you'll notice that only the z-component has a meaningful value: x and y will always be zero.

That's the reason why the z-component of the result is often simply returned as a scalar. This scalar can for example be used to find the winding of three points in 2D space.

From a pure mathematical point of view the cross product in 2D space does not exist, the scalar version is the hack and a 2D cross product that returns a 2D vector makes no sense at all.

Another useful property of the cross product is that its magnitude is related to the sine of the angle between the two vectors:

| a x b | = |a| . |b| . sine(theta)

or

sine(theta) = | a x b | / (|a| . |b|)

So, in implementation 1 above, if a and b are known in advance to be unit vectors then the result of that function is exactly that sine() value.

I'm using 2d cross product in my calculation to find the new correct rotation for an object that is being acted on by a force vector at an arbitrary point relative to its center of mass. (The scalar Z one.)

Implementation 1 is the perp dot product of the two vectors. The best reference I know of for 2D graphics is the excellent Graphics Gems series. If you're doing scratch 2D work, it's really important to have these books. Volume IV has an article called "The Pleasures of Perp Dot Products" that goes over a lot of uses for it.

One major use of perp dot product is to get the scaled sin of the angle between the two vectors, just like the dot product returns the scaled cos of the angle. Of course you can use dot product and perp dot product together to determine the angle between two vectors.

Here is a post on it and here is the Wolfram Math World article.

A useful 2D vector operation is a cross product that returns a scalar. I use it to see if two successive edges in a polygon bend left or right.

From the Chipmunk2D source:

/// 2D vector cross product analog.
/// The cross product of 2D vectors results in a 3D vector with only a z component.
/// This function returns the magnitude of the z value.
static inline cpFloat cpvcross(const cpVect v1, const cpVect v2)
{
return v1.x*v2.y - v1.y*v2.x;
}