I am using the R package pracma to compute the area and centroid of polygons with between 3 and 5 vertices. I know the manual explains cases when the area is negative, I don't quite understand this. Furthermore, the centroids in these cases are not what I am hoping to get.
The problem I am running into is that my program may generate polygons with vertices correctly ordered vertices, but in a manner which gives negative areas:
library(pracma)
xs<-c(0,1,1)
ys<-c(0,1,0)
poly_center(xs,ys)
polyarea(xs,ys)
xs<-c(1,1,0)
ys<-c(0,1,0)
poly_center(xs,ys)
polyarea(xs,ys)
xs<-c(1,0,1)
ys<-c(0,0,1)
poly_center(xs,ys)
polyarea(xs,ys)
Would someone be able to suggest a generalizable method (ie any number of vertices) for sorting the vertices so that I always get a positive area and expected centroid?