Skip to content

Commit 3da2ad1

Browse files
committed
Clean up FlowField
1 parent 12e432e commit 3da2ad1

File tree

3 files changed

+27
-23
lines changed

3 files changed

+27
-23
lines changed

app/src/main/java/com/w2sv/autocrop/flowfield/FlowField.java

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,44 @@
22

33
import android.util.Pair;
44

5-
import androidx.annotation.NonNull;
6-
7-
import java.util.ArrayList;
85
import java.util.HashMap;
6+
import java.util.Iterator;
97
import java.util.Map;
108

119
import processing.core.PApplet;
1210
import processing.core.PVector;
1311

1412
class FlowField {
1513
private static final Map<Pair<Integer, Integer>, Float> xOffCache = new HashMap<>();
16-
private float zOff = 0;
1714

18-
void update(ArrayList<Particle> particles, PApplet parent) {
19-
var pos2Noise = new HashMap<Pair<Integer, Integer>, PVector>();
15+
private float zOff = 0;
2016

21-
for (int i = 0; i < particles.size(); i++) {
22-
var pos = new Pair<>(
23-
PApplet.floor(particles.get(i).pos.x / Sketch.Config.FLOW_FIELD_GRANULARITY) + 1,
24-
PApplet.floor(particles.get(i).pos.y / Sketch.Config.FLOW_FIELD_GRANULARITY) + 1
25-
);
17+
void updateAndApplyTo(Iterator<Particle> particles, PApplet parent) {
18+
HashMap<Pair<Integer, Integer>, PVector> forceCash = new HashMap<>();
2619

27-
if (pos2Noise.containsKey(pos))
28-
particles.get(i).applyFlowFieldVector(pos2Noise.get(pos));
29-
else {
30-
PVector v = PVector.fromAngle(parent.noise(xOff(pos), 0, zOff) * 25.132742f).normalize();
31-
particles.get(i).applyFlowFieldVector(v);
32-
pos2Noise.put(pos, v);
33-
}
34-
}
20+
particles.forEachRemaining((particle -> particle.applyForceVector(
21+
getForceVector(
22+
Pair.create(
23+
PApplet.floor(particle.pos.x / Sketch.Config.FLOW_FIELD_GRANULARITY) + 1,
24+
PApplet.floor(particle.pos.y / Sketch.Config.FLOW_FIELD_GRANULARITY) + 1
25+
),
26+
forceCash,
27+
parent
28+
)
29+
)));
3530

3631
zOff += Sketch.Config.FLOW_FIELD_Z_OFF_INCREMENT;
3732
}
3833

39-
private float xOff(@NonNull Pair<Integer, Integer> pos) {
34+
private PVector getForceVector(Pair<Integer, Integer> pos, HashMap<Pair<Integer, Integer>, PVector> forceCash, PApplet parent) {
35+
if (forceCash.containsKey(pos))
36+
return forceCash.get(pos);
37+
PVector v = PVector.fromAngle(parent.noise(getXOff(pos), 0, zOff) * 25.132742f).normalize();
38+
forceCash.put(pos, v);
39+
return v;
40+
}
41+
42+
private float getXOff(Pair<Integer, Integer> pos) {
4043
if (xOffCache.containsKey(pos))
4144
//noinspection ConstantConditions
4245
return xOffCache.get(pos);

app/src/main/java/com/w2sv/autocrop/flowfield/Particle.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public Particle(PApplet parent) {
4242
previousPos = pos.copy();
4343
}
4444

45-
void applyFlowFieldVector(PVector v) {
45+
void applyForceVector(PVector v) {
4646
acc = v;
4747
}
4848

app/src/main/java/com/w2sv/autocrop/flowfield/Sketch.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ static class Config{
3030
0xFF6B13B5, // purple
3131
0xFF1a0ac7 // ocean blue
3232
);
33+
static final int BACKGROUND_COLOR = 0;
3334
static final int FLOW_FIELD_GRANULARITY = 200;
3435
static final float FLOW_FIELD_Z_OFF_INCREMENT = 0.004f;
3536
}
@@ -53,7 +54,7 @@ public void setup() {
5354
// enable 120fps for devices being capable of it; Otherwise the fps produced by the sketch will
5455
// accommodate the hardware-determined limit
5556
frameRate(120);
56-
background(0);
57+
background(Config.BACKGROUND_COLOR);
5758

5859
// initialize particles
5960
Particle.setFlowFieldDimensions(width, height);
@@ -65,7 +66,7 @@ public void setup() {
6566

6667
@Override
6768
public void draw() {
68-
flowfield.update(particles, this);
69+
flowfield.updateAndApplyTo(particles.iterator(), this);
6970

7071
alphaDropper.dropAlphaIfDue(millis(), g);
7172
Particle.colorHandler.changeColorIfDue(millis(), g);

0 commit comments

Comments
 (0)