D3D编程必备的数学知识(2)

本文详细介绍了向量的基本运算,包括向量的加减法、标量乘法、点积及叉积,并通过实例展示了这些运算的几何意义及其在三维坐标系统中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

向量相加

我们能够通过分别把两个向量的各个分量相加得到向量之和,注意在相加之前必须保证它们有相同的维数。

u + v = (ux+ vx, uy+ vy, uz+ vz)

图5显示的是几何学上的向量相加。

o_figure5.jpg

两个向量相加的代码,我们使用重载的加法操作符:

D3DXVECTOR3 u(2.0f, 0.0f, 1.0f);

D3DXVECTOR3 v(0.0f, -1.0f, 5.0f);

// (2.0 + 0.0,  0.0 + (-1.0),  1.0 + 5.0)

D3DXVECTOR3 sum = u + v; // = (2.0f, -1.0f, 6.0f)

向量相减

和加法类似,通过分别把两个向量的各个分量相减得到向量之差。再次重声两个向量必须是相同维数。

u-v = u + (-v) = (ux - vx, uy - vy, uz - vz)

图6显示的是几何学上的向量相减。

o_figure6.jpg

两个向量相减的代码,我们使用重载的减法操作符:

D3DXVECTOR3 u(2.0f, 0.0f, 1.0f);

D3DXVECTOR3 v(0.0f, -1.0f, 5.0f);

D3DXVECTOR3 difference = u - v; // = (2.0f, 1.0f, -4.0f)

图6显示,向量减法得到一个从v向量终点到u向量终点的向量。假如我们解释uv的分量,我们能用向量相减找到从一个点到另一个点的向量。这是非常方便的操作,因为我们常常想找到从一个点到另一个点的方向向量。

标量与向量的乘积

我们能用一个标量与向量相乘,就象名字暗示的一样,向量按比例变化。这种运算不会改变向量的方向,除非标量是负数,这种情况向量方向相反。

ku = (kux, kuy, kuz)

D3DXVECTOR3类提供了向量与标量乘法的操作符。

D3DXVECTOR3 u(1.0f, 1.0f, -1.0f);

D3DXVECTOR3 scaledVec = u * 10.0f; // = (10.0f, 10.0f, -10.0f)

点积

数学上定义点积是两个向量的乘积。按下面等式计算:

u.v = uxvx + uyvy + uzvz = s

The above formula does not present an obvious geometric meaning. Using the law of cosines, we can find the relationship u.v = ∥u∥∥v∥ cosθ , which says that the dot product between two vectors is the cosine of the angle between them scaled by the vectors' magnitudes. Thus, if both u and v are unit vectors, then u.v is the cosine of the angle between them.

Some useful properties of the dot product:

  • If u.v = 0, then uv.

  • If u.v > 0, then the angle θ, between the two vectors is less than 90 degrees.

  • If u.v < 0, then the angle θ, between the two vectors is greater than 90 degrees.

Note 

The ⊥ symbol means "orthogonal," which is synonymous with the term "perpendicular."

We use the following D3DX function to compute the dot product between two vectors:

FLOAT D3DXVec3Dot(          // Returns the result.
CONST D3DXVECTOR3* pV1, // Left sided operand.
CONST D3DXVECTOR3* pV2 // Right sided operand.
);

D3DXVECTOR3 u(1.0f, -1.0f, 0.0f);
D3DXVECTOR3 v(3.0f, 2.0f, 1.0f);

// 1.0*3.0 + -1.0*2.0 + 0.0*1.0
// = 3.0 + -2.0
float dot = D3DXVec3Dot( &u, &v ); // = 1.0
叉积

第二种乘法在向量数学中叫叉积。不象点积,结果值是一个标量,叉积的结果值是另一个向量。通过把两个向量uv相乘得到另一的向量p,向量p垂直于uv。也就是说向量p垂直于u并且垂直于u

The cross product is computed like so:

p = u×v = [(uyvz - uzvy), (uzvx - uxvz), (uxvy - uyvx)]

In component form:

px = (uyvz - uzvy)

py = (uzvx - uxvz)

pz = (uxvy - uyvx)

Example: Find j = k × i = (0, 0, 1) × (1, 0, 0) and verify that j is orthogonal to both k and i.

Solution:

jx =(0(0)-1(0)) = 0

jy =(1(1)-0(0) = 1

jz=(0(0)-0(1) = 0

So, j = (0, 1, 0). Recall from the section titled "Dot Products" that if u.v = 0, then uv Since j.k = 0 and j.i = 0, we know j is orthogonal to both k and i.

We use the following D3DX function to compute the cross product between two vectors:

D3DXVECTOR3 *D3DXVec3Cross(
D3DXVECTOR3* pOut, // Result.
CONST D3DXVECTOR3* pV1, // Left sided operand.
CONST D3DXVECTOR3* pV2 // Right sided operand.
);

It is obvious from Figure 7 that the vector -p is also mutually orthogonal to both u and v. The order in which we perform the cross product determines whether we get p or -p as a result. In other words, u × v = -(v × u). This shows that the cross product is not commutative. You can determine the vector returned by the cross product by the left hand thumb rule. (We use a left hand rule because we are using a left-handed coordinate system. We would switch to the right hand rule if we were using a right-handed coordinate system.) If you curve the fingers of your left hand in the direction of the first vector toward the second vector, your thumb points in the direction of the returned vector.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值