Here is some C# code:
using System;
using System.Windows;
class Mapper
{
public static Point MapPoint(Point a1, Point a2, Point b1, Point b2, Point p)
{
Vector u = a2 - a1; // Vector along source line a1-a2
Vector v = b2 - b1; // Vector along destination line b1-b2
Point am = a1 + 0.5*u; // Mid-point of line a1-a2
Point bm = b1 + 0.5*v; // Mid-point of line b1-b2
double factor = 1 / (u.Length * v.Length);
double cosA = (u*v) * factor;
double sinA = Vector.CrossProduct(u,v) * factor;
double x = (p - am).X * cosA - (p - am).Y * sinA + bm.X;
double y = (p - am).X * sinA + (p - am).Y * cosA + bm.Y;
return new Point(x,y);
}
static void Main(string[] args)
{
Point a1 = new Point(2,1); Point a2 = new Point(8,5); Point am = new Point(5,3);
Point b1 = new Point(1,3); Point b2 = new Point(5,9); Point bm = new Point(3,6);
Point p, q;
p = a1;
q = MapPoint(a1, a2, b1, b2, p);
Console.WriteLine("a1 maps to: " + q.ToString()); // q should = b1
p = a2;
q = MapPoint(a1, a2, b1, b2, p);
Console.WriteLine("a2 maps to: " + q.ToString()); // q should = b2
p = am;
q = MapPoint(a1, a2, b1, b2, p);
Console.WriteLine("am maps to: " + q.ToString()); // q should = bm
Console.ReadLine();
}
}
It uses the Point and Vector structures from System.Windows (in the WindowsBase assembly).
The important part is the MapPoint function. It maps an input point $p$ to an output point that is returned by the function. The mapping is defined by four points $a_1$, $a_2$, $b_1$ and $b_2$. The idea is that the "source" points $a_1$ and $a_2$ are mapped (roughly) to the destination points $b_1$ and $b_2$ respectively. This mapping will only be exact if the distances $a_1a_2$ and $b_1b_2$ are equal. In fact, what actually happens is that the mid-point of $a_1a_2$ is mapped to the mid-point of $b_1b_2$.
The test code in the Main function shows that things are working correctly. The mid-point $a_m$ gets mapped to $b_m$, as expected. Also, in this case, $a_1$ gets mapped to $b_1$ and $a_2$ gets mapped to $b_2$, since the distances distances $a_1a_2$ and $b_1b_2$ are equal.