给定一组x,y坐标(x1,y1,x2,y2,.......xn,yn),如何求以这些点围成的多边形的面积,公式是什么?代码如何组织?

解决方案 »

  1.   

    给个算法你:
    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;
      }
      

  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();
    }