/
Calculator.java
209 lines (175 loc) · 6.31 KB
/
Calculator.java
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package com.brashmonkey.spriter;
import static java.lang.Math.*;
/**
* A utility class which provides methods to calculate Spriter specific issues,
* like linear interpolation and rotation around a parent object.
* Other interpolation types are coming with the next releases of Spriter.
*
* @author Trixt0r
*
*/
public class Calculator {
public final static float PI = (float)Math.PI;
/**
* Calculates the smallest difference between angle a and b.
* @param a first angle (in degrees)
* @param b second angle (in degrees)
* @return Smallest difference between a and b (between 180 and -180).
*/
public static float angleDifference(float a, float b){
return ((((a - b) % 360) + 540) % 360) - 180;
}
/**
* @param x1 x coordinate of first point.
* @param y1 y coordinate of first point.
* @param x2 x coordinate of second point.
* @param y2 y coordinate of second point.
* @return Angle between the two given points.
*/
public static float angleBetween(float x1, float y1, float x2, float y2){
return (float)toDegrees(atan2(y2-y1,x2-x1));
}
/**
* @param x1 x coordinate of first point.
* @param y1 y coordinate of first point.
* @param x2 x coordinate of second point.
* @param y2 y coordinate of second point.
* @return Distance between the two given points.
*/
public static float distanceBetween(float x1, float y1, float x2, float y2){
float xDiff = x2-x1;
float yDiff = y2-y1;
return (float)sqrt(xDiff*xDiff+yDiff*yDiff);
}
/**
* Solves the equation a*x^3 + b*x^2 + c*x +d = 0.
* @param a
* @param b
* @param c
* @param d
* @return the solution of the cubic function
*/
public static Float solveCubic(float a, float b, float c, float d) {
if (a == 0) return solveQuadratic(b, c, d);
if (d == 0) return 0f;
b /= a;
c /= a;
d /= a;
float squaredB = squared(b);
float q = (3f * c - squaredB) / 9f;
float r = (-27f * d + b * (9f * c - 2f * squaredB)) / 54f;
float disc = cubed(q) + squared(r);
float term1 = b / 3f;
if (disc > 0) {
float s = r + sqrt(disc);
s = (s < 0) ? -cubicRoot(-s) : cubicRoot(s);
float t = r - sqrt(disc);
t = (t < 0) ? -cubicRoot(-t) : cubicRoot(t);
float result = -term1 + s + t;
if (result >= 0 && result <= 1) return result;
} else if (disc == 0) {
float r13 = (r < 0) ? -cubicRoot(-r) : cubicRoot(r);
float result = -term1 + 2f * r13;
if (result >= 0 && result <= 1) return result;
result = -(r13 + term1);
if (result >= 0 && result <= 1) return result;
} else {
q = -q;
float dum1 = q * q * q;
dum1 = acos(r / sqrt(dum1));
float r13 = 2f * sqrt(q);
float result = -term1 + r13 * cos(dum1 / 3f);
if (result >= 0 && result <= 1) return result;
result = -term1 + r13 * cos((dum1 + 2f * PI) / 3f);
if (result >= 0 && result <= 1) return result;
result = -term1 + r13 * cos((dum1 + 4f * PI) / 3f);
if (result >= 0 && result <= 1) return result;
}
return null;
}
/**
* Solves the equation a*x^2 + b*x + c = 0
* @param a
* @param b
* @param c
* @return the solution for the quadratic function
*/
public static Float solveQuadratic(float a, float b, float c) {
float squaredB = squared(b);
float twoA = 2 * a;
float fourAC = 4 * a * c;
float result = (-b + sqrt(squaredB - fourAC)) / twoA;
if (result >= 0 && result <= 1) return result;
result = (-b - sqrt(squaredB - fourAC)) / twoA;
if (result >= 0 && result <= 1) return result;
return null;
}
/**
* Returns the square of the given value.
* @param f the value
* @return the square of the value
*/
public static float squared(float f) { return f * f; }
/**
* Returns the cubed value of the given one.
* @param f the value
* @return the cubed value
*/
public static float cubed(float f) { return f * f * f; }
/**
* Returns the cubic root of the given value.
* @param f the value
* @return the cubic root
*/
public static float cubicRoot(float f) { return (float) pow(f, 1f / 3f); }
/**
* Returns the square root of the given value.
* @param x the value
* @return the square root
*/
public static float sqrt(float x){ return (float)Math.sqrt(x); }
/**
* Returns the arc cosine at the given value.
* @param x the value
* @return the arc cosine
*/
public static float acos(float x){ return (float)Math.acos(x); }
static private final int SIN_BITS = 14; // 16KB. Adjust for accuracy.
static private final int SIN_MASK = ~(-1 << SIN_BITS);
static private final int SIN_COUNT = SIN_MASK + 1;
static private final float radFull = PI * 2;
static private final float degFull = 360;
static private final float radToIndex = SIN_COUNT / radFull;
static private final float degToIndex = SIN_COUNT / degFull;
/** multiply by this to convert from radians to degrees */
static public final float radiansToDegrees = 180f / PI;
static public final float radDeg = radiansToDegrees;
/** multiply by this to convert from degrees to radians */
static public final float degreesToRadians = PI / 180;
static public final float degRad = degreesToRadians;
static private class Sin {
static final float[] table = new float[SIN_COUNT];
static {
for (int i = 0; i < SIN_COUNT; i++)
table[i] = (float)Math.sin((i + 0.5f) / SIN_COUNT * radFull);
for (int i = 0; i < 360; i += 90)
table[(int)(i * degToIndex) & SIN_MASK] = (float)Math.sin(i * degreesToRadians);
}
}
/** Returns the sine in radians from a lookup table. */
static public final float sin (float radians) {
return Sin.table[(int)(radians * radToIndex) & SIN_MASK];
}
/** Returns the cosine in radians from a lookup table. */
static public final float cos (float radians) {
return Sin.table[(int)((radians + PI / 2) * radToIndex) & SIN_MASK];
}
/** Returns the sine in radians from a lookup table. */
static public final float sinDeg (float degrees) {
return Sin.table[(int)(degrees * degToIndex) & SIN_MASK];
}
/** Returns the cosine in radians from a lookup table. */
static public final float cosDeg (float degrees) {
return Sin.table[(int)((degrees + 90) * degToIndex) & SIN_MASK];
}
}