-
Notifications
You must be signed in to change notification settings - Fork 0
/
electrostatics.vl
42 lines (31 loc) · 933 Bytes
/
electrostatics.vl
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
// Welcome to VectorLang!
// example: electrostatics
external Q1 = 1;
external Q1pos = {3.5, 3.5};
external Q2 = -1;
external Q2pos = {6.5, 6.5};
external gridWidth = 10;
const step = 0.5;
def fieldComponent(vector pos, number q, vector qPos) -> vector = [
val diff := pos - qPos;
if (pos != qPos) diff.normalized() * (q / diff.length().sqr())
else {0, 0}
];
def field(vector pos) -> vector
= fieldComponent(pos, Q1, Q1pos) + fieldComponent(pos, Q2, Q2pos);
def plotField(vector pos) -> void = if (pos.x <= gridWidth and pos.y <= gridWidth) [
push();
translate(pos);
val pf := field(pos);
if (pf.length() > step) [
fill(WHITE.blend(RED, (pf.length() - step) / step));
pf := pf.clampLength(0, step);
];
plot(pf);
pop();
plotField(pos + (
if (pos.x >= gridWidth) {-pos.x, step}
else {step, 0}
));
];
def main() -> void = plotField({0, 0});