5
votes

Can Eigen do a 2D cross product?

I was trying to replace this code:

Eigen::Vector2f a, b;
float result = a.x()*b.y() - b.x()*a.y();

With this:

Eigen::Vector2f a, b;
float result = a.cross(b);

However, this gives me the following error:

error C2338: THIS_METHOD_IS_ONLY_FOR_VECTORS_OF_A_SPECIFIC_SIZE

Update

Of course Avi Ginsburg is right and its not really defined. So to clarify: What I'm looking for is the length of the cross product (basically the sine of the angle between the vectors, if I understand it correctly).

2
What do you have against the first solution ? You can consider computing the 2x2 determinant.Yves Daoust
@YvesDaoust I just thought it would be nicer to use a predefined function for such a "standard" computation, instead of doing the computation myself...Jan Rüegg
For the 2x2 case, using generic functions is most probably overkill.Yves Daoust

2 Answers

6
votes

This question has already been considered, see this feature request.

1
votes

The result of a cross product is a vector, not a float. And anyway, a cross product in 2D doesn't make sense. In 2D the result vector would have to be perpendicular to both a and b and they already define the plane, so the result would have to be in the 3rd dimension.