[英]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)
但是int
和float
參數數據類型表示什么? 另外,如果我誤解了上述假設,請更正。
此外,我可以實例化一個標准化的Vector2
object 給定一個角度,還是我需要先用math
庫做一些三角函數?
我試圖弄清楚如何在給定角度的情況下創建單位向量/歸一化 Vector2 實例
單位向量的長度為 1,因此創建具有給定角度的單位向量的最簡單方法是:
v = pygame.math.Vector2(1, 0)
v.rotate_ip(angle)
或者,您可以使用三角函數sin
和cos
創建向量:
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]
int
和float
表示您可以使用整數和浮點數構造向量,但這對 object 的行為沒有影響。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.