Given 3 points, how do I calculate the normal vector?
You need to calculate the cross product of any two non-parallel vectors on the surface. Since you have three points, you can figure this out by taking the cross product of, say, vectors AB and AC.
When you do this, you're calculating a surface normal, of which Wikipedia has a pretty extensive explanation.
Form the cross-product of vectors BA and BC. See http://mathworld.wolfram.com/CrossProduct.html.
It depends on the order of the points. If the points are specified in a counter-clockwise order as seen from a direction opposing the normal, then it's simple to calculate:
Dir = (B - A) x (C - A)
Norm = Dir / len(Dir)
where x
is the cross product.
If you're using OpenTK or XNA (have access to the Vector3 class), then it's simply a matter of:
class Triangle {
Vector3 a, b, c;
public Vector3 Normal {
get {
var dir = Vector3.Cross(b - a, c - a);
var norm = Vector3.Normalize(dir);
return norm;
}
}
}