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

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

问题描述

来自维基百科:

叉积是对三维欧几里德空间中的两个向量进行的二元运算,它产生另一个向量,该向量垂直于包含两个输入向量的平面.

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

实现 1(返回标量):

Implementation 1 (returns a scalar):

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.

推荐答案

Implementation 1 返回由输入向量的常规 3D 叉积产生的向量的大小,将它们的 Z 值隐式为 0(即处理2D 空间作为 3D 空间中的平面).3D 叉积将垂直于该平面,因此具有 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).

请注意,由 3D 叉积产生的向量的大小也等于两个向量之间平行四边形的面积,这为实现 1 提供了另一个目的.另外,这个区域是有符号的,可以用来判断从V1到V2的旋转是逆时针还是顺时针.还应注意,实现 1 是由这两个向量构建的 2x2 矩阵的行列式.

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 返回一个与输入向量垂直的向量,该向量仍处于同一二维平面中.不是经典意义上的叉积,而是给我一个垂直向量"意义上的一致.

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 欧几里得空间在叉积运算下是封闭的——即两个 3D 向量的叉积返回另一个 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天全站免登陆