title: R: Find the values of a 2D function (given by a matrix) along an arbitrary line
body: <p>I have a matrix describing a 2D surface and I need to be able to calculate values along the surface for an arbitrary line.</p>
  
  <p>This is best explained by an example</p>
  
  <pre><code>#x and y axes
  x=c(1:100)
  y=c(1:100)
  # 2D Matrix function defined as 0 except for a middle box filled with 1
  M=matrix(0,nrow=100,ncol=100)
  M[40:60,40:60]=1
  # define two points
  x1=50
  y1=50
  x2=23
  y2=80
  # plot contour graph of M, add points (x1,y1) and (x2,y2)
  # and a line connecting the two
  contour(x,y,M)
  points(x1,y1,col=2)
  points(x2,y2,col=3)
  lines(c(x1,x2),c(y1,y2),lty=2)
  </code></pre>
  
  <p>What I want to do is to get values of M along the line from (x1,y1) to (x2,y2), for instance at the values (xvec,yvec) where xvec=seq(x1,x2,length.out=N) and likewise for yvec.</p>
  
  <p>Is there a simple way to do this in R?</p>
  
  <p>Sincerely</p>