- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试计算位于平面上的多边形的面积(形成非相交封闭形状的共面点的集合),并且我知道一种可以计算 area of an irregular (or any) polygon in two dimensions 的方法- 但不是三个。我的解决方案是旋转平面,使其在 z 方向上的法线为 0(因此我可以将其视为 2D),然后运行 2D 区域函数。
问题是我不知道如何实际确定旋转轴以及如何在 Z 轴上展平平面。我通过我能找到的最简单的 3 维旋转方法进行旋转:Rotation Matrices .因此,鉴于我正在尝试使用旋转矩阵进行旋转,我如何计算出旋转平面的角度,使其与另一个向量的方向相同?我实际上不太了解微积分或欧几里得几何,因此无论哪种解决方案都需要我自学最少的两者都是理想的解决方案。有没有更好的办法?
下面是我的尝试,它甚至没有接近使平面在 Z 轴上平坦。这是我的“Surface”类的一个实例方法,它是我的“Plane”类的派生类,并且有一个共面点数组 (IntersectPoints) 形成一个封闭的多边形。
public virtual double GetArea()
{
Vector zUnit = new Vector(0, 0, 1); //vector perprendicualr to z
Vector nUnit = _normal.AsUnitVector();
Surface tempSurface = null;
double result = 0;
if (nUnit != zUnit && zUnit.Dot(nUnit) != 0) //0 = perprendicular to z
{
tempSurface = (Surface)Clone();
double xAxisAngle = Vector.GetAxisAngle(nUnit, zUnit, Physics.Formulae.Axes.X);
double yAxisAngle = Vector.GetAxisAngle(nUnit, zUnit, Physics.Formulae.Axes.Y);
double rotationAngle = Vector.GetAxisAngle(nUnit, zUnit, Physics.Formulae.Axes.Z);
tempSurface.Rotate(xAxisAngle, yAxisAngle, rotationAngle); //rotating plane so that it is flat on the Z axis
}
else
{
tempSurface = this;
}
for (int x = 0; x < tempSurface.IntersectPoints.Count; x++) //doing a cross sum of each point
{
Point curPoint = tempSurface.IntersectPoints[x];
Point nextPoint;
if (x == tempSurface.IntersectPoints.Count - 1)
{
nextPoint = tempSurface.IntersectPoints[0];
}
else
{
nextPoint = tempSurface.IntersectPoints[x + 1];
}
double cross1 = curPoint.X * nextPoint.Y;
double cross2 = curPoint.Y * nextPoint.X;
result += (cross1 - cross2); //add the cross sum of each set of points to the result
}
return Math.Abs(result / 2); //divide cross sum by 2 and take its absolute value to get the area.
}
这是我的核心旋转和获取轴角度的方法:
private Vector Rotate(double degrees, int axis)
{
if (degrees <= 0) return this;
if (axis < 0 || axis > 2) return this;
degrees = degrees * (Math.PI / 180); //convert to radians
double sin = Math.Sin(degrees);
double cos = Math.Cos(degrees);
double[][] matrix = new double[3][];
//normalizing really small numbers to actually be zero
if (Math.Abs(sin) < 0.00000001)
{
sin = 0;
}
if (Math.Abs(cos) < 0.0000001)
{
cos = 0;
}
//getting our rotation matrix
switch (axis)
{
case 0: //x axis
matrix = new double[][]
{
new double[] {1, 0, 0},
new double[] {0, cos, sin * -1},
new double[] {0, sin, cos}
};
break;
case 1: //y axis
matrix = new double[][]
{
new double[] {cos, 0, sin},
new double[] {0, 1, 0},
new double[] {sin * -1, 0, cos}
};
break;
case 2: //z axis
matrix = new double[][]
{
new double[] {cos, sin * -1, 0},
new double[] {sin, cos, 0},
new double[] {0, 0, 1}
};
break;
default:
return this;
}
return Physics.Formulae.Matrix.MatrixByVector(this, matrix);
}
public static double GetAxisAngle(Point a, Point b, Axes axis, bool inDegrees = true)
{ //pretty sure this doesnt actually work
double distance = GetDistance(a, b);
double difference;
switch (axis)
{
case Axes.X:
difference = b.X - a.X;
break;
case Axes.Y:
difference = b.Y - a.Y;
break;
case Axes.Z :
difference = b.Z - a.Z;
break;
default:
difference = 0;
break;
}
double result = Math.Acos(difference / distance);
if (inDegrees == true)
{
return result * 57.2957; //57.2957 degrees = 1 radian
}
else
{
return result;
}
}
最佳答案
一个可靠的方法是对每条边的顶点进行叉积求和。如果您的顶点共面,这将产生平面的法线,其长度是闭合多边形面积的 2 倍。
请注意,此方法与您问题中链接的 2D 方法非常相似,后者实际上计算 3D 叉积的 2D 等效值,对所有边求和,然后除以 2。
Vector normal = points[count-1].cross(points[0]);
for(int i=1; i<count; ++i) {
normal += points[i-1].cross(points[i]);
}
double area = normal.length() * 0.5;
这种方法的优点:
一个可能的困难:如果您的多边形非常小,并且离原点很远,您可能会遇到浮点精度问题。如果可能出现这种情况,您应该首先平移所有顶点,以便一个顶点位于原点,如下所示:
Vector normal(0,0,0);
Vector origin = points[count-1];
for(int i=1; i<count-1; ++i) {
normal += (points[i-1]-origin).cross(points[i]-origin);
}
double area = normal.length() * 0.5;
关于c# - 计算平面上封闭多边形的面积,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20672183/
从the documentation, 13.5.5: When the last parameter of a method is a closure, you can place the clos
Bjarne Stroustrup 写道: “友元类必须事先在封闭范围内声明或在非类范围内定义,立即封闭声明它为友元的类” 语句的第一部分不是多余的,因为“立即包含类的非类范围”包括“先前在封闭范围中
我有一个网格(如下例),其中包含外墙(标记为 W)、环境 block (E)、开放空间 (o) 和事件点 (A)。目前,此网格存储在 [,] 中,其中包含与给定点关联的所有数据。我试图确定是否包含一个
我正在尝试使用 this blogpost's approach to higher-kinded data without dangling Identity functors for the tr
在下面的代码中: package main import ( "fmt" "time" ) func asChan(vs ...int) <-chan int { c := m
我在传递和评估闭包列表时遇到困难。经过大量简化,该程序显示出与我正在尝试编写的程序相同的错误: use std::vec::flat_map; #[main] fn main() { let li
我正在努力成为一名好公民,并尽可能远离全局范围。有没有办法访问不在全局范围内的 setTimeout 变量? 因此,在此示例中,某人将如何取消“计时器”? myObject.timedAction =
考虑这个例子: def A(): b = 1 def B(): # I can access 'b' from here. print(b)
val listPlans: List = newPlans.mapTry { it.data.map { Plan(it.id, it.nam
我目前正在尝试使用SinonJS对我的 angular.service 进行单元测试,但是遇到了一个问题,希望有人可以阐明为什么会发生这种情况。我已经重构了当前的项目以说明当前的问题。 我还提供了DE
我正在使用 Go channel ,我想知道关闭 channel 和将其设置为 nil 之间有什么区别? 编辑: 在此example ,我想通过关闭 channel 或设置为零来断开发送者和接收者的连
我的应用程序有一个奇怪的行为,我不知道它来自哪里。我已经为 TextView 内容实现了 NSScanner,效果非常好。扫描器与文本存储结合使用,通过 TextView 委托(delegate)方法
我不知道如何让 MyBatis 生成封闭的 or 语句: WHERE x.token = ? AND ( (x.scene = 'A' OR x.scene = 'B')) 这是一个令人惊讶的简单
我不希望这是一个摄像头检测程序。这是一个程序,可以检测应用程序屏幕上颜色的传递。 我想要做的是检测大于 5x5 像素的黑色何时穿过屏幕上定义的空间区域。我想过用一个大区域来拉伸(stretch)整个宽
我一直在使用 RDFLib 来解析数据并将其插入到三元组中。我遇到的一个常见问题是,从关联数据存储库解析时,没有尖括号括起 URL。 要上传数据,我必须手动添加 并使用 URIRef重新创建 URL。
我已经阅读了很多有关此问题的帖子,但我仍然不确定我是否完全理解这些定义。 以下是我认为不同术语的示例。我是否走在正确的轨道上,或者我仍然不理解这些概念。谢谢 Array - unbound and o
我为我的 Android 应用设置了 GooglePlay 内部和封闭式 Alpha 测试设置。 它非常适合允许测试人员加入计划并安装应用程序,但是当我从测试人员电子邮件列表中删除测试人员时,他们仍然
我是一名优秀的程序员,十分优秀!