4
$\begingroup$

I have two lists:

list1 = {{0., 7.}, {-0.0701898, 7.01875}, {-0.140755, 7.03679}, {-0.211687, 
  7.05412}, {-0.28298, 7.07073}, {-0.354627, 7.08663}, {-0.426619, 
  7.10179}, {-0.498951, 7.11622}, {-0.571613, 7.12991}, {-0.644599, 
  7.14286}, {-0.717901, 7.15506}, {-0.791511, 7.16651}, {-0.865421, 
  7.1772}, {-0.939625, 7.18712}, {-1.01411, 7.19628}, {-1.08888, 
  7.20466}, {-1.16391, 7.21227}, {-1.23921, 7.21909}, {-1.31475, 
  7.22513}, {-1.39054, 7.23037}, {-1.46657, 7.23482}, {-1.54283, 
  7.23848}, {-1.6193, 7.24132}, {-1.69599, 7.24336}, {-1.77287, 
  7.24459}, {-1.84995, 7.24501}, {-1.92722, 7.2446}, {-2.00466, 
  7.24338}, {-2.08227, 7.24132}, {-2.16004, 7.23844}, {-2.23796, 
  7.23472}, {-2.31602, 7.23017}, {-2.39421, 7.22478}, {-2.47253, 
  7.21854}, {-2.55096, 7.21146}, {-2.62949, 7.20353}, {-2.70813, 
  7.19475}, {-2.78684, 7.18512}, {-2.86564, 7.17463}, {-2.9445, 
  7.16328}, {-3.02342, 7.15107}, {-3.10239, 7.13799}, {-3.1814, 
  7.12405}, {-3.26045, 7.10924}, {-3.33951, 7.09356}, {-3.41858, 
  7.07701}, {-3.49766, 7.05958}, {-3.57673, 7.04128}, {-3.65578, 
  7.0221}, {-3.73481, 7.00204}, {-3.8138, 6.98111}, {-3.89274, 
  6.95929}, {-3.97163, 6.93659}, {-4.05045, 6.913}, {-4.12919, 
  6.88854}, {-4.20785, 6.86318}, {-4.28642, 6.83694}, {-4.36488, 
  6.80982}, {-4.44322, 6.7818}, {-4.52144, 6.7529}, {-4.59953, 
  6.72311}, {-4.67747, 6.69244}, {-4.75526, 6.66087}, {-4.83288, 
  6.62842}, {-4.91033, 6.59508}, {-4.98759, 6.56085}, {-5.06466, 
  6.52574}, {-5.14152, 6.48973}, {-5.21817, 6.45285}, {-5.29459, 
  6.41507}, {-5.37078, 6.37641}, {-5.44672, 6.33687}, {-5.52241, 
  6.29645}, {-5.59784, 6.25514}, {-5.67298, 6.21295}, {-5.74785, 
  6.16989}, {-5.82241, 6.12595}, {-5.89668, 6.08113}, {-5.97062, 
  6.03544}, {-6.04425, 5.98887}, {-6.11753, 5.94144}, {-6.19047, 
  5.89313}, {-6.26306, 5.84396}, {-6.33528, 5.79393}, {-6.40712, 
  5.74304}, {-6.47858, 5.69129}, {-6.54964, 5.63868}, {-6.62029, 
  5.58522}, {-6.69053, 5.5309}, {-6.76035, 5.47574}, {-6.82973, 
  5.41974}, {-6.89866, 5.36289}, {-6.96714, 5.30521}, {-7.03515, 
  5.24669}, {-7.10269, 5.18734}, {-7.16974, 5.12717}, {-7.2363, 
  5.06617}, {-7.30235, 5.00435}, {-7.36789, 4.94172}, {-7.43291, 
  4.87827}, {-7.49739, 4.81402}, {-7.56133, 4.74896}, {-7.62471, 
  4.68311}, {-7.68753, 4.61646}, {-7.74978, 4.54902}, {-7.81145, 
  4.4808}, {-7.87253, 4.4118}, {-7.93301, 4.34203}, {-7.99287, 
  4.27148}, {-8.05212, 4.20017}, {-8.11074, 4.12811}, {-8.16872, 
  4.05529}, {-8.22606, 3.98172}, {-8.28273, 3.90741}, {-8.33875, 
  3.83236}, {-8.39409, 3.75659}, {-8.44874, 3.68009}, {-8.5027, 
  3.60287}, {-8.55597, 3.52494}, {-8.60852, 3.4463}, {-8.66035, 
  3.36697}, {-8.71145, 3.28694}, {-8.76182, 3.20622}, {-8.81145, 
  3.12483}, {-8.86032, 3.04276}, {-8.90843, 2.96003}, {-8.95576, 
  2.87664}, {-9.00232, 2.7926}, {-9.0481, 2.70792}, {-9.09307, 
  2.62259}, {-9.13725, 2.53664}, {-9.18061, 2.45007}, {-9.22315, 
  2.36288}, {-9.26487, 2.27509}, {-9.30574, 2.1867}, {-9.34578, 
  2.09771}, {-9.38497, 2.00815}, {-9.42329, 1.91801}, {-9.46075, 
  1.8273}, {-9.49734, 1.73604}, {-9.53305, 1.64423}, {-9.56786, 
  1.55188}, {-9.60179, 1.45899}, {-9.63481, 1.36558}, {-9.66691, 
  1.27166}, {-9.69811, 1.17723}, {-9.72838, 1.0823}, {-9.75771, 
  0.986886}, {-9.78611, 0.890993}, {-9.81357, 0.79463}, {-9.84008, 
  0.697808}, {-9.86563, 0.600534}, {-9.89021, 0.502819}, {-9.91383, 
  0.404672}, {-9.93647, 0.306104}, {-9.95813, 0.207122}, {-9.9788, 
  0.107739}, {-9.99848, 0.00796206}}

