fieldDirectionSampler = (x, y, poles) => {
let noise = perlin.gen(x / width * perlinScale, y / height * perlinScale) * 2 * Math.PI
let noiseStr = 0.000002
let perlinBackground = {
sumx: Math.cos(noise) * noiseStr,
sumy: Math.sin(noise) * noiseStr
}
let sumfield = poles.reduce((memo, pole) => {
let dx = (x - pole.x)
let dy = (y - pole.y)
let r = Math.hypot(dx, dy)
let magnitude = pole.str / Math.pow(r, 1.7)
let theta = vectorForGivenPositionAndPole(x, y, pole.x, pole.y, pole.positive)
let fieldx = magnitude * Math.cos(theta)
let fieldy = magnitude * Math.sin(theta)
return {
sumx: memo.sumx + fieldx,
sumy: memo.sumy + fieldy
}
}, perlinBackground)
return Math.atan2(sumfield.sumy, sumfield.sumx) + Math.PI / 2
}