# After Effects distance between two points

I have written an expression to get the distance between two points, but it does not work and I haven't a clue why. It's pretty simple:

``````function findDistance(xa, xb, ya, yb)
{
return Math.sqrt( (xa - xb)^2 + (ya - yb)^2 );
}
findDistance(682, 617, 310, 342);
``````

The error I get is, "Error at line 0... invalid numeric result (divide by zero?)."

I am so confused by this. It seems so straight forward, so I am not even sure what to ask. Can anyone see what is wrong with this?

EDIT: I am trying to narrow down the issue. Even using the following expression is not working:

``````Math.sqrt( (682-617)^2 + (310-342)^2 );
``````

Narrowing down further, this finally worked:

``````Math.sqrt( 5249 );
``````

I also isolated and confirmed that X^2 is the correct way to square something, so what gives?

## 2 Answers

Hate to spoil all your good work, but it's a lot easier to use the built-in `length()` function:

``````length(pointa, pointb)
``````

e.g.

``````length(thisComp.layer(1).position, thisComp.layer(2).position)
``````

or

``````length([682, 617], [310, 342]); //raw vector values as an array
length(thisComp.layer("foo").anchorPoint, [310, 342]); //a spatial property and an array
length([123,456,789], [10,9,8]); //works in 3D too.
``````

It's in vector math in the expressions drop-down panel. I figured it out. The syntax and/or order of operation could not handle squaring two numbers before adding them, so I added parenthesis to isolate each number and square them BEFORE adding them together.

``````function findDistance(xa, xb, ya, yb)
{
return Math.sqrt( ((xa - xb)^2) + ((ya - yb)^2) );
}
findDistance(682, 617, 310, 342);
``````