and:

list2 = {{0., 4.}, {-0.0403813, 4.038}, {-0.0815225, 4.07558}, {-0.123419, 
  4.11274}, {-0.166067, 4.14947}, {-0.209462, 4.18575}, {-0.253599, 
  4.22157}, {-0.298473, 4.25693}, {-0.344079, 4.29181}, {-0.390412, 
  4.32619}, {-0.437467, 4.36008}, {-0.485239, 4.39345}, {-0.533721, 
  4.4263}, {-0.582908, 4.45862}, {-0.632795, 4.49039}, {-0.683374, 
  4.52161}, {-0.734641, 4.55226}, {-0.786588, 4.58233}, {-0.83921, 
  4.61182}, {-0.892499, 4.6407}, {-0.94645, 4.66898}, {-1.00105, 
  4.69664}, {-1.05631, 4.72367}, {-1.1122, 4.75007}, {-1.16872, 
  4.77581}, {-1.22587, 4.80089}, {-1.28364, 4.82531}, {-1.34201, 
  4.84904}, {-1.40099, 4.87209}, {-1.46056, 4.89444}, {-1.52072, 
  4.91608}, {-1.58146, 4.937}, {-1.64276, 4.9572}, {-1.70463, 
  4.97666}, {-1.76705, 4.99538}, {-1.83001, 5.01334}, {-1.89351, 
  5.03054}, {-1.95753, 5.04696}, {-2.02207, 5.06261}, {-2.08712, 
  5.07746}, {-2.15266, 5.09152}, {-2.21869, 5.10477}, {-2.2852, 
  5.11721}, {-2.35218, 5.12882}, {-2.41962, 5.1396}, {-2.48751, 
  5.14954}, {-2.55584, 5.15864}, {-2.6246, 5.16688}, {-2.69377, 
  5.17425}, {-2.76336, 5.18076}, {-2.83334, 5.18639}, {-2.90371, 
  5.19113}, {-2.97445, 5.19499}, {-3.04556, 5.19794}, {-3.11703, 
  5.19999}, {-3.18883, 5.20112}, {-3.26097, 5.20134}, {-3.33344, 
  5.20062}, {-3.40621, 5.19898}, {-3.47928, 5.1964}, {-3.55264, 
  5.19287}, {-3.62627, 5.1884}, {-3.70016, 5.18296}, {-3.77431, 
  5.17657}, {-3.8487, 5.1692}, {-3.92331, 5.16087}, {-3.99814, 
  5.15155}, {-4.07318, 5.14125}, {-4.1484, 5.12996}, {-4.22381, 
  5.11768}, {-4.29938, 5.10441}, {-4.37511, 5.09013}, {-4.45098, 
  5.07484}, {-4.52697, 5.05854}, {-4.60309, 5.04122}, {-4.67931, 
  5.02289}, {-4.75562, 5.00353}, {-4.832, 4.98315}, {-4.90845, 
  4.96174}, {-4.98496, 4.93929}, {-5.0615, 4.91581}, {-5.13807, 
  4.89128}, {-5.21466, 4.86572}, {-5.29124, 4.83911}, {-5.36781, 
  4.81145}, {-5.44435, 4.78274}, {-5.52085, 4.75298}, {-5.5973, 
  4.72216}, {-5.67368, 4.69029}, {-5.74998, 4.65737}, {-5.82619, 
  4.62338}, {-5.90229, 4.58833}, {-5.97826, 4.55222}, {-6.05411, 
  4.51504}, {-6.1298, 4.47681}, {-6.20533, 4.4375}, {-6.28068, 
  4.39714}, {-6.35585, 4.3557}, {-6.43081, 4.31321}, {-6.50555, 
  4.26964}, {-6.58007, 4.22501}, {-6.65433, 4.17932}, {-6.72834, 
  4.13256}, {-6.80208, 4.08473}, {-6.87552, 4.03584}, {-6.94867, 
  3.98589}, {-7.0215, 3.93488}, {-7.09401, 3.88281}, {-7.16617, 
  3.82968}, {-7.23797, 3.7755}, {-7.30941, 3.72025}, {-7.38046, 
  3.66396}, {-7.45111, 3.60662}, {-7.52135, 3.54822}, {-7.59116, 
  3.48878}, {-7.66053, 3.4283}, {-7.72945, 3.36678}, {-7.7979, 
  3.30422}, {-7.86587, 3.24063}, {-7.93334, 3.17601}, {-8.00031, 
  3.11036}, {-8.06675, 3.04368}, {-8.13265, 2.97599}, {-8.19801, 
  2.90728}, {-8.2628, 2.83757}, {-8.32701, 2.76684}, {-8.39063, 
  2.69512}, {-8.45364, 2.62239}, {-8.51603, 2.54868}, {-8.57779, 
  2.47398}, {-8.63891, 2.3983}, {-8.69937, 2.32164}, {-8.75915, 
  2.24401}, {-8.81825, 2.16542}, {-8.87664, 2.08586}, {-8.93433, 
  2.00536}, {-8.99129, 1.92391}, {-9.04751, 1.84152}, {-9.10297, 
  1.7582}, {-9.15767, 1.67395}, {-9.2116, 1.58879}, {-9.26473, 
  1.50271}, {-9.31705, 1.41572}, {-9.36857, 1.32785}, {-9.41925, 
  1.23908}, {-9.46908, 1.14943}, {-9.51807, 1.0589}, {-9.56618, 
  0.967515}, {-9.61342, 0.87527}, {-9.65977, 0.782176}, {-9.70521, 
  0.688243}, {-9.74973, 0.593479}, {-9.79332, 0.497893}, {-9.83598, 
  0.401495}, {-9.87768, 0.304292}, {-9.91842, 0.206296}, {-9.95818, 
  0.107516}, {-9.99696, 0.00796084}}

