Thank you for writing this. Very useful.
In fieldCurve(), you are push()ing the p vectors to the curve and reversing in place after the first loop. You could instead unshift() to append at the end of the curve so you don't have to reverse.
Yes, unshift sounds to me like a horrible method name.
New code:
function fieldCurve(g, x, y, stepLength, numSteps) {
let p = Vec(x, y);
let q = Vec(x, y);
let n = numSteps >> 1;
let curve = new Curve(p);
while (--n > 0) {
let angle = g.getField(p.x, p.y);
let v = Vec(1, 0).rotate(angle).scale(stepLength);
p = p.add(v);
curve.unshift(p);
}
// curve = curve.reverse();
n = numSteps - (numSteps >> 1);
while (--n > 0) {
let angle = g.getField(q.x, q.y);
let v = Vec(-1, 0).rotate(angle).scale(stepLength);
q = q.add(v);
curve.push(q);
}
return curve;
}