Program to print the center of mass:
# Calculate the triangle's center of mass # from numpy import * a = array([0, 1, 0]) b = array([4.3, -2.4, 0.005]) c = array([-3.2, 5.1, -3.) print (a+b+c)/3The output:
[0.36666666666666653, 1.2333333333333332, -0.99833333333333341]
Program to print the vectors' angles:
# Calculate a triangle's three angles # import numpy as np # The vertices a = np.array([0, 1, 0]) b = np.array([4.3, -2.4, 0.005]) c = np.array([-3.2, 5.1, -3.]) # Angle between a-b and c-b ab = a - b ab_modulus = np.sqrt((ab*ab).sum()) cb = c - b cb_modulus = np.sqrt((cb*cb).sum()) abc_cos_angle = np.dot(ab,cb) / ab_modulus / cb_modulus print "Angle a-b-c:", np.arccos(abc_cos_angle) # Angle between c-a and b-a ca = c - a ca_modulus = np.sqrt((ca*ca).sum()) ba = b - a ba_modulus = np.sqrt((ba*ba).sum()) cab_cos_angle = np.dot(ca,ba) / ca_modulus / ba_modulus print "Angle c-a-b:", np.arccos(cab_cos_angle) # Angle between b-c and a-c bc = b - c bc_modulus = np.sqrt((bc*bc).sum()) ac = a - c ac_modulus = np.sqrt((ac*ac).sum()) bca_cos_angle = np.dot(bc,ac) / bc_modulus / ac_modulus print "Angle b-c-a:", np.arccos(bca_cos_angle)The output should be
Angle a-b-c: 0.298174974907 Angle c-a-b: 2.57187588039 Angle b-c-a: 0.271541798289It is always good programming practice to check your programs. This often requires thinking up and then testing some constraint on the program's calculations. Here we can add up the angles---the sum must be pi. Having just run the program we can simply type the following at the prompt to do the check:
print np.arccos(abc_cos_angle) + np.arccos(cab_cos_angle) + np.arccos(bca_cos_angle)and viola!
3.14159265359
Program to print the normal to the plane defined by the triangle:
# Calculate the vector normal to the plane in which a triangle # lies # from numpy import * # The vertices a = array([0, 1, 0]) b = array([4.3, -2.4, 0.005]) c = array([-3.2, 5.1, -3.]) ba = b - a ca = c - a print "Normal to the triangle's plane:", np.cross(ba,ca)Which generates:
Normal to the triangle's plane: [ 10.1795 12.884 6.75 ]