title: Plot intersection of two filled polygons in R
body: <p>Let <code>x1</code> and <code>y1</code> be vectors of equal length, defining the coordinates of vertices of polygon1.<br>
  Let <code>x2</code> and <code>y2</code> be vectors of equal length, defining the coordinates of vertices of polygon2.</p>
  
  <p>Polygon1, for example, can be drawn by <code>polygon(x1,y1,border=NA,col=rgb(0,0,0))</code></p>
  
  <p>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?</p>
  
  <p><strong>Background:</strong></p>
  
  <p>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.</p>
  
  <p><strong>Some constraints on the polygons in my case:</strong></p>
  
  <p>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.</p>
  
  <p><strong>Simplistic example data:</strong></p>
  
  <pre><code>x1 &lt;- 0:6
  y1 &lt;- c(0,1,2,1,0,-1,0)
  x2 &lt;- c(x1,rev(x1))
  y2 &lt;- c(x1*rev(x1)/5,x1*rev(x1)/10)
  </code></pre>