计算二维向量的叉积 [英] Calculating a 2D Vector's Cross Product

查看:1247
本文介绍了计算二维向量的叉积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从维基百科:

叉积是在一个的立体欧氏空间又导致了另一个载体,其为垂直于包含两个输入向量的平面上的两个向量的二进制运算

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

实现1(返回标):

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

实施2(返回载体):

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?

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

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

推荐答案

实施1返回向量的大小,将导致从输入矢量的规则的三维交叉乘积,隐服用他们的Z值作为0(即处理二维空间中的三维空间中的平面)。三维叉积将垂直于该平面,并且因此有0 X - 安培; Y分量(由此返回的标量是3D叉积向量的Z值)。

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

请注意,从三维交积所得的向量的大小也等于在面积的两个矢量之间的平行四边形,这使实现方式1的另一目的。此外,该区域被签名,并且可以被用来确定是否从V1旋转到V2中的逆时针或顺时针方向移动。还应当指出的是,实施1是来自这两个载体构建的2×2矩阵的行列式。

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.

实施2返回垂直于输入向量仍处于相同2D平面的载体。在传统意义上不是一个跨产品,但一致的给我一个垂直的矢量的感觉。

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.

需要注意的是三维欧几里得空间下叉积操作关闭 - 也就是说,两个3D向量的积返回另一个三维矢量。上述两个2D实现都是不符合,在这种或那种方式。

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.

希望这有助于...

这篇关于计算二维向量的叉积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