How to find surface normal of a triangle

The cross product of two sides of the triangle equals the surface normal. So, if vector $V$ = $P_2$ - $P_1$, vector $W$ = $P_3$ - $P_1$, and vector $N$ is the surface normal, then:

$N_x = (V_y * W_z) - (V_z * W_y)$

$N_y = (V_z * W_x) - (V_x * W_z)$

$N_z = (V_x * W_y) - (V_y * W_x)$

If $A$ is the new vector whose components add up to 1, then:

$A_x = \frac {N_x}{(N_x)^2 + (N_y)^2 + (N_z)^2}$

$A_y = \frac {N_y}{(N_x)^2 + (N_y)^2 + (N_z)^2}$

$A_z = \frac {N_z}{(N_x)^2 + (N_y)^2 + (N_z)^2}$

My sources: http://en.wikipedia.org/wiki/Normal_(geometry)


Let $P_1=(x_1,y_1,z_1)$, $P_2=(x_2,y_2,z_2)$ and $P_3=(x_3,y_3,z_3)$. The normal vector to the triangle with these three points as its vertices is then given by the cross product $n=(P_2-P_1)\times (P_3-P_1)$. In matrix form, we then see that $$n=\det\left(\left[\begin{matrix}i&j&k\\ x_2-x_1&y_2-y_1&z_2-z_1\\ x_3-x_1&y_3-y_1&z_3-z_1 \end{matrix}\right]\right)$$

$$=\left(\begin{matrix}(y_2-y_1)(z_3-z_1)-(y_3-y_1)(z_2-z_1)\\ (z_2-z_1)(x_3-x_1)-(x_2-x_1)(z_3-z_1)\\ (x_2-x_1)(y_3-y_1)-(x_3-x_1)(y_2-y_1) \end{matrix}\right)$$

If you need that the sum of the coefficients of $\hat{n}$ equals 1, then set $\alpha$ equal to the sum of the coefficients of $n$ and then let $\hat{n}=\frac{1}{\alpha}n$. Obviously, if $\alpha=0$ then you will never be able to satisfy your condition as any scalar multiple of $n$ will have the same zero-sum of coefficients.


The general question can be written in python like this:

import numpy as np
p1 = np.array([0,0,5])
p2 = np.array([1,0,5])
p3 = np.array([0,1,5])

N = np.cross(p2-p1, p3-p1)

The 'normalized' sum of 1 can be attempted like this:

N = N / N.sum()

For the (0,0,0), (1,1,0), (0,0,1) example mentioned above, where the components add up to 0, the result silently degrades to array([ inf, -inf, nan]).