from math import sin, cos, tan, asin, acos, atan2, radians, degrees, sqrt
def find_net_force_from_file(filename):
total_horizontal = 0
total_vertical = 0
forces=[]
mtr=open(filename,"r")
forces=mtr.readlines()
forces = [i.split(' ') for i in forces]
#Now, let's iterate through each force in the forces:
for force in forces:
#To make our code easier to read, let's pull the
#magnitude and angle out from the tuple:
magnitude, angle = force
#The directions told us we need to convert our
#angles to radians to use Python's trignometric
#functions:
angle = radians(float(angle))
#Now, the horizontal component is the magnitude
#times the cosine of the angle:
horizontal = int(magnitude) * (cos(angle))
#And the vertical component is the magnitude times
#the sine of the angle:
vertical = int(magnitude) * sin(angle)
#Now that we've calculated the horizontal and
#vertical components, let's add those to our
#running tallies:
total_horizontal += horizontal
total_vertical += vertical
#The net magnitude is the hypotenuse of the triangle
#with these two components as their legs. So, we can
#calculate the net magnitude using the Pythagorean
#theorem:
net_magnitude = sqrt(total_horizontal**2 + total_vertical**2)
#Then as instructed, we round it to one decimal point:
net_magnitude = round(net_magnitude, 1)
#Next, we need to calculate the angle. As directed, we
#do this with the atan2 function from Python's math
#library, which takes the vertical and horizontal
#components as arguments:
net_angle = atan2(total_vertical, total_horizontal)
#Then, we convert this back to degrees:
net_angle = degrees(net_angle)
#And round it to one decimal point as instructed:
net_angle = round(net_angle, 1)
mtr.close()
#Last, we return the tuple of the madnitude and angle:
return (net_magnitude, net_angle)