Re: How to calculate the area of a polygon?
- To: mathgroup at smc.vnet.net
- Subject: [mg27715] Re: How to calculate the area of a polygon?
- From: "Paul Lutus" <nospam at nosite.com>
- Date: Tue, 13 Mar 2001 03:52:35 -0500 (EST)
- References: <98htbn$mod@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
"liwen liwen" <gzgear at yahoo.com> wrote in message news:98htbn$mod at smc.vnet.net... > Dear friends, > > I want to calculate the area of a polygon, for > example, the polygon is defined by following points: > data={{0,0},{5,0},{10,5},{0,10},{-5,5}}; > Regardless of that it is concave or convex. > > Any help is appreciated. The area of an irregular polygon is actually quite easy to compute: a = 1/2 * ((x1+x2)(y1-y2)+(x2+x3)(y2-y3)+...+(xn+x1)(yn-y1)) Here is sample code: data = {{1,3},{2,4},{5,4},{5,7},{7,5},{7,1},{3,1}}; seg[a_List,b_List] := (a[[1]]+b[[1]])(a[[2]]-b[[2]]) area[v_List] := Module[ {a = 0,len = Length[v],j}, For[i = 1,i <=len, (* wrap around at end of data set *) j = Mod[i,len]+1; a += seg[v[[i]],v[[j]]]; i++]; (* Always positive areas *) Return[Abs[a] 1/2]; ]; area[data] 19.5 -- Paul Lutus www.arachnoid.com