title: Playing perfect Tetris: how to align and scale two curves using scaling and translation?
body: <p>Given parameters of scaling on the y axis (s) and translation on the x axis (t), how to scale and align two curves that do not coincide when the purpose is to maximize curve superposition (not minimize distance)?</p>
  
  <p>As indicated by @DWin, this could be rebranded "How to play Tetris perfectly with R", though it does have applications far beyond winning a Tetris game.</p>
  
  <p>A variation of this question could involve any number of rigid body transformations (rotation, translation and scaling).</p>
  
  <p>Given curve 1</p>
  
  <pre><code>curve1&lt;-data.frame(x=c(1,1,2,2,3),
                     y=c(9,6,6,3,3))
  
  with(curve1, plot(x=x, y=y, type="l", xlim=c(0,10), ylim=c(0,10)))
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/OVSaY.png" alt="enter image description here"></p>
  
  <p>and curve 2</p>
  
  <pre><code>curve2&lt;-data.frame(x=c(4,5,5,6,6,7),
                     y=c(2,2,1,1,2,3))
  
  with(curve2, plot(x=x, y=y, type="l", xlim=c(0,10), ylim=c(0,10)))
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/XGZV1.png" alt="curve 2"></p>
  
  <p>I wish to find s and t that maximize superposition between the two curves.</p>
  
  <p>Ideally the method would be in R using optim.</p>
  
  <p>In this made up example, t=3 and s=1/3, so that</p>
  
  <pre><code>t=3
  s=1/3
  
  with(curve2, plot(x=x, y=y, type="l", xlim=c(0,10), ylim=c(0,10)))
  
  with(curve1, lines(x=x+t, y=y*s, col="red"))
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/sTecs.png" alt="enter image description here"></p>
  
  <p>Note that to obtain such a fitting, regions that can have a consensus must have a higher weight on parametrization than regions that can not be superimposed and that the larger the consensus region the higher the weight.</p>
  
  <p>Trails I have been exploring:
  <li><a href="https://stackoverflow.com/questions/6723157/how-to-compare-two-curves-arrays-of-points">minimize area between curves</a><br>
  <li><a href="http://www.lems.brown.edu/vision/publications/Kimia%27s_Publication/Journals/aligning_curves-PAMI.pdf" rel="nofollow noreferrer">algorithm for superposing two curves</a><br>
  <li>shape analysis using the shapes package<br>
  <li><a href="http://en.wikipedia.org/wiki/Iterative_closest_point" rel="nofollow noreferrer">Iterative closest point (ICP)</a> if someone has implemented it in R or can port one of the C implementations</p>
  
  <p>Bonus points for a method using maximum likelihood (assuming normal distribution of error).</p>