繁体   English   中英

pygame.math.Vector2 构造函数参数表示什么?

[英]What do the pygame.math.Vector2 constructor parameters indicate?

我试图弄清楚如何在给定角度(度)的情况下创建单位向量/归一化 Vector2 实例。 我在阅读 pygame 在https://www.pygame.org/docs/ref/math.html#pygame.math.Vector2的文档时遇到了以下问题:

Vector2() -> Vector2
Vector2(int) -> Vector2
Vector2(float) -> Vector2
Vector2(Vector2) -> Vector2
Vector2(x, y) -> Vector2
Vector2((x, y)) -> Vector2

据我了解,它们生成以下向量:

Vector2() -> (0, 0)
Vector2(Vector2) -> (Vector2.x, Vector2.y)
Vector2(x, y) -> (x, y)
Vector2((x, y)) -> (x, y)

但是intfloat参数数据类型表示什么? 另外,如果我误解了上述假设,请更正。

此外,我可以实例化一个标准化的Vector2 object 给定一个角度,还是我需要先用math库做一些三角函数?

我试图弄清楚如何在给定角度的情况下创建单位向量/归一化 Vector2 实例

单位向量的长度为 1,因此创建具有给定角度的单位向量的最简单方法是:

v = pygame.math.Vector2(1, 0)
v.rotate_ip(angle)

或者,您可以使用三角函数sincos创建向量:

angle_radian = math.radians(angle_degree)
v = pygame.math.Vector2(math.cos(angle_radian), math.sin(angle_radian))

所有向量构造函数构造一个具有指定标量的向量。 只有一个参数的构造函数创建一个向量,其中 x 和 y 分量相等。

您可以使用print轻松找到自己:

print(pygame.math.Vector2())
print(pygame.math.Vector2(1))
print(pygame.math.Vector2(1.5))
print(pygame.math.Vector2(2, 3))
print(pygame.math.Vector2((2.5, 3.5)))

Output:

[0, 0]
[1, 1]
[1.5, 1.5]
[2, 3]
[2.5, 3.5]

intfloat表示您可以使用整数和浮点数构造向量,但这对 object 的行为没有影响。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM