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.

blend n number of values by distance example

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:

enter image description here

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!

  • 1
    $\begingroup$ Look up barycentric coordinates $\endgroup$ – bubba Aug 15 '14 at 12:38
  • $\begingroup$ @bubba this looks like a much more precise aproeach, will deffently look more in to this aswell. thanks! $\endgroup$ – 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:


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:


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.