Euclidian distance - optimization and casting
dstances[i]=(int)sqrt((dx*dx)+(dy*dy));


This only converts to double once. it will do all the math integer style and and then convert it to a double just before calling sqrt.

    distances[i]=(int)sqrt((double)(dx*dx)+(dy*dy));


This is actually the same as:

    distances[i]=(int)sqrt(  ((double)(dx*dx))   + (dy*dy));    


not:

    distances[i]=(int)sqrt((double)(  (dx*dx)) + (dy*dy)  );    


As a result it will convert dx*dx to double, then convert dy*dy to double then add them and the pass it off to sqrt.

In modern computers floating point numbers are really fast, in some cases even faster then integers. As a result, you well be much better off to use double throughout rather then converting from ints to doubles and back again. The conversion between int and double is expensive so avoiding it may actually be your best bet.

A common trick used in algorithms which need to work with distances is to rework the algorithm so that you can use the square of the distance. That way you can avoid the expensive sqrt call. This works because x*x &lt; y*y iff x &lt; y. Be careful though, whether or not you can do this depends on what you are doing with that distance.