# Purpose of finding distance

#1

What exactly is the purpose of this:

```// How far apart are the 'color' and 'c'? float dist; dist = pow(red - r, 2) + pow(green - g, 2) + pow(blue - b, 2);```

I don’t understand what is going on there at all. I see that we check next to see if it’s the closest distance, but closest distance to what?

#2

That code finds the distance between the elements of NSColor *color and NSColor *c, the format of which are apparently stored in a CGFLoat (which is a typedef for float).
I don’t understand why the pow() function is used here though… why not just subtract the two, like so:

``dist = (red - r) + (green - g) + (blue - b);``

#3

The pow () saves you from getting zero values for distance. Imagine for example the case:
(red - r) == - ((green - g) + (blue - b))

#4

I disagree with ibex10. I believe that the reason why the sample code uses pow() is to calculate the distance between two points on a 2D space. This tells how far “apart” the two colors are.

For a graphical illustration of this, in the color picker, select Color Wheel.

(Of course, I could be wrong, since I am a newbie myself; but this is what came to me.)