Skip to content

Commit fd78b39

Browse files
committed
float array math
1 parent 8032022 commit fd78b39

File tree

2 files changed

+35
-21
lines changed

2 files changed

+35
-21
lines changed

src/main/java/com/cleanroommc/modularui/test/TestGuis.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import com.cleanroommc.modularui.utils.GameObjectHelper;
3333
import com.cleanroommc.modularui.utils.Interpolation;
3434
import com.cleanroommc.modularui.utils.Interpolations;
35-
import com.cleanroommc.modularui.utils.MathUtils;
3635
import com.cleanroommc.modularui.utils.Platform;
3736
import com.cleanroommc.modularui.utils.SpriteHelper;
3837
import com.cleanroommc.modularui.utils.fakeworld.ArraySchema;
@@ -535,17 +534,15 @@ public static ModularPanel buildCollapseDisabledChildrenUI() {
535534
}
536535

537536
public static @NotNull ModularPanel buildGraphUI() {
538-
float[] x = FloatArrayMath.linspace(-5, 5, 100);
539-
float[] y = FloatArrayMath.sin(x, null);
537+
float[] x = FloatArrayMath.linspace(-25, 25, 200);
538+
// sin(x) / x
539+
float[] y1 = FloatArrayMath.div(FloatArrayMath.sin(x, null), x, null);
540540
return new ModularPanel("graph")
541-
.size(200, 200)
541+
.size(200, 160)
542542
.padding(5)
543543
.overlay(new GraphDrawable()
544-
.yLim(-1.2f, 1.2f)
545-
.xTickFinder(MathUtils.PI_HALF, 1)
546-
.yTickFinder(0.2f, 1)
547544
.graphAspectRatio(16 / 9f)
548-
.plot(x, y));
545+
.plot(x, y1));
549546
}
550547

551548
public static @NotNull ModularPanel buildAspectRatioUI() {

src/main/java/com/cleanroommc/modularui/utils/FloatArrayMath.java

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,7 @@ public static float min(float[] arr) {
171171
* @return result array
172172
*/
173173
public static float[] plus(float[] src, float op, float @Nullable [] res) {
174-
if (res == null) res = new float[src.length];
175-
int n = Math.min(src.length, res.length);
176-
for (int i = 0; i < n; i++) res[i] = src[i] + op;
177-
return res;
174+
return applyEach(src, v -> v + op, res);
178175
}
179176

180177
/**
@@ -186,11 +183,7 @@ public static float[] plus(float[] src, float op, float @Nullable [] res) {
186183
* @return result array
187184
*/
188185
public static float[] plus(float[] src, float[] op, float @Nullable [] res) {
189-
if (src.length != op.length) throw new IllegalArgumentException("Can't add arrays of different size.");
190-
if (res == null) res = new float[src.length];
191-
int n = Math.min(src.length, res.length);
192-
for (int i = 0; i < n; i++) res[i] = src[i] + op[i];
193-
return res;
186+
return applyEach(src, op, Float::sum, res);
194187
}
195188

196189
/**
@@ -202,16 +195,21 @@ public static float[] plus(float[] src, float[] op, float @Nullable [] res) {
202195
* @return result array
203196
*/
204197
public static float[] mult(float[] src, float op, float @Nullable [] res) {
205-
if (res == null) res = new float[src.length];
206-
int n = Math.min(src.length, res.length);
207-
for (int i = 0; i < n; i++) res[i] = src[i] * op;
208-
return res;
198+
return applyEach(src, v -> v * op, res);
199+
}
200+
201+
public static float[] mult(float[] src, float[] op, float @Nullable [] res) {
202+
return applyEach(src, op, (v, op1) -> v * op1, res);
209203
}
210204

211205
public static float[] div(float[] src, float op, float @Nullable [] res) {
212206
return mult(src, 1 / op, res);
213207
}
214208

209+
public static float[] div(float[] src, float[] op, float @Nullable [] res) {
210+
return applyEach(src, op, (v, op1) -> v / op1, res);
211+
}
212+
215213
public static float[] diff(float[] src) {
216214
if (src.length < 2) return EMPTY;
217215
if (src.length == 2) return new float[]{src[1] - src[0]};
@@ -247,6 +245,16 @@ public static float[] applyEach(float[] src, float[] operands1, float[] operands
247245
return res;
248246
}
249247

248+
public static float[] applyEach(float[] src, float[][] operands, NFloatOperator op, float @Nullable [] res) {
249+
if (src.length != operands.length) {
250+
throw new IllegalArgumentException("Can't apply operator to operands of different size.");
251+
}
252+
if (res == null) res = new float[src.length];
253+
int n = Math.min(src.length, res.length);
254+
for (int i = 0; i < n; i++) res[i] = op.apply(src[i], operands[i]);
255+
return res;
256+
}
257+
250258
public static float[] abs(float[] src, float @Nullable [] res) {
251259
return applyEach(src, Math::abs, res);
252260
}
@@ -259,6 +267,10 @@ public static float[] cos(float[] src, float @Nullable [] res) {
259267
return applyEach(src, MathUtils::cos, res);
260268
}
261269

270+
public static float[] tan(float[] src, float @Nullable [] res) {
271+
return applyEach(src, MathUtils::tan, res);
272+
}
273+
262274
public static float[] clamp(float[] src, float min, float max, float @Nullable [] res) {
263275
return applyEach(src, v -> MathUtils.clamp(v, min, max), res);
264276
}
@@ -291,4 +303,9 @@ public interface TernaryFloatOperator {
291303

292304
float apply(float v, float op1, float op2);
293305
}
306+
307+
public interface NFloatOperator {
308+
309+
float apply(float v, float[] op);
310+
}
294311
}

0 commit comments

Comments
 (0)