Forums > MaxMSP

### simple distance issue

Nov 13 2012 | 3:17 pm

couldn’t seem to find an answer to my problem. just calculating the distance between two points in cv.jit.track: any idea what in my expression may cause a "nan" output? am I bracketing incorrectly?

expr sqrt(((\$i1 – \$i2) ^ 2) + ((\$i3 – \$i4) ^ 2))

Nov 13 2012 | 3:37 pm

expr sqrt (((a-b)^2)+((c-d)^2)) i.e. the square root of the sum of the quantity of a – b squared plus the quantity c – d squared… right? looks right to me

Nov 13 2012 | 3:59 pm

^ does a Bitwise XOR, try:

`expr sqrt(((\$i1 - \$i2) * (\$i1 - \$i2)) + ((\$i3 - \$i4) * (\$i3 - \$i4)))`

Nov 13 2012 | 4:02 pm

Hi,

well, first you’re taking the difference of \$i1 and \$i2 and then you apply the ‘bitwise exclusive or’ operator with the result and 2. This is certainly NOT what you wish to achieve…

The ‘power’ function in expr is called ‘pow’, so you should do `expr sqrt ( pow ( \$i1 - \$i2 , 2 ) + pow ( \$i3 - \$i4 , 2 ) )`. On the other hand, the power function is quite expensive computationally compared to a simple multiplication, so for squaring a value, you should better use the form

`expr sqrt ( (\$i1 - \$i2)*(\$i1 - \$i2) + (\$i3 - \$i4)*(\$i3 - \$i4) )`

HTH,
Ádám

Nov 13 2012 | 6:27 pm

ahh, thanks for identifying the snag

Nov 13 2012 | 6:40 pm

Apropos of nothing, I’ll point out that polar coordinates are not the solution to the problem.

``````-- Pasted Max  Patch, click to expand. --

```
```
Nov 13 2012 | 10:07 pm

where is c*c when you need it.

Nov 14 2012 | 6:00 pm

sorry if I misinformed. "^" is a symbol for "the ‘bitwise exclusive or’ operator with the result and 2"?? Thought it indicated an exponent. My apologies.

Viewing 8 posts - 1 through 8 (of 8 total)

Forums > MaxMSP