-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
48 lines (40 loc) · 1002 Bytes
/
sketch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
let Field, Mag;
function setup() {
createCanvas(600, 600);
Field = new VectorField(400,400, 40, 40, createVector(0,0));
Mag = new Magnet(createVector(200,200), 60, 1.5);
stroke(255);
strokeWeight(2);
}
let frame = 1;
function draw() {
background(0);
translate(200,200);
scale(0.5);
if(frame %1 ==0)
{
Mag.Angle+= 0.01;
}
frame++;
drawMagneticFieldLines();
}
function drawMagneticFieldLines() {
noFill();
stroke(100, 100, 255);
for (let i = 0; i < 10; i++) {
let angle = map(i, 0, 10, -HALF_PI, 3*HALF_PI);
let startX = Mag.Pos.x + Mag.Length / 2 * cos(angle);
let startY = Mag.Pos.y + Mag.Length / 2 * sin(angle);
drawFieldLine(createVector(startX, startY), angle);
}
}
function drawFieldLine(start, angle) {
let pos = start.copy();
beginShape();
for (let i = 0; i < 1000; i++) {
let strengthVec = Mag.CalculateMagneticField(pos).normalize().mult(3);
pos.add(strengthVec);
vertex(pos.x, pos.y);
}
endShape();
}