- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
给定顶点的位置和表面法线。如何计算两个三角形接触的面积(可能为 0)?这些三角形也在 3D 空间中,所以如果它们没有正确排列,只是相互挤压,接触面积应该为 0。(在 C# 中)
最佳答案
这不是一个微不足道的问题,所以让我们把它分解成几个步骤。
要使两个三角形共面,一个三角形的所有顶点必须位于另一个三角形确定的平面内。
使用描述的算法 here我们可以检查每个顶点是否是这种情况,但由于 float 不是完全精确的事实,您将需要定义一些错误阈值以确定仍然算作共面的东西。
假设 va
和 vb
分别是三角形 A 和 B 的向量,代码可能如下所示。
(注意:我从未使用过 Unity,所有代码都是靠内存编写的,所以如果不是 100% 正确,请原谅)。
public static bool AreTrianglesCoplanar(Vector3[] va, Vector3[] vb) {
// make sure these are actually triangles
Debug.Assert(va.Length == 3);
Debug.Assert(vb.Length == 3);
// calculate the (scaled) normal of triangle A
var normal = Vector3.Cross(va[1] - va[0], va[2] - va[0]);
// iterate all vertices of triangle B
for(var vertex in vb) {
// calculate the dot product between the normal and the vector va[0] --> vertex
// the dot product will be 0 (or very small) if the angle between these vectors
// is a right angle (90°)
float dot = Vector3.Dot(normal, vertex - va[0]).
// the error threshold
const float epsilon = 0.001f;
// if the dot product is above the threshold, the vertex lies outside the plane
// in that case the two triangles are not coplanar
if(Math.Abs(dot) > epsilon)
return false;
}
return true;
}
我们现在知道所有六个顶点都在同一个嵌入到 3D 空间的 2D 平面中,但是我们所有的顶点坐标仍然是三维的。所以下一步是将我们的点投影到 2D 坐标系中,这样它们的相对位置就会被保留。
This answer很好地解释了数学。
首先,我们需要找到一组构成正交基的三个向量(它们必须彼此正交且长度为 1)。
va[0]
到 va 的向量[1]
) 并将其归一化。我们还需要在平面上选择一个点作为我们的原点,例如 va[0]
。
有了所有这些参数,并使用链接 amswer 中的公式,我们可以从其他答案)。请注意——因为我们所有的点都位于定义该法向量的平面内——第三个坐标(在另一个答案中称为 s
)将始终(接近)零。
public static void ProjectTo2DPlane(
Vector3[] va, Vector3[] vb
out Vector2[] vaProjected, out Vector2[] vbProjecte
) {
// calculate the three coordinate system axes
var normal = Vector3.Cross(va[1] - va[0], va[2] - va[0]).normalized;
var e1 = Vector3.Normalize(va[1] - va[0]);
var e2 = Vector3.Cross(normal, e1);
// select an origin point
var origin = va[0];
// projection function we will apply to every vertex
Vector2 ProjectVertex(Vector3 vertex) {
float s = Dot(normal, vertex - origin);
float t1 = Dot(e1, vertex - origin);
float t2 = Dot(e2, vertex - origin);
// sanity check: this should be close to zero
// (otherwise the point is outside the plane)
Debug.Assert(Math.Abs(s) < 0.001);
return new Vector2(t1, t2);
}
// project the vertices using Linq
vaProjected = va.Select(ProjectVertex).ToArray();
vbProjected = vb.Select(ProjectVertex).ToArray();
}
健全性检查:
va[0]
应该投影到 (0, 0)。va[1]
应该被投影到 (*, 0),所以在新的 x 轴上的某个地方。 This answer这个答案提到了最后一步所需的算法。
Sutherland-Hodgman algorithm连续剪辑一个三角形与另一边的每一边。结果将是三角形、四边形或空多边形。
最后,shoelace formula可用于计算生成的裁剪多边形的面积。
假设您实现了 CalculateIntersecionPolygon
和 CalculatePolygonArea
这两个函数,最终的交集面积可以这样计算:
public static float CalculateIntersectionArea(Mesh triangleA, Mesh triangleB) {
var verticesA = triangleA.GetVertices();
var verticesB = triangleB.GetVertices();
if(!AreTrianglesCoplanar(verticesA, verticesB))
return 0f; // the triangles are not coplanar
ProjectTo2DPlane(verticesA, verticesB, out Vector2[] projectedA, out Vector2[] projectedB);
CalculateIntersecionPolygon(projectedA, projectedB, out List<Vector2> intersection);
if(intersection.Count == 0)
return 0f; // the triangles didn't overlap
return CalculatePolygonArea(intersection);
}
关于c# - 怎么求两个三角形相交的面积,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66614852/
for (i = 0; i <= 1000; i++) { if ( i % 3 === 0){ console.log(i); } if ( i % 5 ==
对于一项作业,我需要解决一个数学问题。我将其缩小为以下内容: 令 A[1, ... ,n] 为 n 整数数组。 令y 为整数常量。 现在,我必须编写一个算法,在 O(n) 时间内找到 M(y) 的最小
我可以使用 iOS MediaPlayer 并通过这种方式播放电影。但我需要,寻找一秒钟的电影。我该怎么做,我像这样通过 MediaPlayer 播放电影: NSURL *videoURL =
我听说过 eCos看起来作为一个爱好项目来玩会很有趣。 任何人都可以推荐一个价格合理的开发板。如果它不会增加太多成本,我想要几个按钮来按下(并以编程方式检测按下)和一些调试输出的 LCD。以太网会很好
给定 a 到 b 的范围和数字 k ,找到 a 到 b [包括两者]之间的所有 k-素数。 k-素数的定义:如果一个数恰好有 k 个不同的素数因子,则该数是 k-素数。 即 a=4 , b=10 k=
这是对 my previous question 的重新措辞尝试作为它收到的反馈的结果。 我想要一个简单的网络通信,我可以将其用作底层框架,而无需再次查看。我只想将一个字符串从一台 PC 推送到另一台
我有许多节点通过其他类型的中间节点连接。如图所示,中间节点可以有多个。我需要找到给定数量的节点的所有中间节点,并按初始节点之间的链接数量对其进行排序。在我的示例中,给定 A、B、C、D,它应该返回节点
我的代码遇到问题。我试图找到这个 5x5 数组的总和,但它总是给我总计 0。当我使用 2x2 数组时,它可以工作,但对于 5x5 数组则不起作用。有人可以帮忙吗? import java.util.*
我们有一个给定的数组,我们想要打印 BST 中每个节点的级别。 例如,如果给定数组为:{15, 6, 2, 10, 9, 7, 13} 那么答案是: 1 2 3 3 4 5 4 (表示存储15的节点级
我对 R 和编程非常陌生,所以请留在我身边:) 我正在尝试使用迭代来查找无限迭代到小数点后第四位的值。 IE。其中小数点后第四位不变。所以 1.4223,其中 3 不再改变,所以小数点后 3 位的结果
我的问题与 Fastest way of computing the power that a "power of 2" number used? 非常相似: 将 x=2^y 作为输入,我想输出 y。
如何找到三个非零数字中最小的一个。 我尝试引入一个非常小的数字eps = 1e-6(我的数字为零或明显大于eps)并在min(x,eps)、min(y,eps)之间进行测试)等我什么也没得到。有没有办
我有一个类(class),他们计算矩阵中最大的“1”岛,但他的岛概念是“如果两个单元在水平、垂直或对角线上彼此相邻,则称它们是相连的。 “ 我需要帮助来删除对角台阶。 class GFG {
我开始使用 IDE Jupyter && Python 3.6 并出现了一个问题。我必须通过IDE绘制Petersen子图中的哈密顿路径,但我不知道该怎么做。 我显示有关该图的信息: Petersen
public static void main(String[] args) { int sum = 2; int isPrime; for(int x = 3; x Mat
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: How much time should it take to find the sum of all prime
我想找到给定节点到链表二叉搜索树中根的距离。我有下面的代码来计算树的高度(root.getHeightN()),从根到叶子,但我现在需要的是从叶子到根。 public int getHeightN()
是否有一种优雅的方法使用预先计算的 KDTree 来查找连接组件的数量?现在使用呼吸优先搜索算法以及 k 最近邻的 KDTree 给出的邻接矩阵来查找连接的组件,但是是否有更好的可能性? import
我有一个要求,我需要找到具有相同名称的不同对象中 amt 值的总和。下面是代码片段 traveler = [ { description: 'Senior', Amount: 50}, {
我正在尝试使用 pandas 对某些列进行求和,同时保留其他列。例如: member_no, data_1, data_2, data_3, dat_1, dat_2, other_1, other_
我是一名优秀的程序员,十分优秀!