# Calculate difference between two points

I’m trying to work out the distance between two touch points on the iPhone but so far I’m having little success.

I’ve got two ofPoint variables that store point data when two fingers are down. These then get updated on move. Then what I did was use ofDistSquared which I thought would give me the distance between the two points but the numbers I get back are either 0 or really huge. Also the x always comes back as zero, yet the placement of the objects of the screen is always correct!

Anybody any ideas or can show me the way?

Aside from that, I’m totally loving the iPhone dev stuff. Thank you.

Bren.

Hey Brendan,

It is always helpful to post your code with problems like these.

But here is a simple example of how I would do it.

``````
ofPoint p1(100, 100);
ofPoint p2(300, 300);

ofPoint delta = p2 - p1;

float distBetween = sqrt(delta.x * delta.x + delta.y * delta.y);

``````

– how are you printing out your values?
It could be that you are printing a float as an int and it is getting mangled by printf.

All the best,
Theo

hey, yea the printf %i vs %f is quite likely to be why your values are displaying wrong.

on a side note, if you use the ofxVectorMath addon, it has the distance stuff built in:

``````

ofxVec2f p1, p2;
// fill p1 and p2;

ofxVec2f diff = p1 - p2;
float distSquared = diff.lengthSquared();
float dist = diff.length();

``````

That’s great, thanks both.