vpython学习手册——矢量对象

矢量对象

矢量对象不是可显示的对象,但它是 3D 计算的强大辅助工具。它的性质类似于科学和工程中使用的向量。
vector(x,y,z)
这将创建一个具有给定分量 x、y 和 z 的 3D 矢量对象。
向量可以相加或相减,或乘以一个普通数。例如,
v1 = vector(1,2,3)
v2 = vector(10,20,30)
print(v1+v2) # 显示 <1 22 33>
print(2*v1) # 显示 <2 4 6>
您可以参考向量的各个组件:
v2.x是 10,v2.y是 20,v2.z是 30
可以从向量制作向量:vector(v2)仍然是vector(10,20,30)。这是制作矢量的单独副本的便捷方法。

矢量函数

以下函数可用于处理向量:
mag(A) = A.mag = |A|,向量的大小,也就是向量 的长度(或称模)
mag2(A) = A.mag2 = |A|*|A|,向量的大小平方
norm(A) = A.norm() = A/|A|,向量方向的单位向量
hat(A) = A.hat = A/|A|,向量方向的单位向量;A.norm() 的替代方法,基于单位向量通常以ĉ的形式书写,在向量上加上一个“帽子”

为方便起见,norm(vec(0,0,0))或vec(0) ,0,0).hat计算为vec(0,0,0)。
dot(A,B) = A.dot(B) = A dot B,两个向量之间的标量点积
cross(A,B) = A.cross(B ),两个向量之间的向量叉积
diff_angle(A, B) = A.diff_angle(B),两个向量之间的角度,以弧度为单位
proj(A,B) = A.proj(B) = dot(A,norm(B))*norm(B),向量投影A 沿 B
comp(A,B) = A.comp(B) = dot(A,norm(B)),A 沿 B
A.equals(B)的标量投影为 True 如果A和B具有相同分量(这意味着它们具有相同的幅度和相同的方向)。
vector.random()生成一个向量,每个向量的分量都是 -1 到 +1 范围内的随机数

一些例子:

mag(A) # 计算 A 的长度
mag(vector(1,1,1)) # = sqrt(3) = 1.732…
mag2(vector(1,1,1)) # = 3, 幅度平方
可以重置向量的幅度或幅度平方:
v2.mag = 5 # 设置震级为 5; 方向没有变化
v2.mag2 = 2.7 # 将 v2 的平方大小设置为 2.7
您可以使用 norm() 将幅度重置为 1:
norm(A) # A/|A|,归一化;1
范数的大小(向量(1,1,1))=向量(1,1,1)/sqrt(3)
你也可以写v1.norm()或v1.hat。
你可以改变向量的方向而不改变它的大小:
v2.hat = v1 # 将 v2 的方向改为 v1,但不改变 v2 的大小
diff_angle(v1,v2)
你也可以写v1.diff_angle(v2). 为方便起见,如果任一向量的幅度为零,则计算角度差为零.
cross(A,B)或A.cross(B)给出两个向量的叉积,一个向量垂直于由A和B定义的平面,在右手规则定义的方向上:如果右手的手指手从A向B弯曲,拇指指向叉积的方向。这个向量的大小等于 mag(A)*mag(B)*sin(diff_angle(A,B))。
dot(A,B)或A.dot(B)给出两个向量的点积,它是一个普通数,等于mag(A)*mag(B)*cos(diff_angle(A,B))。如果将两个向量归一化,点积会给出向量之间夹角的余弦,这通常很有用。

旋转向量

有一个旋转向量的函数:
v2 = rotate(v1, angle=a, axis=vector(x,y,z))
角度必须以弧度为单位。默认轴是 (0,0,1),用于在 xy 平面中围绕 z 轴旋转。旋转矢量没有原点。你也可以写v2 = v1.rotate(angle=a, axis=vector(x,y,z))。还有一个对象的旋转功能。
有用于在度数和弧度之间转换的函数,其中 360 度中有 2pi 弧度:
radians(360)相当于 2
pi
degrees(2*pi)相当于 360

原版英文文档

