多边形面积计算

import flash.geom.Point;

import flash.display.Sprite;

/*

A(x1,y1)、B(x2,y2)、C(x3,y3)三点的面积公式为

            |x1   x2   x3|

S(A,B,C) =  |y1   y2   y3| * 0.5 = [(x1-x3)*(y2-y3) - (x2-x3)*(y1-y3)]*0.5 ;(当三点为逆时针时为正,顺时针则为负的) 

            |1    1    1 |

//S(A,B,C) = Math.abs((x1-x3)*(y2-y3)-(x2-x3)*(y1-y3))*0.5 ;



对多边形A1A2A3、、、An(顺或逆时针都可以),设平面上有任意的一点P,则有:

S(A1,A2,A3,、、、,An) = abs(S(P,A1,A2) + S(P,A2,A3)+、、、+S(P,An,A1))

P是可以取任意的一点,用(0,0)时就是下面的了:

设点顺序 (x1 y1) (x2 y2)    ... (xn yn)

则面积等于

                 |x1   y1 |     |x2   y2|            |x(n-1)   y(n-1)|   |xn   yn|

       Math.abs( |        | +   |       | + ...... + |               | + |       | )*0.5

                 |x2   y2 |     |x3   y3|            |xn         yn  |   |x1   y1|





		其中

       |x1   y1|

       |       |=x1*y2-y1*x2

       |x2   y2|

	   |x2   y2|

       |       |=x2*y3-y2*x3

       |x3   y3|

	   |x(n-1)   y(n-1)|

       |               |=x(n-1)*yn-y(n-1)*xn

       |xn         yn  |

	   |xn   yn|

       |       |=xn*y1-yn*x1

       |x1   y1|

	   

因此面积公式展开为:



         |x1    y1|      |x2     y2|         |x(n-1)   y(n-1)|    |xn   yn|

Math.abs(|        |   +  |         |+ ... +  |               |  + |       | )*0.5=Math.abs((x1*y2-y1*x2)+(x2*y3-y2*x3)+...+(x(n-1)*yn-y(n-1)*xn)+(xn*y1-yn*x1))*0.5;

         |x2    y2|      |x3     y3|         |xn         yn  |    |x1   y1|



//Math.abs((x1*y2-y1*x2)+(x2*y3-y2*x3)+...+(x(n-1)*yn-y(n-1)*xn)+(xn*y1-yn*x1))*0.5;

*/

var pt:Point = new Point(0,0);

var pt1:Point = new Point(10,10);

var pt2:Point = new Point(10,60);

var pt3:Point = new Point(60,60);

var pt4:Point = new Point(60,80);

var pt5:Point = new Point(10,80);

var pt6:Point = new Point(10,110);

var pt7:Point = new Point(110,110);

var pt8:Point = new Point(110,60);

var pt9:Point = new Point(160,60);

var pt10:Point = new Point(160,10);



var s:Sprite = new Sprite  ;

addChild(s);

s.graphics.lineStyle(1,0xff0000);

s.graphics.moveTo(pt1.x,pt1.y);

s.graphics.lineTo(pt2.x,pt2.y);

s.graphics.lineTo(pt3.x,pt3.y);

s.graphics.lineTo(pt4.x,pt4.y);

s.graphics.lineTo(pt5.x,pt5.y);

s.graphics.lineTo(pt6.x,pt6.y);

s.graphics.lineTo(pt7.x,pt7.y);

s.graphics.lineTo(pt8.x,pt8.y);

s.graphics.lineTo(pt9.x,pt9.y);

s.graphics.lineTo(pt10.x,pt10.y);

s.graphics.lineTo(pt1.x,pt1.y);







function Matrix3Pt(pt1:Point,pt2:Point,pt3:Point,pt4:Point,pt5:Point,pt6:Point,pt7:Point,pt8:Point,pt9:Point,pt10:Point):Number

{

	//Math.abs((x1*y2-y1*x2)+(x2*y3-y2*x3)+...+(x(n-1)*yn-y(n-1)*xn)+(xn*y1-yn*x1))*0.5;

	var area:Number =Math.abs(

							 (pt1.x*pt2.y-pt1.y*pt2.x)

							+(pt2.x*pt3.y-pt2.y*pt3.x)

							+(pt3.x*pt4.y-pt3.y*pt4.x)

							+(pt4.x*pt5.y-pt4.y*pt5.x)

							+(pt5.x*pt6.y-pt5.y*pt6.x)

							+(pt6.x*pt7.y-pt6.y*pt7.x)

							+(pt7.x*pt8.y-pt7.y*pt8.x)

							+(pt8.x*pt9.y-pt8.y*pt9.x)

							+(pt9.x*pt10.y-pt9.y*pt10.x)

							+(pt10.x*pt1.y-pt10.y*pt1.x)

							)*0.5;

							 

	return area;

}

trace(Matrix3Pt(pt1,pt2,pt3,pt4,pt5,pt6,pt7,pt8,pt9,pt10));

import flash.geom.Point;

import flash.display.Sprite;



var pt0:Point = new Point(10,10);

var pt1:Point = new Point(10,60);

var pt2:Point = new Point(60,60);

var pt3:Point = new Point(60,80);

var pt4:Point = new Point(10,80);

var pt5:Point = new Point(10,110);

var pt6:Point = new Point(110,110);

var pt7:Point = new Point(110,60);

var pt8:Point = new Point(160,60);

var pt9:Point = new Point(160,10);



var s:Sprite = new Sprite  ;

addChild(s);

s.graphics.lineStyle(1,0xff0000);

s.graphics.moveTo(pt0.x,pt0.y);

s.graphics.lineTo(pt1.x,pt1.y);

s.graphics.lineTo(pt2.x,pt2.y);

s.graphics.lineTo(pt3.x,pt3.y);

s.graphics.lineTo(pt4.x,pt4.y);

s.graphics.lineTo(pt5.x,pt5.y);

s.graphics.lineTo(pt6.x,pt6.y);

s.graphics.lineTo(pt7.x,pt7.y);

s.graphics.lineTo(pt8.x,pt8.y);

s.graphics.lineTo(pt9.x,pt9.y);

s.graphics.lineTo(pt0.x,pt0.y);



var arr:Array = new Array  ;

arr.push(pt0);

arr.push(pt1);

arr.push(pt2);

arr.push(pt3);

arr.push(pt4);

arr.push(pt5);

arr.push(pt6);

arr.push(pt7);

arr.push(pt8);

arr.push(pt9);

var count:uint = 0;



trace(polygonArea(arr));



function polygonArea(arr:Array):Number

{

	var i:uint = 0;



	var area:Number = 0;

	var len:uint = arr.length;

	for (i = 0; i < len; i+=2)

	{

		if ((i+2)<len)

		{

			area +=  Matrix3Pt(arr[i],arr[i + 1],arr[i + 2]);

		}

		else

		{

			area +=  Matrix3Pt(arr[i],arr[i + 1],arr[0]);

		}

	}

	//此处注意Math.abs和乘以0.5要在加完之后执行

	return Math.abs(area)*0.5;

}



function Matrix3Pt(pt1:Point,pt2:Point,pt3:Point):Number

{

	var area:Number =(pt1.x*pt2.y-pt1.y*pt2.x)+(pt2.x*pt3.y-pt2.y*pt3.x);

	return area;

}









 

你可能感兴趣的:(计算)