[docs]defnormal_polygon(polygon,unitized=True):"""Compute the normal of a polygon defined by a sequence of points. Parameters ---------- polygon : list of list A list of polygon point coordinates. Returns ------- list The normal vector. Raises ------ ValueError If less than three points are provided. Notes ----- The points in the list should be unique. For example, the first and last point in the list should not be the same. """p=len(polygon)assertp>2,"At least three points required"nx=0ny=0nz=0o=centroid_points(polygon)a=polygon[-1]oa=subtract_vectors(a,o)foriinrange(p):b=polygon[i]ob=subtract_vectors(b,o)n=cross_vectors(oa,ob)oa=obnx+=n[0]ny+=n[1]nz+=n[2]ifnotunitized:returnnx,ny,nzreturnnormalize_vector([nx,ny,nz])
[docs]defnormal_triangle(triangle,unitized=True):"""Compute the normal vector of a triangle. Parameters ---------- triangle : list of list A list of triangle point coordinates. Returns ------- list The normal vector. Raises ------ ValueError If the triangle does not have three vertices. """assertlen(triangle)==3,"Three points are required."a,b,c=triangleab=subtract_vectors(b,a)ac=subtract_vectors(c,a)n=cross_vectors(ab,ac)ifnotunitized:returnnlvec=length_vector(n)returnn[0]/lvec,n[1]/lvec,n[2]/lvec
[docs]defnormal_triangle_xy(triangle,unitized=True):"""Compute the normal vector of a triangle assumed to lie in the XY plane. Parameters ---------- triangle : list of list A list of triangle point coordinates. Z-coordinates are ignored. Returns ------- list The normal vector, which is a vector perpendicular to the XY plane. Raises ------ ValueError If the triangle does not have three vertices. """a,b,c=triangleab=subtract_vectors_xy(b,a)ac=subtract_vectors_xy(c,a)n=cross_vectors_xy(ab,ac)ifnotunitized:returnnlvec=length_vector_xy(n)returnn[0]/lvec,n[1]/lvec,n[2]/lvec