# Blend n number of values by distance

I have n number of values which each have a distance that determens how much of the amount that should be blended.

I've tried to illustrate my problem visually: The blue numbers is the values, the white numer is the distance. and the orange is the result i would like the function to produce.

If each value, was an RGB Struct, and the function i'm looking for where to be run for each point within 3 points that makes up a triangle, the result should look somewhat like this:

So how would i mathmaticly blend these values, by only knowing their distance to the green dot? Hope you can help and my question issn't too stupid. Thanks!

• Look up barycentric coordinates – bubba Aug 15 '14 at 12:38
• @bubba this looks like a much more precise aproeach, will deffently look more in to this aswell. thanks! – BjarkeCK Aug 15 '14 at 13:42

It looks like you want each point to have a weighting inversely proportional to its distance from the green point.

Say our $4$ amounts are $a_1, a_2, a_3, a_4$ and their distances $d_1, d_2, d_3, d_4$.

The weight given to the $i^{th}$ point should then be:

$$\dfrac{1/d_i}{1/d_1 + 1/d_2 + 1/d_3 + 1/d_4} = \dfrac{1/d_i}{\sum_{j=1}^{4}{1/d_j}}$$

And we multiply that value by $a_i$ to get the amount contributed by the $i^{th}$ point.

Summing that for the four points gives the answer we want:

$$\dfrac{\sum_{i=1}^{4}{a_i/d_i}}{\sum_{j=1}^{4}{1/d_j}}$$

Because it's possible for one of the distances to be $0$, as in your third example, we first need a rule that if $d_i = 0$ for some $i$ then the result is simply $a_i$.

How this works for your second example:

\begin{eqnarray*} \mbox{Result} &=& \dfrac{\sum_{i=1}^{4}{a_i/d_i}}{\sum_{j=1}^{4}{1/d_j}} \\ && \\ &=& \dfrac{100/80 + 100/80 + 200/80 + 200/80}{1/80 + 1/80 + 1/80 + 1/80} \\ && \\ &=& 150 \end{eqnarray*}

Of course, you can easily change the formula from $4$ to any number $n$ of points:

$$\dfrac{\sum_{i=1}^{n}{a_i/d_i}}{\sum_{j=1}^{n}{1/d_j}}$$

• Man, thank you so much. it works splendidly! – BjarkeCK Aug 15 '14 at 13:41