Last active
August 18, 2017 19:17
-
-
Save gncgnc/559630f460cadf8d1a830257d3c63045 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function MagneticField() { | |
this.mmts = []; // magnetic moments in the field | |
this.addmmt = function(mx,my,px,py) { | |
this.mmts.push({ | |
m: new p5.Vector(mx,my), // the moment vector | |
pos: new p5.Vector(px,py) // its position in the field | |
}) | |
} | |
this.getValue = function(x,y) { | |
var v = new p5.Vector(0,0) | |
for (var i = 0; i < this.mmts.length; i++) { | |
var mm = this.mmts[i] | |
var r = new p5.Vector(x,y).sub(mm.pos).mult(0.01), | |
rmag = r.mag(), | |
v1 = p5.Vector.mult(r, 3 * mm.m.dot(r) / pow(rmag,5)) | |
v2 = p5.Vector.mult(mm.m, -pow(rmag,-3)) | |
v.add(p5.Vector.add(v1,v2)) | |
} | |
return v | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Particle(x,y, f) { | |
this.pos = new p5.Vector(x,y); | |
this.inipos = this.pos.copy(); | |
this.vel = new p5.Vector(0,0); | |
this.f = f; | |
this.hist = [] | |
this.dispLength = 0; | |
this.dispStart = 0; | |
this.done = false | |
this.color = color(255); | |
this.show = function() { | |
pg.stroke(255) | |
var startI = int(this.dispStart*this.hist.length), | |
endI = startI + int(this.dispLength*this.hist.length) | |
pg.beginShape() | |
for (var i = startI; i < endI; i++) { | |
if(i>=this.hist.length || i<0) continue; | |
pg.vertex(this.hist[i].x,this.hist[i].y) | |
} | |
pg.endShape() | |
} | |
// integrates field f, | |
// optionally integrates backward from starting point | |
this.update = function(n,h) { | |
var n = n ? n : 1; | |
var h = h ? h : 0.01; | |
for (var i=0; i<n; i++) { | |
var midVal = p5.Vector.add( | |
this.pos, | |
p5.Vector.mult( | |
this.f.getValue(this.pos.x,this.pos.y), | |
h/2) | |
) | |
this.pos.add( | |
p5.Vector.mult( | |
this.f.getValue(midVal.x,midVal.y), | |
h | |
).limit(5) | |
) | |
} | |
this.hist.push(this.pos.copy()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment