remap a value slightly different
in
Programming Questions
•
2 years ago
the function is a bit remade here to keep it more simple to show.
I have
example.getNextFloatResult(min, max));
however the minAllowed and/or maxAllowed should only be used if the minAllowed is higher then the minData in the class or the maxAllowed is lower then the maxData.
However it's more complex but i can't figure it out.
In the example i have
float minAllowed = 5;
float maxAllowed = 7;
and the result is 10, but it should be between 5 and 7
I have
example.getNextFloatResult(min, max));
however the minAllowed and/or maxAllowed should only be used if the minAllowed is higher then the minData in the class or the maxAllowed is lower then the maxData.
However it's more complex but i can't figure it out.
In the example i have
float minAllowed = 5;
float maxAllowed = 7;
and the result is 10, but it should be between 5 and 7
- Example example;
void setup() {
float[] values = {
10, 20, 50, 80
};
example = new Example(values);
float minAllowed = 5;
float maxAllowed = 7;
// is 10, should be else
println(example.getNextFloatResult(minAllowed, maxAllowed));
}
class Example {
float minData;
float maxData;
float[] values;
Example(float[] values) {
this.values = values;
minData = MAX_FLOAT;
maxData = MIN_FLOAT;
for (int i = 0; i < values.length; i++) {
if (values[i] < minData) minData = values[i];
if (values[i] > maxData) maxData = values[i];
}
}
float getNextFloatResult(float minAllowed, float maxAllowed) {
// if both min and max are outside current range
if (minData < minAllowed && maxData > maxAllowed ) {
println("m1");
return map(values[0], minData, maxData, minAllowed, maxAllowed);
}
else if (minData < minAllowed) {
println("m2");
return map(values[0], minData, maxData, minAllowed, maxData);
}
else if (maxData > maxAllowed) {
println("m3");
return map(values[0], minData, maxData, minData, maxAllowed);
}
println("m4");
return values[0];
}
}
1