112

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.

Marco Sulla
  • 15,299
  • 14
  • 65
  • 100
Zack The Human
  • 8,373
  • 7
  • 39
  • 60
  • 15
    Implementation 2 is wrong. You need two vectors to form a cross product. – bobobobo Jun 24 '09 at 17:37
  • 9
    Implementation 2 rotates the given vector _v_ by -90 degrees. Substitue -90 in `x' = x cos θ - y sin θ` and `y' = x sin θ + y cos θ`. Another variation of this implementation would be to `return Vector2D(-v.Y, v.X);` which is rotate _v_ by +90 degrees. – legends2k Feb 15 '13 at 08:19
  • 3
    @legends2k: It's worth to note that implementation 2 is an extension of [using the determinant to evaluate the cross product](https://en.wikipedia.org/wiki/Cross_product#Matrix_notation): just remove last row and column. Such an extension always has `N-1` operands for `N` dimensions. – Tim Čas Oct 29 '15 at 19:01
  • 4
    Implementation 1 calculates the **magnitude** of the Cross Product. – Mateen Ulhaq Apr 12 '16 at 23:53
  • 1
    @MateenUlhaq sort of, it's the "*signed* magnitude" – Moritz Mahringer Jun 29 '18 at 21:47
  • As @mattias commented on one answer: Implementation 2 *is a cross product* of the provided vector with the z-axis; – Octopus Sep 13 '20 at 22:38
  • @bobobobo In vector algebra, the cross product in d-space is often defined as a multilinear operation (a "product") taking d-1 vectors and producing a vector in their orthogonal complement. So no, you don't need two vectors to form a cross product. See [here.](https://math.stackexchange.com/a/186000) – Daniel Steck May 21 '21 at 10:28

6 Answers6

129

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.

starball
  • 20,030
  • 7
  • 43
  • 238
Drew Hall
  • 28,429
  • 12
  • 61
  • 81
  • 9
    Actually, implementation 2 is cross product of v and the unit vector pointing up at the z-direction. – mattiast Oct 28 '08 at 20:03
  • @mattiast: True. That's exactly how the 2D 'perp' operation is described in 3D. – Drew Hall Oct 30 '08 at 01:15
  • @mattiast: Implementation 2 can be thought of as an extension of using a [determinant to compute the cross product](https://en.wikipedia.org/wiki/Cross_product#Matrix_notation) --- just remove last row&column. It should be noted that implementation 1 is equivalent to: `DotProduct(a, CrossProduct(b))`, which is (very elegantly!) consistent with the notion of a "perpendicular dot product" (which is what that implementation 1 is also [and perhaps more accurately] known as!). – Tim Čas Oct 29 '15 at 19:08
  • In your first paragraph, the magnitude is the absolute value of what's returned. It's not quite the same thing as the Z component. As you point out in the 2nd paragraph, you can use the sign of the cross to repel vampires... err, I mean to detect when a vector is leaving vs. entering the outline of a polygon, for example. – Peter Cordes Oct 15 '16 at 18:42
90

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.

Tom
  • 1,329
  • 6
  • 15
Nils Pipenbrinck
  • 83,631
  • 31
  • 151
  • 221
  • "for example be used to find the winding of three points in 2D space" @Nils Pipenbrinck, what do yo mean by winding in this context ? – Nader Belal Apr 14 '19 at 10:41
  • 2
    @NaderBelal I suppose winding here would imply - if we go from point a to b to c, will we be going clockwise or anti-clockwise, in terms of the angle we just spanned. – Amit Tomar Sep 19 '19 at 14:52
13

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
  • 334,560
  • 70
  • 407
  • 495
10

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.

Bill Burdick
  • 935
  • 10
  • 15
7

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;
}
Bram
  • 7,440
  • 3
  • 52
  • 94
3

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