Given 3 points, how do I calculate the normal vector?

前端 未结 3 1470
忘掉有多难
忘掉有多难 2021-02-01 03:07

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

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-01 03:56

    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;
            }
        }
    }
    

提交回复
热议问题