java - Creating formula for distance and damage -


    public double getdamage(double distance){         int damage1 = 30; // (0 - 38.1)         int damage2 = 20; // (50.8 - *)         double range1 = 38.1;         double range2 = 50.8;          double damage = 0; // formula          return damage;     } 

i try create formula calculate amount of damage has been effected distance.

(variable distance =) 0 till 38.1 metre return 30 damage.
50.8 till inifite return 20 damage.
38.1 till 50.8 decrease linear 30 -> 20.

how can make method work?
in advance.

sounds this:

double x = (distance - range1) / (range2 - range1); if (x < 0)   x = 0; if (x > 1)   x = 1; return damage1 + x * (damage2 - damage1); 

basically follow linear rule , adjust stay in linear interval.


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -