views:

10224

answers:

4

From wikipedia:

the cross product is a binary operation on two vectors in a three-dimensional Euclidean space that results in another vector which is perpendicular to the plane containing the two input vectors.

Given that the definition requires at least three dimensions, how does one calculate the cross product of two 2d vectors?

I have seen two implementations. One returns a new vector (but only accepts a single vector), the other returns a scalar (but is a calculation between two vectors).

Implementation 1 (returns a scalar):

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

Implementation 2 (returns a vector):

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

Why the varying implementations? What would I use the scalar implementation for? What would I use the vector implementation for?

The reason I ask is because I'm writing a Vector2D class myself and don't know which method to use.

+8  A: 

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).

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...

Drew Hall
Thanks. Your explanation makes a lot of sense.
Zack Mulgrew
Actually, implementation 2 is cross product of v and the unit vector pointing up at the z-direction.
mattiast
@mattiast: True. That's exactly how the 2D 'perp' operation is described in 3D.
Drew Hall
+11  A: 

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 see how the cross-product of two vectors would be assuming that the 2D vectors are extended to 3D by setting the 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 meaningfull value. X and y will always be zero.

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

From a pure mathematical point of view the crossproduct 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.

Nils Pipenbrinck
+2  A: 

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.

Alnitak
Thanks! This is good to know.
Zack Mulgrew
A: 

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.)

Thanks for the example!
Zack Mulgrew