Cartesian Space - linear transformation

I am working on a problem that seems to have a solution in linear algebra. I will try to explain it, and will post clarification if needed. I was wondering if someone could help me with this -

Suppose we have two sets of points (P1 and P2) in separate N-dimensional Cartesian Spaces S1 and S2 (so we have two sets of n-tuples in real n-dimensional space)

We need to find linear transformation from S1 to S2.

My theory is

In order to find the transformation matrix it is sufficient to measure the distances between points D1...Dj that belong to P1 and K1..Kj that belong to P2, such that D1..Dj do not belong to the same N-1 - dimensional plane in S1 and K1..Kj do not belong to the same N-1 dimensional plane in S2.

This is linear algebra, and, while this theory is intuitive, I need a reference or a way to build transformation matrix.

===

As an example in 2D space -

suppose we have points A1,A2, A3 that do not lay on the same line (meaning they form a real triangle)

Suppose also we have points B1, B2, B3 and B4 where B1, B2 and B3 do not lay on the same line. The X and Y axis of the B points are not necessarily the same as X and Y axis of the A points, and may not be parallel or orthogonal.

My theory - by knowing the absolute distances between A1..A3 and B1..B3 (all pairs of dots - A1 to B1..B3, A2 to B1..B3 etc) (we will know them from some black-box function that will simply return the absolute distance) we can design a transformation matrix, so coordinates of B4 can be recalculated without measuring the distance between B4 and any other points.

I would appreciate any thoughts on this.