From 83cb0653209f3300e220e76d3f57a000ef4219a6 Mon Sep 17 00:00:00 2001 From: Elizabeth Hunt Date: Sun, 25 Jan 2026 21:01:28 -0800 Subject: Make life grid a torus and vastly simplify audio 'visualization' --- core/src/main/java/coffee/liz/ecs/math/Mat2.java | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) (limited to 'core/src/main/java/coffee/liz/ecs') diff --git a/core/src/main/java/coffee/liz/ecs/math/Mat2.java b/core/src/main/java/coffee/liz/ecs/math/Mat2.java index 8be945c..9975227 100644 --- a/core/src/main/java/coffee/liz/ecs/math/Mat2.java +++ b/core/src/main/java/coffee/liz/ecs/math/Mat2.java @@ -31,7 +31,8 @@ public final class Mat2 { } /** - * Convolves a {@link Convolver} across a matrix. + * Convolves a {@link Convolver} across a matrix reaching neighbors around the + * grid like a torus. * * @param mat * is the row-indexed 2d matrix to convolve. @@ -49,7 +50,7 @@ public final class Mat2 { * @param * is the type of the resulting type of each convolution. */ - public static List> convolve(final List> mat, final Vec2 axes, + public static List> convolveTorus(final List> mat, final Vec2 axes, final Supplier init, final Convolver convolver, final BiFunction finalReduction) { final List> rows = new ArrayList<>(); for (int y = 0; y < mat.size(); y++) { @@ -58,13 +59,9 @@ public final class Mat2 { final T center = mat.get(y).get(x); R result = init.get(); for (int dy = -axes.getY(); dy <= axes.getY(); dy++) { - final int ry = y + dy; - if (ry < 0 || ry >= mat.size()) - continue; + final int ry = Math.floorMod(y + dy, mat.size()); for (int dx = -axes.getX(); dx <= axes.getX(); dx++) { - final int rx = x + dx; - if (rx < 0 || rx >= mat.get(ry).size()) - continue; + final int rx = Math.floorMod(x + dx, mat.get(ry).size()); result = convolver.convolve(mat.get(ry).get(rx), Vec2i.builder().x(dx).y(dy).build(), result); } } -- cgit v1.2.3-70-g09d2