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

<pre><code>length(pointa, pointb)
</code></pre>

<p>e.g. </p>

<pre><code>length(thisComp.layer(1).position, thisComp.layer(2).position)
</code></pre>

<p>or</p>

<pre><code>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. 
</code></pre>

<p>It's in <strong>vector math</strong> in the expressions drop-down panel.
<a href="https://i.sstatic.net/chgPx.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/chgPx.png" alt="enter image description here"></a></p>

CC BY-SA 3.0
2016-01-20T01:02:26.957Z
2016-07-20T05:50:33.590Z
2016-07-20T05:50:33.590Z
stib
<p>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.</p>

<pre class="lang-js prettyprint-override"><code>function findDistance(xa, xb, ya, yb)
{
    return Math.sqrt( ((xa - xb)^2) + ((ya - yb)^2) );
}
findDistance(682, 617, 310, 342);
</code></pre>

CC BY-SA 3.0
2016-01-18T16:27:00.500Z
2016-07-20T12:36:58.810Z
2016-07-20T12:36:58.810Z
Ben Mora
<p>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:</p>

<pre class="lang-js prettyprint-override"><code>function findDistance(xa, xb, ya, yb)
{
    return Math.sqrt( (xa - xb)^2 + (ya - yb)^2 );
}
findDistance(682, 617, 310, 342);
</code></pre>

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

<p>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?</p>

<hr>

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

<pre class="lang-js prettyprint-override"><code>Math.sqrt( (682-617)^2 + (310-342)^2 );
</code></pre>

<p>Narrowing down further, this finally worked:</p>

<pre class="lang-js prettyprint-override"><code>Math.sqrt( 5249 );
</code></pre>

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

<after-effects><after-effects-expressions>
After Effects distance between two points
Ben Mora
# After Effects distance between two points

(Asked by: username_0 on 2016-01-18)

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?

**Tags:** after-effects, after-effects-expressions


---

## 2 answers

### Answer by username_1 on 2016-01-20. Score: 6

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.


---

### Answer by username_0 on 2016-01-18. Score: 3

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);


---

