[R] integrate over polygon

rolf at math.unb.ca rolf at math.unb.ca
Fri Feb 16 01:11:11 CET 2007


If you can integrate over a trapezoid (with horizontal base and
vertical sides) then it's fairly easy to integrate over any (non-self
intersecting) polygon.  ``Draw'' a line which is below the polygon.
For each segment in the polygon, integrate over trapezoid whose base
is on the aforesaid line and whose top edge is the aforesaid edge of
the polygon.  If the edge in question goes from left to right,
multiply by -1.  (Leave alone if the edge goes from right to left;
vertical edges contribute 0.) Add up the results.

This amounts to applying Green's Theorem in the plane.

I don't know of any code that does this, but it shouldn't be
hard to write.  (*Given* that you can integrate over the
trapezoids.)

Another approach:

        library(spatstat)
        W <- owin(poly=X) # X is your polygon, represented as a list
                          # with components ``x'' and ``y'' giving
                          # the vertices of the polygon in *anti*
                          # clockwise order.  The first vertex should
                          # NOT be repeated at the end.
        IM <- as.im(foo,W)
        int <- summary(IM)$integral # Based on a 100 x 100 pixellation
                                    # of the bounding box of the polygon.
        oop <- spatstat.options(npixel=500)
        IM <- as.im(foo,W)
        int <- summary(IM)$integral # Based on a 500 x 500 pixellation
                                    # of the bounding box of the polygon.
        spatstat.options(oop)       # Set npixel back to the default.

HTH.

                                cheers,

                                        Rolf Turner
                                        rolf at math.unb.ca



More information about the R-help mailing list