pymunk.vec2d
模块¶
此模块包含Vec 2d类,当需要2d载体时,该类在所有Pymunk中使用。
创建Vec2ds很容易:
>>> from pymunk.vec2d import Vec2d
>>> Vec2d(1, 2)
Vec2d(1, 2)
>>> xy = (1, 2)
>>> Vec2d(*xy)
Vec2d(1, 2)
>>> '%.2f, %.2f' % Vec2d.from_polar(3, math.pi/4)
'2.12, 2.12'
您可以通过位置和属性访问来索引到Vec 2ds::
>>> v = Vec2d(1, 2)
>>> v.x, v.y
(1, 2)
>>> v[0], v[1]
(1, 2)
Vec2ds可以转换为列表或元组,它们的长度为2::
>>> list(Vec2d(1, 2))
[1, 2]
>>> tuple(Vec2d(1, 2))
(1, 2)
>>> len(Vec2d(1, 2))
2
Vec 2d支持许多常见操作,例如加法和乘::
>>> Vec2d(7.3, 4.2) + Vec2d(1, 2)
Vec2d(8.3, 6.2)
>>> Vec2d(7.3, 4.2) * 2
Vec2d(14.6, 8.4)
Vec 2ds是不可变的,这意味着您无法更新它们。但您可以替换它们::
>>> v = Vec2d(1, 2)
>>> v.x = 4
Traceback (most recent call last):
...
AttributeError: can't set attribute
>>> v += (3, 4)
>>> v
Vec2d(4, 6)
Vec 2ds可以比较::
>>> Vec2d(7.3, 4.2) == Vec2d(7.3, 4.2)
True
>>> Vec2d(7.3, 4.2) == Vec2d(0, 0)
False
Vec 2d类几乎在pymunk中的任何地方都使用,用于2d坐标和载体,例如定义空间中的重力载体。然而,Pymunk足够聪明,可以将字节组或类字节对象转换为Vec 2ds,因此如果您碰巧有一个字节组,通常不需要显式执行转换::
>>> import pymunk
>>> space = pymunk.Space()
>>> space.gravity
Vec2d(0.0, 0.0)
>>> space.gravity = 3, 5
>>> space.gravity
Vec2d(3.0, 5.0)
>>> space.gravity += 2, 6
>>> space.gravity
Vec2d(5.0, 11.0)
最后,Vec 2ds可以腌制和取消腌制:
>>> import pickle
>>> data = pickle.dumps(Vec2d(5, 0.3))
>>> pickle.loads(data)
Vec2d(5, 0.3)
- class pymunk.vec2d.Vec2d(x: float, y: float)[源代码]¶
基类:
NamedTuple
2D向量类,支持向量和标量运算符,还提供了一些高级函数。
- __abs__() float [源代码]¶
返回Vec 2d的长度。
>>> abs(Vec2d(3, 4)) 5.0 >>> abs(Vec2d(3, 4)) == Vec2d(3, 4).length True
- __add__(other: tuple[float, float]) Vec2d [源代码]¶
将Vec 2d与另一个Vec 2d或大小为2的多元组添加。
>>> Vec2d(3, 4) + Vec2d(1, 2) Vec2d(4, 6) >>> Vec2d(3, 4) + (1, 2) Vec2d(4, 6)
- __floordiv__(other: float) Vec2d [源代码]¶
用浮点数除下限(也称为整除)。
>>> Vec2d(3, 6) // 2.0 Vec2d(1.0, 3.0) >>> Vec2d(0, 0) // 2.0 Vec2d(0.0, 0.0)
- __sub__(other: tuple[float, float]) Vec2d [源代码]¶
用另一个Vec 2d或大小为2的多元组减去Vec 2d。
>>> Vec2d(3, 4) - Vec2d(1, 2) Vec2d(2, 2) >>> Vec2d(3, 4) - (1, 2) Vec2d(2, 2)
- __truediv__(other: float) Vec2d [源代码]¶
被浮动除。
>>> Vec2d(3, 6) / 2.0 Vec2d(1.5, 3.0) >>> Vec2d(0,0) / 2.0 Vec2d(0.0, 0.0)
- property angle: float¶
向量的角度(以弧度为单位)。
角度使用atan 2(y,x)计算。
>>> '%.2f' % Vec2d(-1, 0).angle '3.14' >>> Vec2d(0, 0).angle 0.0
- property angle_degrees: float¶
获取向量的角度(以度为单位)。
>>> Vec2d(0, 1).angle_degrees 90.0 >>> Vec2d(0, 0).angle_degrees 0.0
- convert_to_basis(x_vector: tuple[float, float], y_vector: tuple[float, float]) Vec2d [源代码]¶
将该载体转换为由给定x和y载体定义的新基。
>>> Vec2d(10, 1).convert_to_basis((5, 0), (0, 0.5)) Vec2d(2.0, 2.0)
- cross(other: tuple[float, float]) float [源代码]¶
该载体与另一个载体之间的叉积。
V1.cross(V2)->v1.x v2.y - v1.y v2.x
>>> Vec2d(1, 0.5).cross((4, 6)) 4.0
- dot(other: tuple[float, float]) float [源代码]¶
该载体与其他载体之间的点积。v1.dot-> v1.x v2.x + v1.y v2.y
>>> Vec2d(5, 0).dot((0, 5)) 0.0 >>> Vec2d(1, 2).dot((3, 4)) 11.0
- static from_polar(length: float, angle: float) Vec2d [源代码]¶
根据长度和角度(以弧度为单位)创建新的Vec 2d。
请参阅Vec2d.polar_tuple了解相反情况。
>>> Vec2d.from_polar(2, 0) Vec2d(2.0, 0.0) >>> Vec2d(2, 0).rotated(0.5) == Vec2d.from_polar(2, 0.5) True >>> v = Vec2d.from_polar(2, 0.5) >>> v.length, v.angle (2.0, 0.5)
- get_angle_between(other: tuple[float, float]) float [源代码]¶
获取该方向的角度(以弧度为单位)。
>>> '%.2f' % Vec2d(3, 0).get_angle_between(Vec2d(-1, 0)) '3.14' >>> Vec2d(3, 0).get_angle_between(Vec2d(0, 0)) 0.0 >>> Vec2d(0, 0).get_angle_between(Vec2d(0, 0)) 0.0
- get_angle_degrees_between(other: Vec2d) float [源代码]¶
得到向量和另一个向量之间的角度,单位为度。
>>> Vec2d(3, 0).get_angle_degrees_between(Vec2d(-1, 0)) 180.0 >>> Vec2d(3, 0).get_angle_degrees_between(Vec2d(0, 0)) 0.0 >>> Vec2d(0, 0).get_angle_degrees_between(Vec2d(0, 0)) 0.0
- get_dist_sqrd(other: tuple[float, float]) float [源代码]¶
该载体与其他载体之间的平方距离。使用此方法比首先调用get_distance()然后对结果进行平方()更有效。
自 7.0.0 版本弃用: 请使用
get_distance_squared()
而不是.>>> Vec2d(1, 0).get_dist_sqrd((1, 10)) 100 >>> Vec2d(1, 2).get_dist_sqrd((10, 11)) 162 >>> Vec2d(1, 2).get_distance((10, 11))**2 162.0
- get_distance(other: tuple[float, float]) float [源代码]¶
该载体与其他载体之间的距离。
>>> Vec2d(0, 2).get_distance((0, -3)) 5.0 >>> a, b = Vec2d(3, 2), Vec2d(4,3) >>> a.get_distance(b) == (a - b).length == (b - a).length True
- get_distance_squared(other: tuple[float, float]) float [源代码]¶
该载体与其他载体之间的平方距离。使用此方法比首先调用get_distance()然后对结果进行平方()更有效。
>>> Vec2d(1, 0).get_distance_squared((1, 10)) 100 >>> Vec2d(1, 2).get_distance_squared((10, 11)) 162 >>> Vec2d(1, 2).get_distance((10, 11))**2 162.0
- get_length_sqrd() float [源代码]¶
获取该方向的平方长度。如果平方长度足够,则使用此方法更有效,而不是首先访问. long然后执行x**2。
自 7.0.0 版本弃用: 请使用
length_squared
而不是.>>> v = Vec2d(3, 4) >>> v.get_length_sqrd() == v.length**2 True >>> Vec2d(0, 0).get_length_sqrd() 0
- property int_tuple: tuple[int, int]¶
作为int的多元组的该载体的x和y值。使用 round() 四舍五入到最近的int。
>>> Vec2d(0.9, 2.4).int_tuple (1, 2)
- interpolate_to(other: tuple[float, float], range: float) Vec2d [源代码]¶
当前载体和另一个载体之间的载体插值。
>>> Vec2d(10,20).interpolate_to((20,-20), 0.1) Vec2d(11.0, 16.0)
- property length: float¶
获取向量的长度。
>>> Vec2d(10, 0).length 10.0 >>> '%.2f' % Vec2d(10, 20).length '22.36' >>> Vec2d(0, 0).length 0.0
- property length_squared: float¶
获取该方向的平方长度。如果平方长度足够,则使用此方法而不是首先访问. long然后执行x**2会更有效。
>>> v = Vec2d(3, 4) >>> v.length_squared == v.length**2 True >>> Vec2d(0, 0).length_squared 0
- normalized() Vec2d [源代码]¶
获取该载体的规范化副本。注意:如果该载体的长度为0,该函数将返回Vec 2d(0.0,0.0)。
>>> Vec2d(3, 0).normalized() Vec2d(1.0, 0.0) >>> Vec2d(3, 4).normalized() Vec2d(0.6, 0.8) >>> Vec2d(0, 0).normalized() Vec2d(0.0, 0.0)
- normalized_and_length() tuple[Vec2d, float] [源代码]¶
规范化该载体并返回规范化之前的长度。
>>> Vec2d(3, 0).normalized_and_length() (Vec2d(1.0, 0.0), 3.0) >>> Vec2d(3, 4).normalized_and_length() (Vec2d(0.6, 0.8), 5.0) >>> Vec2d(0, 0).normalized_and_length() (Vec2d(0.0, 0.0), 0.0)
- perpendicular_normal() Vec2d [源代码]¶
获取从原始载体逆时针旋转90度的垂直规范化载体。
>>> Vec2d(1, 0).perpendicular_normal() Vec2d(0.0, 1.0) >>> Vec2d(2, 0).perpendicular_normal() Vec2d(0.0, 1.0) >>> Vec2d(1, 1).perpendicular_normal().angle_degrees 135.0 >>> Vec2d(1, 1).angle_degrees + 90 135.0 >>> Vec2d(0, 0).perpendicular_normal() Vec2d(0, 0)
- property polar_tuple: tuple[float, float]¶
将此载体返回为极坐标(长度、角度)
请参阅Vec2d.from_polar()了解相反情况。
>>> Vec2d(2, 0).polar_tuple (2.0, 0.0) >>> Vec2d(2, 0).rotated(0.5).polar_tuple (2.0, 0.5) >>> Vec2d.from_polar(2, 0.5).polar_tuple (2.0, 0.5)
- projection(other: tuple[float, float]) Vec2d [源代码]¶
将此载体投影到其他载体之上。
>>> Vec2d(10, 1).projection((5.0, 0)) Vec2d(10.0, 0.0) >>> Vec2d(10, 1).projection((10, 5)) Vec2d(8.4, 4.2) >>> Vec2d(10, 1).projection((0, 0)) Vec2d(0.0, 0.0)
- rotated(angle_radians: float) Vec2d [源代码]¶
通过按角度弧度旋转该向量来创建并返回一个新向量。
>>> '%.2f' % Vec2d(2,0).rotated(math.pi).angle '3.14' >>> Vec2d(0,0).rotated(1) Vec2d(0.0, 0.0)
- rotated_degrees(angle_degrees: float) Vec2d [源代码]¶
- 创建并返回一个新的向量,方法是将此向量旋转
角度_度。
>>> Vec2d(2,0).rotated_degrees(90.0).angle_degrees 90.0 >>> Vec2d(0, 0).rotated_degrees(90.0) Vec2d(0.0, 0.0)
- scale_to_length(length: float) Vec2d [源代码]¶
返回缩放到给定长度的该向量的副本。
请注意,零长度Vec 2d无法缩放,但会引发异常。
>>> Vec2d(1, 0).scale_to_length(10) Vec2d(10.0, 0.0) >>> '%.2f, %.2f' % Vec2d(10, 20).scale_to_length(20) '8.94, 17.89' >>> Vec2d(1, 0).scale_to_length(0) Vec2d(0.0, 0.0) >>> Vec2d(0, 0).scale_to_length(1) Traceback (most recent call last): ... ZeroDivisionError: float division by zero
- x: float¶
字段号0的别名
- y: float¶
字段号%1的别名