'''oneLinePair.py Compute the mapping defined by one pair of lines, according to Feature-Based Metamorphosis, a la Beier and Neely. ''' import math from pointLineDist import * def perp(dx, dy): '''Return a vector the same length as, and perpendicular to the input.''' return (dy, -dx) def findSourceCoords(X, P, Q, Pp, Qp): '''Given input point X=(xd,yd) find the source image point Xp=(xs,ys) using the line segments (P,Q) and (Pp, Qp) to define the mapping.''' (xd,yd) = X # unpack points... (x0,y0) = P (x1,y1) = Q # Compute u and v (u,v) = pointLineDist(xd,yd,x0,y0,x1,y1) (x0p,y0p) = Pp (x1p,y1p) = Qp # denominator in B&N eq 3: newhypot = math.sqrt((x1p-x0p)^2+(y1p-y0p)^2) # numerator in B&N eq 3: (vx,vy)=perp(x1p-x0p, y1p-y0p) # overall of eq 3: xs = x0p + u*(x1p-x0p) + v * vx / newhypot ys = y0p + u*(y1p-y0p) + v * vy / newhypot return (xs,ys) def test(): P = (0,0); Q=(1,0) Pp = (5,5); Qp=(6,5) X = (13,18) print findSourceCoords(X, P, Q, Pp, Qp) test()