0
What about you guys? , I’m studying about vectors with the pygame python library and I don’t understand why an asterisk next to the letter A in the vector defined in the position variable down in the code. Can anyone explain to me the behavior of this in the code?
import pygame
import math
class Vector2():
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def __str__(self):
return "(%s, %s)" %(self.x, self.y)
def from_points(P1, P2):
return Vector2 ( P2[0] - P1[0], P2[1] - P1[1] )
def get_magnitude(self):
return math.sqrt( self.x**2 + self.y**2 )
def normalize(self):
magnitude = self.get_magnitude()
self.x /= magnitude
self.y /= magnitude
def __add__(self, rhs): # rhs quer dizer Right Hand Side (Lado Direito)
return Vector2(self.x + rhs.x , self.y + rhs.y)
def __mul__(self, scalar):
return Vector2(self.x * scalar, self.y * scalar)
def __truediv__(self, scalar):
return Vector2(self.x / scalar, self.y / scalar)
A = (10.0, 20.0)
B = (30.0, 35.0)
AB = Vector2.from_points(A, B)
step = AB * .1
position = Vector2(*A) # esse asterisco junto a letra A que não entendi
for n in range(10):
position += step
print(position)
In short, he deconstructs the tuple by defining each value of
A
as a parameter ofVector2
. It would be the equivalent of doingVector2(A[0], A[1])
in that case.– Woss
Valew Woss, now the code makes sense to me. I’m not very experienced with python, but I’ve never seen it in python.
– Eduardo