38

Given three 3D points (A,B, & C) how do I calculate the normal vector? The three points define a plane and I want the vector perpendicular to this plane.

Can I get sample C# code that demonstrates this?

Ronen Ness
  • 9,923
  • 4
  • 33
  • 50
DenaliHardtail
  • 27,362
  • 56
  • 154
  • 233
  • 8
    Math has a lot to do with programming. Especially this math, if you're doing anything 3D. – Todd Gamblin Dec 27 '09 at 18:10
  • 14
    Give him a break, it's marked C# and .net-3.5. This is a problem that comes up immediately when you start *programming* 3D apps. Look at the related questions, we usually allow this stuff. – Frank Krueger Dec 27 '09 at 18:31
  • 1
    My decision line is: will the math that is being asked about be implemented *in code*. As it will, this gets to stay. – dmckee --- ex-moderator kitten May 25 '10 at 06:55
  • 1
    @NoonSilk: StackOverflow postings are read by other visitors, possibly a long time after they have been written (as you can see by my finding this question six years after your comment). Keeping questions that ought to be closed in a *closed* state and questions that ought to remain open in an *open* state is just a part of the usual curating to keep everything tidy for future visitors. – O. R. Mapper Sep 15 '16 at 10:32

3 Answers3

49

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;
        }
    }
}
Frank Krueger
  • 69,552
  • 46
  • 163
  • 208
6

Form the cross-product of vectors BA and BC. See http://mathworld.wolfram.com/CrossProduct.html.

Steve Emmerson
  • 7,702
  • 5
  • 33
  • 59
2

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.

Todd Gamblin
  • 58,354
  • 15
  • 89
  • 96