-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvis.ts
More file actions
89 lines (78 loc) · 2.15 KB
/
vis.ts
File metadata and controls
89 lines (78 loc) · 2.15 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import * as PIXI from 'pixi.js';
export class SpringMass {
public pixi: PIXI.Container;
private mass: PIXI.Graphics;
private spring: PIXI.Graphics;
private realLength: number;
private _extension: number;
private COIL_LENGTH = 40;
constructor(private springLen: number, private massSize: number) {
this.realLength = springLen - massSize / 2;
this.spring = new PIXI.Graphics();
this.spring.lineStyle({
width: 5,
color: 0x999999
});
this.spring.moveTo(0, 0);
for (let x = 0; x <= this.realLength - (this.realLength % (this.COIL_LENGTH / 2)); x++) {
this.spring.lineTo(x, -Math.sin((x * 2 * Math.PI) / this.COIL_LENGTH) * 50);
}
this.spring.lineTo(this.realLength, 0);
this.mass = new PIXI.Graphics();
this.mass.beginFill(0);
this.mass.drawRect(0, 0, massSize, massSize);
this.mass.endFill();
this.mass.pivot.x = this.mass.width / 2;
this.mass.pivot.y = this.mass.height / 2;
this.mass.x = springLen;
this.pixi = new PIXI.Container();
this.pixi.addChild(this.spring);
this.pixi.addChild(this.mass);
}
public set extension(e: number) {
this.mass.x = this.springLen + e;
this.spring.scale.x = (this.realLength + e) / this.realLength;
this._extension = e;
}
public get extension() {
return this._extension;
}
}
export type Vector = {
x: number;
y: number;
};
export class VectorField {
public pixi: PIXI.Container;
SIZE = 400;
constructor(_f: (v: Vector) => Vector) {
this.pixi = new PIXI.Container();
const lines = new PIXI.Graphics();
lines.lineStyle({
width: 3,
color: 0
});
lines.moveTo(-this.SIZE, 0);
lines.lineTo(this.SIZE, 0);
lines.moveTo(0, -this.SIZE);
lines.lineTo(0, this.SIZE);
lines.lineStyle({
width: 1,
color: 0
});
for (let x = 0; x < this.SIZE; x += this.SIZE / 10) {
lines.moveTo(x, this.SIZE);
lines.lineTo(x, -this.SIZE);
lines.moveTo(-x, this.SIZE);
lines.lineTo(-x, -this.SIZE);
}
for (let y = 0; y < this.SIZE; y += this.SIZE / 10) {
lines.moveTo(this.SIZE, y);
lines.lineTo(-this.SIZE, y);
lines.moveTo(this.SIZE, -y);
lines.lineTo(-this.SIZE, -y);
}
// function drawVector(x: number, y: number) {}
this.pixi.addChild(lines);
}
}