计算多边形面积 [英] Calculating Polygon Area

查看:97
本文介绍了计算多边形面积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我已经在javascript中获得了这段代码,可以从网上计算出不规则的多边形面积.

So I've gotten this code in javascript to calculate irregular polygon area from the net.

function polygonArea(X, Y, numPoints)  
{    
area = 0;  // Accumulates area in the loop   
j = numPoints-1;  // The last vertex is the 'previous' one to the first

  for (i=0; i<numPoints; i++)
  { area = area +  (X[j]+X[i]) * (Y[j]-Y[i]); 
      j = i;  //j is previous vertex to i
  }   
  return area/2; 
}

var xPts = [3, 3, 2, 2, 3, 3, 6, 6, 9, 9, 4, 4 ];
var yPts = [2, 4, 4, 5, 5, 6, 6, 5, 5, 3, 3, 2];

var a = polygonArea(xPts, yPts, 4); 
alert("Area  = " + a);

结果似乎是正确的.如果顶点按顺时针方向跟踪,它将显示正结果,但是如果我沿逆时针方向跟踪顶点,它将变为负结果.为什么会这样?

The results seems to be correct. if the vertex traced by clock-wise direction, it will shows positive results however it will become negative if i traced vertex in anti clockwise direction. Why is that so?

此算法如何工作?我真的很想知道其背后的数学解释是什么,因为我仍然很难理解网上的解释.

How does this algorithm work? i really want to know what is the mathematical explanation behind it, because i am still having a hard time to understand the explanation on the net.

推荐答案

想象一下从每个顶点到Y轴绘制水平线;对于每个边缘,这将描述一个梯形:

Imagine drawing horizontal lines from each vertex to the Y axis; for each edge, this will describe a trapezoid:

Y-axis
^
|
|--------o (X[j], Y[j])
|         \
|          \
|           \
|------------o (X[i], Y[i])
|
+----------------------------> X-axis

如果Y[i] <= Y[j],则内部循环中的公式(X[j]+X[i]) * (Y[j]-Y[i])计算该梯形面积的两倍;如果Y[i] >= Y[j],则计算面积两倍.

The formula (X[j]+X[i]) * (Y[j]-Y[i]) in the inner loop computes twice the area of this trapezoid if Y[i] <= Y[j], or negative twice the area if Y[i] >= Y[j].

对于封闭的多边形,这自然会从下降"边缘左侧的面积中减去上升"边缘左侧的面积.如果多边形是顺时针方向,则将整齐地切出多边形的精确区域(加倍).如果逆时针旋转,则得到负(加倍)区域.

For a closed polygon, this naturally subtracts the area to the left of the "upgoing" edges from the area to the left of the "downgoing" edges. If the polygon is clockwise, this neatly cuts out the exact (doubled) area of the polygon; if counterclockwise, you get the negative (doubled) area.

要计算给定多边形的面积,

To compute the area of a given polygon,

Y-axis
^
|
|        o------o
|        |       \
|        |        \
|        o         \
|         \         o                  
|          \       /
|           \     /
|            \   /
|             \ /
|              o
|
+-------------------------> X-axis

进入下降区域:

Y-axis
^
|
|--------o------o
|                \
|                 \
|        o         \
|                   o                  
|                  /
|                 /
|                /
|               /
|--------------o
|
+-------------------------> X-axis

减去上行区域:

Y-axis
^
|
|--------o      o
|        |
|        |
|        o
|         \         o                  
|          \
|           \
|            \
|             \
|--------------o
|
+-------------------------> X-axis

尽管上面的示例使用了凸多边形,但是此面积计算对于任意多边形都是正确的,而不管它们可能具有多少个上下行路径.

Although the above example uses a convex polygon, this area computation is correct for arbitrary polygons, regardless of how many up- and down- paths they may have.

这篇关于计算多边形面积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