Posted 11 January 2012 - 08:01 AM
I believe you can solve this problem using bilinear interpolation. To start with you have the formula,
x(s,t) = a_1 * s * t + a_2 * s * (1 - t) + a_3 * (1 - s) * t + a_4 * (1 - s) * (1 - t)
where 0 <= s <= 1, 0 <= t <= 1, and the a_i are 2D coefficients that we need to figure out. I'm going to define the boundary conditions you've provided as
A = (171,22)
B = (488,155)
C = (24,126)
D = (361, 346)
So now we use the boundary conditions to get
x(0,0) = a_4 = A
x(1,0) = a_2 = B
x(0,1) = a_3 = C
x(1,1) = a_1 = D
Or,
x(s,t) = D * s * t + B * s * (1 - t) + C * (1 - s) * t + A * (1 - s) * (1 - t)
This gives us a mapping from [0,1]x[0,1] to the co-ordinates specified by A, B, C, and D, but we actually need to the reverse so we need to invert this equation. First, the equation is actually made up of two equations: one for x component and one for y. We can write this as
P.x = D.x * s * t + B.x * s * (1 - t) + C.x * (1 - s) * t + A.x * (1 - s) * (1 - t)
P.y = D.y * s * t + B.y * s * (1 - t) + C.y * (1 - s) * t + A.y * (1 - s) * (1 - t)
where P is the point that we are trying to map to the [0,1]x[0,1] domain. To do this we isolate the 's' and 't' variables,
P.x = s * (D.x * t + B.x * (1 - t) - C.x - A.x * (1 - t)) + (C.x + A.x * (1 - t))
s = P.x - (C.x + A.x * (1 - t)) / (D.x * t + B.x * (1 - t) - C.x - A.x * (1 - t))
Similarly, for the y equation
s = P.y - (C.y + A.y * (1 - t)) / (D.y * t + B.y * (1 - t) - C.y - A.y * (1 - t))
Equating the equations for 's' we get.
(D.y * t + B.y * (1 - t) - C.y - A.y * (1 - t)) * (D.x * t + B.x * (1 - t) - C.x - A.x * (1 - t)) = (P.x - (C.x + A.x * (1 - t))) * (P.y - (C.y + A.y * (1 - t)))
As messy as this all looks it is just a quadratic equation. Solve for 't' and then plug it back into one of the equations for 's'.
-Josh