C# 给定 3 分,我如何计算法向量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1966587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:16:19  来源:igfitidea点击:

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

c#math.net-3.5geometry

提问by DenaliHardtail

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.

给定三个 3D 点(A、B 和 C),我如何计算法向量?这三个点定义了一个平面,我希望向量垂直于这个平面。

Can I get sample C# code that demonstrates this?

我可以获得演示这一点的示例 C# 代码吗?

采纳答案by Frank Krueger

It depends on the order of the points. If the points are specified in a counter-clockwise order as seen from a direction opposingthe normal, then it's simple to calculate:

这取决于点的顺序。如果从与法线相反的方向看,以逆时针顺序指定点,那么计算起来很简单:

Dir = (B - A) x (C - A)
Norm = Dir / len(Dir)

where xis the cross product.

哪里x是叉积。

If you're using OpenTK or XNA (have access to the Vector3 class), then it's simply a matter of:

如果您使用的是 OpenTK 或 XNA(可以访问 Vector3 类),那么这只是一个问题:

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

回答by Steve Emmerson

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

形成向量 BA 和 BC 的叉积。请参阅http://mathworld.wolfram.com/CrossProduct.html

回答by Todd Gamblin

You need to calculate the cross productof 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.

您需要计算表面上任意两个非平行向量的叉积。由于您有三个点,因此您可以通过对向量 AB 和 AC 进行叉积来计算。

When you do this, you're calculating a surface normal, of which Wikipedia has a pretty extensive explanation.

当你这样做时,你正在计算一个表面法线,维基百科有一个非常广泛的解释。