Skip to content

Instantly share code, notes, and snippets.

@tdhooper
Created December 13, 2019 14:59
Show Gist options
  • Save tdhooper/a9c21ec6b10b31691ab63b51e2595f99 to your computer and use it in GitHub Desktop.
Save tdhooper/a9c21ec6b10b31691ab63b51e2595f99 to your computer and use it in GitHub Desktop.
Stellar magnetic field
const sub = (a, b) => [
a[0] - b[0],
a[1] - b[1],
];
const distance = (a, b) => Math.sqrt(
Math.pow(a[0] - b[0], 2) +
Math.pow(a[1] - b[1], 2)
);
const length = (a) => Math.sqrt(
Math.pow(a[0], 2) +
Math.pow(a[1], 2)
);
const normalize = (a) => {
const l = length(a);
return [
a[0] / l,
a[1] / l,
];
};
const rand = (n) => {
const f = Math.sin(n) * 4375.5453123;
return f - Math.floor(f);
};
// What function to produce streamlines for
let vectorField = function(p) {
p = [p.x, p.y];
let v = [0,0];
let o, r, s, m, d;
const limit = 12.;
for (let i = 0; i < limit; i++) {
r = rand(i/limit) - .5;
o = i + r * 2.;
a = [
Math.sin(o / limit * Math.PI * 2),
Math.cos(o / limit * Math.PI * 2)
];
a[0] *= 2;
a[1] *= 2;
d = distance(p, a);
a = sub(p, a);
a = normalize(a);
a[0] /= d;
a[1] /= d;
s = Math.sign((i % 2) -.5 );
a[0] *= s;
a[1] *= s;
v = sub(v, a);
}
v = normalize(v);
return {
x: v[0],
y: v[1]
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment