Lambo
Lambo

Reputation: 897

Something in areapl() function that I can't understand in R

I'm trying to learn how to calculate an area using some of the functions in R packages. areapl() is the first one that I found in splancs package. There is one example at the end of the help file:

x <- c(1,0,0,1,1,1,1,3,3,1)
y <- c(0,0,1,1,0,0,-1,-1,0,0)
m <- cbind(x, y)
plot(m, type="b")
areapl(m)
areapl(m[1:5,])
areapl(m[6:10,])

Obviously the answers of areapl(m[1:5,]) and areapl(m[6:10,]) are what we expected however when I try this areapl(m) it gives me 1 as the answer. What I expected was the sum of areapl(m[1:5,]) and areapl(m[6:10,]) which should be 3 rather than the difference of the two areas.

Can anyone help me understand how this function works?

Much appreciate for the help.

Upvotes: 1

Views: 268

Answers (1)

nicola
nicola

Reputation: 24480

You must preserve the order (clockwise or counter-clockwise) of the points you are giving. Try:

   x[6:10]<-x[10:6]
   y[6:10]<-y[10:6]
   m <- cbind(x, y)
   areapl(m)

I don't have splancs installed and didn't test it. However, I tested it with other polygon libraries, so I'm pretty sure that that is the issue. You provided the points of the first polygon clockwise and the points of the second one counter-clockwise. Try yo be coherent and you should be fine.

Upvotes: 3

Related Questions