FORUMS: list search recent posts

# Scaling and Rotating a Shape between two points

FAQ   •   VIEW ALL
 Scaling and Rotating a Shape between two points on Mar 10, 2015 at 2:44:51 am

I'm attempting to reproduce within AfterEffects a common feature of AutoCAD drawings, in which two points (typically the two lower corners of a bounding box defining an implied edge) each stay attached to another respective object (line segment) on either side and -- driven by movements of those attached corners -- the object automatically scales up or down AND rotates to match the angle of the two points. It's similar to the now-common touch-screen gesture of scaling an image by touching the screen with two fingers and spreading the fingers apart...with the added challenge of accommodating rotation at the same time.

Basic idea / what I've tried:

1) Move anchor point for the "joint object" (the object suspended between two defined points) to its lower left corner at default rotation (zero degrees)...this becomes the "theta" of the relevant trig functions;

2) Use Expressions (and basic trig) to set the rotation of the joint object to the angle between Point A and Point B;

3) Use Expressions to calculate the Scale of the joint object (distance formula of line from A to B/original base width of joint object) * 100 (to return a percentage value);

I've managed to get something which works partially (say, within -45 degrees to 45 degrees), but my Rotation Expression for the joint object falls off into all sorts of craziness outside that range.

I also haven't come up with a fix for the divide-by-zero errors. I've tried temporarily adding in tiny numbers (.000000001) to avoid zero, but I feel mathematically guilty about it as it's a kind of cheat and I'd rather work out a proper solution.

Any recommendations -- including fresh/lateral thinking approaches to achieving the goal through some other means, would be greatly appreciated.

 Re: Scaling and Rotating a Shape between two pointson Mar 10, 2015 at 4:54:21 am

It seems to work if you start with a square solid (just leave the anchor point in the center and use these:

// rotation

a = thisComp.layer("Point A").position;
b = thisComp.layer("Point B").position;
v = b-a;

// scale

a = thisComp.layer("Point A").position;
b = thisComp.layer("Point B").position;
d = length(a,b);
s =( d/width)*100;
[s,s]

// position

a = thisComp.layer("Point A").position;
b = thisComp.layer("Point B").position;
v = (b - a)/2;
(a+b)/2 + [v[1],-v[0]]

Hopefully that will get you headed in the right direction.

Dan

 Re: Scaling and Rotating a Shape between two pointson Mar 10, 2015 at 8:10:01 am

That was more than just the right direction...only made one minor tweak in your suggestion, and it became a complete solution.

(I replaced the generic "width" with the original width of the link object, and then BAM! everything scales perfectly)

P.S. special thanks for the absurdly fast response time (around two hours?!? if you get much faster than that you'll have to open a drive through lane...)