Re: How to calculate the area of a polygon?
- To: mathgroup at smc.vnet.net
- Subject: [mg27723] Re: [mg27712] How to calculate the area of a polygon?
- From: Andrzej Kozlowski <andrzej at platon.c.u-tokyo.ac.jp>
- Date: Tue, 13 Mar 2001 03:52:42 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
Here is one way. First observe that it is easy to compute the area of a triangle. Indeed, the follwoing formula is well known: AreaOfTriangle[{p1_List, p2_List, p3_List}] := Abs[Det[{Append[p1, 1], Append[p2, 1], Append[p3, 1]}]/2] The next step is to download from MathSource the Martin Kraus's triangulation package: <http://library.wolfram.com/packages/polygontriangulation/> After installing and loading the package you can proceed as follows. I shall consider an easy case so that we can verify the answer by hand. Thus, let's take: In[4]:= data={{-1,0},{0,1},{1,0},{0,-1}}; Obviously the area of this square is 2. We triangulatate the square using Martin Kraus's package: << PolygonTriangulation`SimplePolygonTriangulation` I am assuming of course that your polygon is simple, in other words, it has no self-intersections. In[6]:= triang=SimplePolygonTriangulation[data] Out[6]= {{2,3,1},{4,1,3}} We convert this to a list of vertices of triangles: In[7]:= triangPoints=Map[Part[data,#]&,triang] Out[7]= {{{0,1},{1,0},{-1,0}},{{0,-1},{-1,0},{1,0}}} We compute the areas of the triangles: In[34]:= Map[AreaOfTriangle,triangPoints] Out[34]= {1,1} Finally, we add them up: In[35]:= Plus@@% Out[35]= 2 You should be able to convert it all into a single function to compute the area of an arbitrary simple polygon. There is another way which is however slow and gives only an approaximate answer. Not so long ago someone asked on this list for a function which determines whether a point is inside or outside a given polygon. A few candidates were offered. You should choose the most efficient one yourself. Then define a function f[p_,data_] which given apoint p and a polygon determined by data takes the value 1 if the point is inside the polygon and 0 if it is outside. We can then simple integrate this function, using NIntegrate, over the area of the smalles rectangle with sides parallel to the x and y axes containing the given polygon. The function would look like this: AreaOfPolygon[data_] := Module[{xmin = Min[Transpose[data][[1]]], xmax = Max[Transpose[data][[1]]], ymin = Min[Transpose[data][[2]]], ymax = Max[Transpose[data][[2]]]}, NIntegrate[ Evaluate[f[{x, y}, data]], {x, xmin, xmax}, {y, ymin, ymax}]] where f is the function above. You will have to choose f yourself. Whatever you choose this approach will surely be much slower and less accurate than the one based on the triangulation package. -- Andrzej Kozlowski Toyama International University JAPAN http://platon.c.u-tokyo.ac.jp/andrzej/ on 3/12/01 8:09 AM, liwen liwen at gzgear at yahoo.com wrote: > 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. > > Thank you very much! > > Liwen 3/11/2001 > > E-mail: gzgear at yahoo.com > > __________________________________________________ > Do You Yahoo!? > Yahoo! Auctions - Buy the things you want at great prices. > http://auctions.yahoo.com/ > >