方法一:
float area_of_polygon(int vcount,float x[],float y[]) { int i; float s; if (vcount <3) return 0; s=y[0]*(x[vcount-1]-x[1]); for (i=1;i <vcount;i++) s+=y[i]*(x[(i-1)]-x[(i+1)%vcount]); return s/2; }
方法二:
算法版大牛海星原创
public float area_of_polygon(Point[] APoints) { if (APoints.Length < 3) return 0; float s = APoints[0].Y * (APoints[APoints.Length - 1].X - APoints[1].X); for (int i = 1; i < APoints.Length; i++) s += APoints[i].Y * (APoints[(i - 1)].X - APoints[(i + 1) % APoints.Length].X); return System.Math.Abs(s / 2); } private void button1_Click(object sender, EventArgs e) { Text = area_of_polygon(new Point[] { new Point(0, 0), new Point(0, 10), new Point(10, 10), new Point(10, 0)}).ToString(); }
方法三:
生成多边形,其实就是指定这些顶点的顺序 在确定了顺序后, 假定是p0, p2, ...., pn-1 那么多边形的面积可以简单的按照如下公式计算 float area = 0; for (int i=0, j=1; i <n; i++, j++) { j = j % n; //因为当j=n时候,需要让j回归1 area += p[i].x*p[j].y - p[i].y*p[j].x; } area = fabs( area * 0.5);//取1/2再取绝对值,因为求出来的面积值可能为负(各顶点顺时针排列的时候)