The vector Object
The vector object is not a displayable object but is a powerful aid to 3D computations. Its properties are similar to vectors used in science and engineering.

vector(x,y,z)

This creates a 3D vector object with the given components x, y, and z.

Vectors can be added or subtracted from each other, or multiplied by an ordinary number. For example,

v1 = vector(1,2,3)
v2 = vector(10,20,30)
print(v1+v2) # displays <1 22 33>
print(2*v1) # displays <2 4 6>

You can refer to individual components of a vector:

v2.x is 10, v2.y is 20, v2.z is 30

It is okay to make a vector from a vector: vector(v2) is still vector(10,20,30). This is a convenient way to make a separate copy of a vector.

Vector functions

The following functions are available for working with vectors:

mag(A) = A.mag = |A|, the magnitude of a vector

mag2(A) = A.mag2 = |A|*|A|, the vector’s magnitude squared

norm(A) = A.norm() = A/|A|, a unit vector in the direction of the vector

hat(A) = A.hat = A/|A|, a unit vector in the direction of the vector; an alternative to A.norm(), based on the fact that unit vectors are customarily written in the form ĉ, with a “hat” over the vector

For convenience, norm(vec(0,0,0)) or vec(0,0,0).hat is calculated to be vec(0,0,0).

dot(A,B) = A.dot(B) = A dot B, the scalar dot product between two vectors

cross(A,B) = A.cross(B), the vector cross product between two vectors

diff_angle(A,B) = A.diff_angle(B), the angle between two vectors, in radians

proj(A,B) = A.proj(B) = dot(A,norm(B))*norm(B), the vector projection of A along B

comp(A,B) = A.comp(B) = dot(A,norm(B)), the scalar projection of A along B

A.equals(B) is True if A and B have the same components (which means that they have the same magnitude and the same direction).

vector.random() produces a vector each of whose components is a random number in the range -1 to +1

Some examples:

mag(A) # calculates length of A
mag(vector(1,1,1)) # = sqrt(3) = 1.732…
mag2(vector(1,1,1)) # = 3, the magnitude squared

It is possible to reset the magnitude or the magnitude squared of a vector:

v2.mag = 5 # sets magnitude to 5; no change in direction
v2.mag2 = 2.7 # sets squared magnitude of v2 to 2.7

You can reset the magnitude to 1 with norm():

norm(A) # A/|A|, normalized; magnitude of 1
norm(vector(1,1,1)) = vector(1,1,1)/sqrt(3)

You can also write v1.norm() or v1.hat.

You can change the direction of a vector without changing its magnitude:

v2.hat = v1 # changes the direction of v2 to that of v1
# but not the magnitude of v2

To calculate the angle between two vectors (the “difference” of the angles of the two vectors).

diff_angle(v1,v2)

You can also write v1.diff_angle(v2). For convenience, if either of the vectors has zero magnitude, the difference of the angles is calculated to be zero.

cross(A,B) or A.cross(B) gives the cross product of two vectors, a vector perpendicular to the plane defined by A and B, in a direction defined by the right-hand rule: if the fingers of the right hand bend from A toward B, the thumb points in the direction of the cross product. The magnitude of this vector is equal mag(A)*mag(B)*sin(diff_angle(A,B)).

dot(A,B) or A.dot(B) gives the dot product of two vectors, which is an ordinary number equal to mag(A)*mag(B)*cos(diff_angle(A,B)). If the two vectors are normalized, the dot product gives the cosine of the angle between the vectors, which is often useful.

Rotating a vector

There is a function for rotating a vector:

v2 = rotate(v1, angle=a, axis=vector(x,y,z))

The angle must be in radians. The default axis is (0,0,1), for a rotation in the xy plane around the z axis. There is no origin for rotating a vector. You can also write v2 = v1.rotate(angle=a, axis=vector(x,y,z)). There is also a rotate capability for objects.

There are functions for converting between degrees and radians, where there are 2*pi radians in 360 degrees:

radians(360) is equivalent to 2*pi

degrees(2*pi) is equivalent to 360

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值