Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating a 2D Vector's Cross Product

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 is only defined in three (or seven, one and zero) 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.

like image 969
Zack The Human Avatar asked Oct 28 '08 16:10

Zack The Human


People also ask

What does cross product represent in 2d?

"2D cross products" are more properly called 2d wedge products. Wedge products generalize to other dimensions, but cross products are always 3d wedge products. The usual operator symbol for a wedge product is ^ . You can use 2d wedge products to determine if one vector is to the left or the right of another one.


1 Answers

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

like image 73
Drew Hall Avatar answered Oct 16 '22 16:10

Drew Hall