views:

257

answers:

2

It is described as -||xi-xy||^2.

So for 2 two dimensional points do I code it like this?

- ((x1-x2) + (y1-y2))^2

or

-( (x1-x2)^2 + (y1-y2)^2 )

or

-(sqrt( (x1-x2)^2 + (y1-y2)^2 ))

or some other way?

+7  A: 

The correct answer is

-( (x1-x2)^2 + (y1-y2)^2 )

The mathematical description is accurate, but not useful for implementation. It's stated as the square of the distance between the points, which if implemented directly would be something like:

len = sqrt( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) );
result = -( len*len );

which can simplified to

result = -( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) );

which is your #2.

tfinniga
There's really no reason for the `sqrt` and the latter `-` operation on the square. Just apply `-` to the sum of the squares (after eliminating `sqrt`) as that value will always be positive.
Adam Robinson
Thanks Adam, I clarified my answer.
tfinniga
+1  A: 

The third is the negative of the distance. The second appears to be the negative of the square of the distance.

Jerry Coffin