Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

I'm in the middle of programming something, and I really need a formula that can go from 0 to 1 depending on the distance an object is to another one. I can get the formula to return 1 if the objects' distance equals 0, but I need it to also take into account a minimum variable.

In short (and perhaps more accurately), if an object is beyond maxdist away, the formula should return 0 or less. If it's closer than mindist, it should return 1 or greater. I can clamp the values when necessary. This is what I have so far.

 blenddist = 1.0 - ( ( 1.0 / gimaxdist) * dist)

Where would I work in mindist to allow me to set the 'zero-point'?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

Let $d$ be the distance, let $m$ be what you're calling $mindist$, let $M$ be what you're calling $maxdist$, let $b$ be what you're calling $blenddist$. Then $$b={d-M\over m-M}$$ does what you ask.

share|improve this answer
Thanks, I'll try it out right now. –  SolarLune Jun 4 '12 at 2:54
Just tried it - it works perfectly. Thanks! –  SolarLune Jun 4 '12 at 3:04

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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