Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a polygon:

Polygon[{{0, 200 }, {200, 100}, {500, 300}, {100, 700}}]

How can I figure out its area? The docs page does not have any example.

So far I've reached this point with no success:

Needs["Polytopes`"]
Area[Polygon[{{0, 200 }, {200, 100}, {500, 300}, {100, 700}}]];

Concerning Area, the documentation states:

Area[polygon]
$\ \ \ \ \ $ gives the area of polygon polygon, when the edges of polygon have unit length.

So it is possible that my question really should be how to set unit length on edges of polygon?

share|improve this question
    
Area of polygon = 1/2 apothem * perimeter * height –  user9316 Sep 1 '13 at 13:11
add comment

3 Answers

up vote 14 down vote accepted

Basing on undocumented function introduced by J. M. in this Q&A:

pts = {{0, 200}, {200, 100}, {500, 300}, {100, 700}};

Graphics`Mesh`MeshInit[];
PolygonArea[pts]
155000.

Usage of undocumented functions is usualy useful but keep in mind that it may not work with future versions or with all cases. On the other side, it may work. One just can't be sure and trust them too much. ;)

share|improve this answer
add comment

I prefer PolygonArea myself, but for version 7 users, there's this function by Mr.Wizard for non-intersecting polygons:

polygonArea = 
 Compile[{{v, _Real, 2}}, 
   Block[{x, y},
    {x, y} = Transpose[v]; 
    Abs[x.RotateLeft[y] - RotateLeft[x].y]/2
   ]
 ]

It gives the same answer as the undocumented built-in (as it should).

share|improve this answer
1  
This algorithm is given in Mathworld. –  Sjoerd C. de Vries Aug 27 '13 at 23:05
4  
polygonArea is 3 times fast in my test. And even more with CompilationTarget->"C" –  Murta Aug 27 '13 at 23:35
    
Heh -- I wrote this ten years ago. I'm glad it's still serviceable. +1 of course. :^) –  Mr.Wizard Aug 28 '13 at 0:36
    
@Mr.Wizard Let's see... 10 years ago, I did not know a single programming language. In fact, I had not written a single line of code in any language and had not even considered using a computer for programming :D –  rm -rf Aug 28 '13 at 0:41
    
By the way, you're making me feel old. :-p –  Mr.Wizard Aug 28 '13 at 1:22
add comment

I cannot resist. Writing the code rm -rf posted was one of my first Mathematica programming exercises from over eleven years ago. (Boy, time flies.) Here's a copy of the old Notebook, which I kept, and yes, these are the actual notes I wrote myself. (I do not attest to their present accuracy, only their historical record.)


Polygon

Area of non-self-intersecting polygons, described by a set of {x,y} points, using the direct Determinant method. In this version, emphasis was placed on conciseness of code.

polyarea = Abs@Tr[Det /@ Partition[#, 2, 1, 1]]/2 &;

Again, the Determinant method, but in a semi-explicit form that is somewhat more efficient.

polyarea = Abs[Tr[#*#4 - #2*#3 & @@@ Partition[Flatten[#], 4, 2, 1]]/2] &;

These are compiled versions of the prior two functions, respectively. Of note is that the version using Det is only marginally improved with Compile, while the other becomes several times more efficient. Viewing the compiled code will show that Det is apparently not actually compiled (it is explicit). Also note the changes that were made to each function to conform to the constraints of Compile.

polyarea = 
  Compile[{{v, _Real, 2}}, 
   Abs@Tr[Det /@ Partition[Append[v, v[[1]]], 2, 1]]/2];

polyarea = 
  Compile[{{v, _Real, 2}}, 
   Abs[Tr[#[[1]]*#[[4]] - #[[2]]*#[[3]] & /@ 
       Partition[Flatten[Append[v, v[[1]]]], 4, 2]]/2]];

Updated May 26, 2002.

In this function the Determinant method is optimized using Dot-products.

polyarea = 
  Block[{x, y, R = RotateLeft}, {x, y} = Thread@#; Abs[x.R@y - R@x.y]/2] &;

This compiled version of the Dot-based function is nearly twice as efficient as the non-compiled version. Amazingly, this makes it over 30 times as fast as the direct Determinant approach.

polyarea = 
  Compile[{{v, _Real, 2}}, 
   Block[{x, y}, {x, y} = Transpose@v; 
    Abs[x.RotateLeft@y - RotateLeft@x.y]/2]];

By the way, these days I would use Module for the non-compiled version; I used to abuse Block rather badly.

share|improve this answer
    
My first Mathematica notebook should be burned and the ashes of the bytes scattered across the Pacific Ocean. –  rm -rf Aug 28 '13 at 1:01
    
@rm-rf Not proud of them, eh? ;-p Mind you this wasn't my first attempt at Mathematica programming, rather one if my earliest exercises. I don't think I kept any of my first attempts, and besides, at the beginning I thought Mathematica was just a big calculator; I didn't realize you could program with it right away. –  Mr.Wizard Aug 28 '13 at 1:05
    
@rm-rf p.s. I'm now digging through the archives trying to find the oldest thing I have that vaguely qualifies as programming. –  Mr.Wizard Aug 28 '13 at 1:07
    
@rm-rf I may have found it; my first use of anything besides an explicit formula followed by Solve or Plot: Table[Rationalize[N[Pi],10^-d],{d,1,5}] <--- look Ma, Programming!! lol –  Mr.Wizard Aug 28 '13 at 1:13
2  
Heh, I don't have my first ones, but it probably was something along the lines of For[... For[... mat[[i,j]] = RandomReal[];]] :P –  rm -rf Aug 28 '13 at 1:23
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.