我正在尝试计算位于平面上的多边形的面积(形成不相交的闭合形状的共面点的集合),并且我知道一种可以计算 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/