【发布时间】:2016-08-26 18:30:53
【问题描述】:
在给定一组 3D 顶点的情况下,我希望能够计算任何形状的 2D 多边形的表面积。例如,这个图形的表面积是多少?
var polygon = new Polygon([new Point(0,0,0), new Point(5,8,2), new Point(11,15,7)])
polygon.areaIfPolygonIs3D()
--> some predictable result, no matter how many vertices the polygon has...
请记住,多边形只有一个表面。它们是扁平的,但可以是三角形或梯形或随机形状,并且可以以 3D 角度浮动……将它们想象为在 3D 空间中任意转动的纸片。
到目前为止,我尝试做的是将物体旋转平,然后使用基本公式计算当前在我的代码中工作的 2D 不规则多边形的面积(公式:http://www.wikihow.com/Calculate-the-Area-of-a-Polygon)。我很难弄清楚如何旋转所有顶点,使多边形平放(所有“z”值都是 0),所以我放弃了这条路,但如果有人能到达那里,我愿意尝试。 (也许 Point.rotateBy() 中存在错误。)
我可以使用点和边(使用 point.to(point) 创建),边有“theta”(edge.theta())和“phi”(edge.phi())。
无论如何,如果有人可以填写这里的内容并在我花了一整天的努力重新学习我从高中忘记的所有几何图形后帮助我,那将不胜感激!
var locatorRho = function(x,y,z) {
return Math.sqrt(x*x + y*y + z*z);
}
var locatorTheta = function(x,y) {
return Math.atan2(y,x);
};
var locatorPhi = function(x,y,z) {
return z == 0 ? Math.PI_2 : Math.acos(z/locatorRho(x, y, z));
}
// rotates a point according to another point ('locator'), and their 2D angle ('theta') and 3D angle ('phi')
Point.prototype.rotateBy = function(locator, theta, phi) {
phi = (phi == undefined ? 0 : phi);
var relativeX = this.x() - locator.x();
var relativeY = this.y() - locator.y();
var relativeZ = this.z() - locator.z();
var distance = locatorRho(relativeX, relativeY, relativeZ);
var newTheta = locatorTheta(relativeX, relativeY) + theta;
var newPhi = locatorPhi(relativeX, relativeY, relativeZ) + phi;
this._x = locatorX(distance, newTheta, newPhi) + locator.x();
this._y = locatorY(distance, newTheta, newPhi) + locator.y();
this._z = locatorZ(distance, newPhi) + locator.z();
}
Polygon.prototype.signedArea = function() {
var vertices = this.vertices();
var area = 0;
for(var i=0, j=1, length=vertices.length; i<length; ++i, j=(i+1)%length) {
area += vertices[i].x()*vertices[j].y() - vertices[j].x()*vertices[i].y();
}
return 0.5*area
}
Polygon.prototype.areaIfPolygonIs2D = function() {
return Math.abs(rotatedFlatCopy.signedArea())
}
Polygon.prototype.areaIfPolygonIs3D = function() {
... help here I am so stuck ...
}
var vertices = [some number of Points, e.g., new Point(x,y,z)]
var polygon = new Polygon(vertices)
var polygon.areaIfPolygonIs3D()
--> result
【问题讨论】:
-
所以你是说 3d 中的所有点都在一个平面上,你对这个平面上这些点所包围的区域感兴趣,对吧?或者您对投影面积感兴趣。
标签: javascript math geometry polygon area