Skip to content

Instantly share code, notes, and snippets.

@felixarntz
Created November 6, 2013 12:06
Show Gist options
  • Save felixarntz/7335059 to your computer and use it in GitHub Desktop.
Save felixarntz/7335059 to your computer and use it in GitHub Desktop.
// EDIT THIS
trackLayer=effect("Track Layer Pin")("Layer");
cornerPin=trackLayer.effect("Corner Pin");
// DO NOT EDIT THIS
trackPoint=value
c0=cornerPin("Upper Left").valueAtTime(0);
c1=cornerPin("Lower Left").valueAtTime(0);
c2=cornerPin("Lower Right").valueAtTime(0);
c3=cornerPin("Upper Right").valueAtTime(0);
p0=cornerPin("Upper Left");
p1=cornerPin("Lower Left");
p2=cornerPin("Lower Right");
p3=cornerPin("Upper Right");
function Track(p)
{
a = (c1[1]-c0[1])*(c2[0]-c3[0]-c1[0]+c0[0]) - (c1[0]-c0[0])*(c2[1]-c3[1]-c1[1]+c0[1]);
b = (p[0]-c0[0])*(c2[1]-c3[1]-c1[1]+c0[1]) - (c1[0]-c0[0])*(c3[1]-c0[1]) - (p[1]-c0[1])*(c2[0]-c3[0]-c1[0]+c0[0]) + (c1[1]-c0[1])*(c3[0]-c0[0]);
c = (p[0]-c0[0])*(c3[1]-c0[1]) - (p[1]-c0[1])*(c3[0]-c0[0]);
d = b*b - 4*a*c;
if (d < 0)
return p;
if (a != 0)
dx = (-b + Math.sqrt(d)) / (2*a);
else
dx = -c/b;
dy = (p[0] - c0[0] - dx * (c1[0] - c0[0])) / (c3[0] + dx * (c2[0] - c3[0]) - c0[0] - dx * (c1[0]- c0[0]));
A = p0 + dx * (p1 - p0);
B = p3 + dx * (p2- p3);
return A + dy * (B - A);
}
Track(trackPoint);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment