簡體   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