-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPercentageMath.c
65 lines (54 loc) · 2 KB
/
PercentageMath.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//
// PercentageMath.c
// Brain
//
// Created by Evan on 5/16/12.
// Copyright (c) 2012 Super Top Secret. All rights reserved.
//
#import "PercentageMath.h"
#import "math.h"
double PercentBetween(double value, double min, double max) {
double den = max - min;
if (den == 0.0f) {
return min;
} else {
double num = value - min;
return num / den;
}
}
double PercentBetweenQuadratic(double value, double min, double max) {
return PercentBetweenExponential(value, min, max, 2);
}
double PercentBetweenCubic(double value, double min, double max) {
return PercentBetweenExponential(value, min, max, 3);
}
double PercentBetweenQuartic(double value, double min, double max) {
return PercentBetweenExponential(value, min, max, 4);
}
double PercentBetweenExponential(double value, double min, double max, double exp) {
return pow(PercentBetween(value, min, max), exp);
}
double ValueBetween(double percent, double min, double max) {
return min + (max - min) * percent;
}
double ValueBetweenByPercent(double value, double pMin, double pMax, double vMin, double vMax)
{
double p = PercentBetween(value, pMin, pMax);
double v = ValueBetween(p, vMin, vMax);
return v;
}
double ValueBetweenByPercentQuadratic(double value, double pMin, double pMax, double vMin, double vMax) {
return ValueBetweenByPercentExponential(value, pMin, pMax, vMin, vMax, 2);
}
double ValueBetweenByPercentCubic(double value, double pMin, double pMax, double vMin, double vMax) {
return ValueBetweenByPercentExponential(value, pMin, pMax, vMin, vMax, 3);
}
double ValueBetweenByPercentQuartic(double value, double pMin, double pMax, double vMin, double vMax) {
return ValueBetweenByPercentExponential(value, pMin, pMax, vMin, vMax, 4);
}
double ValueBetweenByPercentExponential(double value, double pMin, double pMax, double vMin, double vMax, double exp)
{
double p = PercentBetweenExponential(value, pMin, pMax, exp);
double v = ValueBetween(p, vMin, vMax);
return v;
}