Let x1
and y1
be vectors of equal length, defining the coordinates of vertices of polygon1.
Let x2
and y2
be vectors of equal length, defining the coordinates of vertices of polygon2.
Polygon1, for example, can be drawn by polygon(x1,y1,border=NA,col=rgb(0,0,0))
What is the easiest way (preferably in base R i.e. without any packages) to fill only the area that belongs to both polygon1 and polygon2? In other words, what is the easiest way to draw (filled) polygon1 clipped by polygon 2?
Background:
I'm using this to shade a contoured (multi-colored) area under a standard plot. Each inter-contour region needs to be intersected with the area under the plot.
Some constraints on the polygons in my case:
In case it matters, polygon1 is in my case definable as the region between the x-axis and some y=f1(x), while polygon 2 is definable as the region enclosed between y=a*f2(x) and y=b*f2(x) where a>b.
Simplistic example data:
x1 <- 0:6
y1 <- c(0,1,2,1,0,-1,0)
x2 <- c(x1,rev(x1))
y2 <- c(x1*rev(x1)/5,x1*rev(x1)/10)