def prog ( ): import matplotlib.pyplot as plt import numpy as np t = np.array ( [ \ [ 4, 1 ], \ [ 8, 3 ], \ [ 0, 9 ] ] ) print ( t ) centroid = np.mean ( t, axis = 0 ) print ( centroid ) print ( triangle_sides ( t ) ) angles = triangle_angles ( t ) print ( angles ) print ( angles * 180 / np.pi ) area = triangle_area ( t ) print ( area ) area = triangle_area_signed ( t ) print ( area ) t1 = np.array ( [ \ [ 4, 1 ], \ [ 0, 9 ], \ [ 8, 3 ] ] ) area = triangle_area_signed ( t1 ) print ( area ) print ( triangle_orientation ( t ) ) print ( triangle_orientation ( t1 ) ) print ( triangle_contains_point ( t, [ 1, 2 ] ) ) print ( triangle_contains_point ( t, [ 2, 5 ] ) ) print ( triangle_contains_point ( t, [ 4, 4 ] ) ) return def line_side ( p0, p1, q ): import numpy as np v0 = p1 - p0 v1 = q - p0 v0xv1 = float ( np.cross ( v0, v1 ) ) return v0xv1 def triangle_angles ( t ): import numpy as np s = triangle_sides ( t ) angle = np.zeros ( 3 ) for i in range ( 0, 3 ): ip1 = ( i + 1 ) % 3 ip2 = ( i + 2 ) % 3 top = s[i]**2 + s[ip2]**2 - s[ip1]**2 bot = 2.0 * s[i] * s[ip2] angle[i] = np.arccos ( top / bot ) return angle def triangle_area ( t ): import numpy as np s = triangle_sides ( t ) abc = 0.5 * np.sum ( s ) area = np.sqrt ( abc * ( abc - s[0] ) * ( abc - s[1] ) * ( abc - s[2] ) ) return area def triangle_area_signed ( t ): import numpy as np area = 0.5 * np.cross ( ( t[1,:] - t[0,:] ), ( t[2,:] - t[0,:] ) ) return area def triangle_contains_point ( t, q ): value = True for i in range ( 0, 3 ): ip1 = ( i + 1 ) % 3 if ( line_side ( t[i,:], t[ip1,:], q ) < 0.0 ): value = False return value return value def triangle_orientation ( t ): import numpy as np area = triangle_area_signed ( t ) if ( 0 <= area ): orientation = +1 else: orientation = -1 return orientation def triangle_sides ( t ): import numpy as np s = np.zeros ( 3 ) for i in range ( 0, 3 ): ip1 = ( i + 1 ) % 3 ip2 = ( i + 2 ) % 3 s[i] = np.linalg.norm ( t[ip2,:] - t[ip1,:] ) return s prog ( )