private double LimitValue(double value, double max) { if (value < 0) value = 0; if (value > max) value = max; return value; }