-
Notifications
You must be signed in to change notification settings - Fork 0
/
Renderer.js
70 lines (59 loc) · 1.97 KB
/
Renderer.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
class Renderer {
constructor(dftFunction, vertices) {
this.dftFunction = dftFunction;
this.vertices = vertices;
this.path = [];
}
/*
* Builds all epicycle data based on the dft results
*/
buildEpicycles() {
this.epicycles = [];
const position = new Complex(0, 0);
for (let i=0; i<this.vertices.length; i++) {
const initialDirectionVector = this.dftFunction[i]; // y-Axis (offset/position)
const radialVelocity = i - floor(this.vertices.length / 2); // x-Axis (frequency/velocity)
const epicycle = new Epicycle(position, initialDirectionVector, radialVelocity);
this.epicycles.push(epicycle);
position.add(initialDirectionVector);
}
this.epicycles = this.epicycles.sort((a, b) => b.size - a.size);
}
/*
* Renders all epicycles
*/
render() {
// Draw epicycles
let currentCoords = new Complex(0, 0);
for (const epicycle of this.epicycles) {
epicycle.update();
epicycle.draw(currentCoords);
currentCoords = epicycle.getPlanetPosition();
}
// Draw vertex history (result line of epicycle drawing)
const lastPlanet = this.epicycles[this.epicycles.length-1];
let previousPathPoint = this.path[0];
this.path.push(lastPlanet.getPlanetPosition());
if (this.path.length > 1) {
let i=128;
for (const vertex of this.path) {
colorMode(HSB, 255);
stroke(i%255, 255, 255);
strokeWeight(2);
if (customMode || dist(vertex.x, vertex.y, previousPathPoint.x, previousPathPoint.y) < 0.8)
line(vertex.x * SCALE, vertex.y * SCALE, previousPathPoint.x * SCALE, previousPathPoint.y * SCALE);
previousPathPoint = vertex;
i+=0.2;
}
}
// Draw vertices (user inputs)
if (drawData) {
colorMode(RGB, 255);
for (const vertex of this.vertices) {
stroke(255);
strokeWeight(4);
point(vertex.x * SCALE, vertex.y * SCALE);
}
}
}
}