Now I can plot them on a graph:

ListPlot[{list1,list2},Joined -> True]

enter image description here

Now the question is:

I would like to move the yellow line in such a way that it resembles as much as possible the blue line. I don't want to distort the line, just move it in x-y direction or rotate it.

In the end I would like to have a program that automatically figures out the best x and y mouvement and best rotation about a point (the program should also find that point and the angle) such that the the yellow line fits as good as possible above the blue line.

How can I do that ?

EDIT:

Here are some starting hints based on comments:

{err,transfo} = 
 FindGeometricTransform[list1, list2, 
  TransformationClass -> "Rigid"];

Then to visualize:

ListPlot[{list1,list2,transfo[list2],Joined->True]

enter image description here

Now... How do I get the displacment info in x and in y and the rotation info that was needed to do the geometric

$\endgroup$
  • 2
    $\begingroup$ Try FindGeometricTransform[] with the setting TransformationClass -> "Rigid". $\endgroup$ – J. M. is away Apr 23 '17 at 12:27
  • $\begingroup$ @J.M. Thanks a lot, I will have a look at this fucntion. $\endgroup$ – henry Apr 23 '17 at 12:40
  • $\begingroup$ @J.M. Okay,so I tried it: FindGeometricTransform[list1, list2, TransformationClass -> "Rigid"] ... It seems to find a homogenous transformation matrix, but how do I visualize the result ? $\endgroup$ – henry Apr 23 '17 at 12:43
  • $\begingroup$ With Line and GeometricTransformation. $\endgroup$ – Szabolcs Apr 23 '17 at 13:00
  • $\begingroup$ @Szabolcs could you show me how to do it ? $\endgroup$ – henry Apr 23 '17 at 13:12
4
$\begingroup$

Like Szabolcs and J.M. showed, and which you showed in your updated question, FindGeometricTransform can solve this problem. You then asked how to get the displacement information and rotation information. This information can be extracted using TransformationMatrix:

{err, tf} = FindGeometricTransform[list1, list2, TransformationClass -> "Rigid"];
(tm = TransformationMatrix[tf]) // MatrixForm

Mathematica graphics

The 2x2 submatrix in the top left corner is the rotation transform. The two top elements in the rightmost column correspond to displacement.

rotation = tm[[1 ;; 2, 1 ;; 2]];
displacement = tm[[1 ;; 2, -1]];
list3 = displacement + rotation.# & /@ list2;
ListPlot[{list1, list2, list3}]

Mathematica graphics

One problem with the FindGeometricTransform is that it isn't clear what it is we're minimizing. Explicitly minimizing a cost function, in this case Norm[x]^2, is perhaps preferable in this respect:

{val, sol} = NMinimize[
  Norm[{x, y} + RotationMatrix[th].# & /@ list2 - list1]^2,
  {x, y, th}
  ]

{27.0478, {x -> 0.253746, y -> 2.49059, th -> 0.263939}}

tf2 = Function[{pt}, ({x, y} + RotationMatrix[th].pt /. sol)];
ListPlot[{list1, list2, list3, tf2 /@ list2}]

Mathematica graphics

The result is similar to that of FindGeometricTransform and as a bonus we get the parameters directly.

$\endgroup$
  • $\begingroup$ Wow. Very nice ! Thanks a lot. Could you explain me what exactly: ".# & /@ list2 - list1" does ? $\endgroup$ – henry Apr 23 '17 at 18:16
  • $\begingroup$ {x, y} + RotationMatrix[th].# & /@ list2 is the displacement plus the rotated coordinate, for each coordinate in list2. To understand the subtraction just try it with two small lists like {1,2,3} - {4,5,6} and see what happens. $\endgroup$ – C. E. Apr 23 '17 at 18:21
  • $\begingroup$ Ah, I see baiscally you are calculating the difference between the displaced and rotated points of list2, and the fixed points of list1 -> and indeed this is what you want to minimize. Is that correct ? $\endgroup$ – henry Apr 23 '17 at 18:34
  • $\begingroup$ @DoHe I am minimizing the squared norm of that vector, yes. This is what I refer to as a cost function. It's not the only choice, but it seems to work. This particular choice is sometimes called a "least squares" optimization. $\endgroup$ – C. E. Apr 23 '17 at 18:39
  • 1
    $\begingroup$ @DoHe You need to know which points in the first list correspond to which points in the second list, and then create two lists with the same number of points in each. Otherwise it gets tricky. (As in, out of scope for this question.) $\endgroup$ – C. E. Apr 23 '17 at 19:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.