Skip to content

Instantly share code, notes, and snippets.

@nakosung
Created September 11, 2013 01:55
Show Gist options
  • Save nakosung/6518458 to your computer and use it in GitHub Desktop.
Save nakosung/6518458 to your computer and use it in GitHub Desktop.
Pinch transformation
http://www.wolframalpha.com/input/?i=inv%28+%7B+%7B+a%2C+-b%2C+1%2C+0+%7D%2C+%7Bb%2C+a%2C+0%2C+1%7D%2C+%7B+c%2C+-d%2C+1%2C+0+%7D%2C+%7B+d%2C+c%2C+0%2C+1+%7D+%7D+%29+*+%7B+%7Bx%7D%2C+%7By%7D%2C+%7Bz%7D%2C+%7Bw%7D+%7D
calc = (p1, p2, q1, q2) ->
low = (a,b,c,d,x,y,z,w) ->
scale = 1/(a^2-2 a c+b^2-2 b d+c^2+d^2)
sct = -b w+d w+a x-c x+b y-d y-a z+c z
sst = -a w+c w-b x+d x+a y-c y+b z-d z
tx = z a^2-d w a-c x a+d y a-c z a+b c w+c^2 x+d^2 x-b d x-b c y+b^2 z-b d z
ty = w a^2-c w a-d x a-c y a+d z a+b^2 w-b d w+b c x+c^2 y+d^2 y-b d y-b c z)
[scale * sct, scale * sst, scale * tx, scale * ty]
[sct,sst,tx,ty] = low p1.x, p1.y, p2.x, p2.y, q1.x, q2.y
scale = Math.sqrt(sct * sct + sst * sst)
ct = sct / scale
st = sst / scale
theta = Math.atan2( st, ct ) * 180 / Math.PI
scaling:scale, rotation:theta, translation:{x:tx, y:ty}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment