I'm trying to plot the Minkowski sum of a square and a rhombus.
# a unit square
sq <- square(1)
# a rhombus
line1 <- psp(0,1,1,0,window=owin())
line2 <- psp(0,0,1,1,window=owin())
rhombus <- line1 %(+)% line2
# the Minkowski sum of the rhombus and the square
ply <- rhombus %(+)% sq
The MinkowskiSum() function (or the %(+)% operator) returns an object with attribute $bdry that contains 2 different sets of boundary points.
ply$bdry # it has two sets of boundary points
After plotting it out, it seems like the first element in ply$bdry is the correct set of boundary points for ply.
par(mfrow=c(1,2))
plot(ply, axes=TRUE, border="blue", lwd=2, hatch=TRUE, hatchargs=list(col="blue"))
plot(rhombus, add=TRUE, axes=TRUE, lwd=2, hatch=TRUE, hatchargs=list(texture=5))
plot(sq, add=TRUE, border="red", lwd=2)
plot(ply, axes=TRUE, border="white")
polygon(ply$bdry[[1]]$x, y = ply$bdry[[1]]$y, border="lightpink", col="lavenderblush") # this is the correct set of boundary points of the Minkowski sum
polygon(ply$bdry[[2]]$x, y = ply$bdry[[2]]$y, border="blue", col="lavender") # this is not the correct set of boundary points of the Minkowski sum
I've read through the manual and the source code but still couldn't figure out:
- Why does MinkowskiSum() return multiple sets of boundaries and what does each set of boundary points correspond to?
- How do I know which set of boundary points is the right one to use for plotting the resulting Minkowski sum?
Any help is appreciated!
You don't need to inspect the internal structure of the resulting object to decide how to plot it. Just use
plot(which will be dispatched toplot.owin).The result
plyis a spatial window (object of class"owin"). Internally this is represented as a list with many components. One of the components is$bdrywhich gives the coordinates of the vertices of the polygon or polygons that represent the object. A spatial window may consist of multiple separate polygons, or polygons with holes; in these cases,$bdryis a list with one entry for each connected polygon representing a piece of the outer boundary or the boundary of a hole.In your example, the result of the operation consists of two polygons. The first one is the one you would expect. The second one is a very tiny hole. You can see this using
print.owinorsummary.owin:The tiny hole is the result of numerical rounding error. Polygon calculations in
spatstatare performed mainly using thepolyclippackage which works in integer arithmetic. Input polygons in continuous space are discretised to integer coordinates with a default resolution of1e-09units, using the midpoint of the surrounding rectangle as the origin. Thepolyclipcalculation results in a hole of size about1e-09by1e-09. Use the...arguments toMinkowskiSumto change the discretisation.