Function was written to accept pairs of variables, is there a way to write
them like that in python 3?
The function is defined as:
def addVectors((angle1, length1), (angle2, length2)):
x = math.sin(angle1) * length1 + math.sin(angle2) * length2
y = math.cos(angle1) * length1 + math.cos(angle2) * length2
angle = 0.5 * math.pi - math.atan2(y, x)
length = math.hypot(x, y)
return (angle, length)
It is used like this:
def collide(p1, p2):
dx = p1.x - p2.x
dy = p1.y - p2.y
dist = math.hypot(dx, dy)
if dist < p1.size + p2.size:
angle = math.atan2(dy, dx) + 0.5 * math.pi
total_mass = p1.mass + p2.mass
(p1.angle, p1.speed) = addVectors((p1.angle,
p1.speed*(p1.mass-p2.mass)/total_mass), (angle,
2*p2.speed*p2.mass/total_mass))
(p2.angle, p2.speed) = addVectors((p2.angle, p2.speed*(p2.mass-
p1.mass)/total_mass), (angle+math.pi,
2*p1.speed*p1.mass/total_mass))
p1.speed *= elasticity
p2.speed *= elasticity
My problem is that python 3 will only let me assign one group of
parentheses to a function, but the example here uses two, and the results
and the way to get them are a result of this course. I'm not sure if there
is a way to fix this, or if I have to restructure the formula, which I'm
really not sure I'm able to do. Any advice?
No comments:
Post a Comment