Skip to content
Prev 61278 / 398498 Next

Can I calculate the area of a polygon?

On 19-Dec-04 Andreas Kretschmer,,, wrote:
Welcome!
Not sure what you mean by "the installed plr - Language", but for
calculating areas og polygons you may find the following function
useful.
Example based on the coordinates ((0,0),(0,2),(2,2),(3,3),(5,0))
you give above.

Let X be the matrix of the coordinates:

  > X
       [,1] [,2]
  [1,]    0    0
  [2,]    0    2
  [3,]    2    2
  [4,]    3    3
  [5,]    5    0


Function "area":

  area<-function(X){
    X<-rbind(X,X[1,])
    x<-X[,1]; y<-X[,2]; lx<-length(x)
    -sum((x[2:lx]-x[1:lx-1])*(y[2:lx]+y[1:lx-1]))/2
  }

  > area(X)
  [1] -9.5

Note that this function generates a "signed area": positive
if the bounding contour is "counter-clockwise" in the order
of the vertices as given in X, negative if clockwise.

The principle of the function is that it first closes the
polygon by repeating the first point, and then (in effect)
calculates the contour integral

  Int -y*dx

round the contour of the polygon (equivalent to Int x*dy).

Hoping this helps,
Ted.


--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 094 0861  [NB: New number!]
Date: 19-Dec-04                                       Time: 13:40:32
------------------------------ XFMail ------------------------------