47. Calculating member orientation and length

Questions and discussion for this lecture live here. Fire away by hitting Reply below :fire:

Hi Sean, I would like to suggest an improvement to your code. I think that it would be far more simple if to get the orientation of the member you use the function math.atan2 (it provides an angle between -pi/2 and pi/2 but that’s enough).
Here the code that I implemented (I tested it and everything works fine as with yours).

def memberOrientation(memberNO):
    memberIndex = memberNO - 1 #Index identifying member in arrays of members
    node_i = members[memberIndex][0]
    node_j = members[memberIndex][1]
    
    xi = nodes[node_i - 1][0] #x-coord of node i
    yi = nodes[node_i - 1][1] #y-coord of node i
    xj = nodes[node_j - 1][0] #x-coord of node j
    yj = nodes[node_j - 1][1] #y-coord of node j

    dx = xj-xi #x component of member vector
    dy = yj-yi #y component of member vector
    
    #Angle of member
    
    theta = math.atan2(dy,dx)
    
    #Length of member
    
    L = math.sqrt(dx**2+dy**2)
        
    return [theta, L]

Thanks @Oliviero - yes this is a great improvement. I wasn’t familiar with that function at the time of writing…a great time saver :+1:

1 Like