-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsvgPath.js
More file actions
200 lines (157 loc) · 4.73 KB
/
svgPath.js
File metadata and controls
200 lines (157 loc) · 4.73 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
let dim = {w: 0, h: 0};
let width = 1000, height = 1000;
let signalX = [];
let signalY = [];
let signalXFourier = [], signalYFourier = [];
let path = [];
let area;
function preload() {
let path = document.getElementById("path");
let length = Math.floor(path.getTotalLength());
// console.log(path.getTotalLength());
// console.log(path.getPointAtLength(0));
let skip = 20;
for ( let i = 0; i < length; i+= skip) {
let point = path.getPointAtLength(i);
if ( point.x > dim.w ) dim.w = point.x;
if ( point.y > dim.h ) dim.h = point.y;
signalX.push(Math.floor(point.x));
signalY.push(Math.floor(point.y));
}
for ( let i = 0; i < signalX.length; i++) {
let pointX = map(signalX[i], 0, dim.w, 0, height / 4);
let pointY = map(signalY[i], 0, dim.h, 0, height / 4);
signalX[i] = pointX;
signalY[i] = pointY;
}
}
function createFourier(signal) {
signalX = [];
signalY = [];
let skip = 1;
for ( let i = 0; i < signal.length; i+= skip) {
let point = signal[i];
signalX[i] = Math.floor(point.x);
signalY[i] = Math.floor(point.y);
}
console.log(signal, signalX, signalY);
signalYFourier = dft(signalY);
signalXFourier = dft(signalX);
signalXFourier.sort((a, b) => b.amp - a.amp);
signalYFourier.sort((a, b) => b.amp - a.amp);
N = signalYFourier.length;
time = 0;
}
let time = 0;
let N = 0;
function setup() {
console.log(signalX.length);
console.log(signalX, signalY);
createCanvas(width, height);
area = new DrawingArea(0,0, width / 2, height / 2);
signalYFourier = dft(signalY);
signalXFourier = dft(signalX);
signalXFourier.sort((a, b) => b.amp - a.amp);
signalYFourier.sort((a, b) => b.amp - a.amp);
N = signalY.length;
console.log(signalY);
}
function draw() {
background(50);
area.draw();
let dy = epicycle(width/ 4, 3 * height/4, signalYFourier, time, HALF_PI);
let dx = epicycle(3 * width/ 4, height / 4, signalXFourier, time, 0);
let vector = {x: dx.x, y: dy.y};
line(dy.x, dy.y, vector.x, vector.y);
line(dx.x, dx.y, vector.x, vector.y);
ellipse(vector.x, vector.y, 5);
path.push(vector);
//path.unshift(drawFourier(width/4, height/2, 0, test));
time += TWO_PI / N;
//console.log(path);
if ( time >= TWO_PI ) {
time = 0;
path = [];
}
beginShape();
stroke(200);
for ( let i = 0; i < path.length; i++) {
vertex(path[i].x, path[i].y);
}
endShape();
//noLoop();
}
function epicycle(x, y, fourier, time, rotation) {
let off = {x: 0, y: 0};
noFill();
stroke(200);
for ( let i = 0; i < fourier.length; i++) {
let {phase, amp, freq} = fourier[i];
if ( freq == 0 ) continue;
let xNew = x + amp * cos(phase + rotation + freq * time);
let yNew = y + amp * sin(phase + rotation + freq * time);
ellipse(x + off.x, y + off.y, fourier[i].amp * 2);
line(x, y, xNew, yNew);
x = xNew;
y = yNew;
}
return {x, y};
}
function dft(signal) {
let fourier = [];
let N = signal.length;
for ( let i = 0; i < signal.length; i++) {
let re = 0;
let im = 0;
for ( let k = 0; k < signal.length; k++) {
let angle = (i * k * TWO_PI) / N;
let amp = signal[k];
re += amp * cos(angle);
im -= amp * sin(angle);
}
re /= signal.length;
im /= signal.length;
let amp = sqrt(re**2 + im**2);
let phase = atan2(im, re);
let freq = i;
//if ( re < 0 ) phase += PI;
fourier[i] = {re, im, phase, amp, freq};
}
console.log(fourier);
return fourier
}
class DrawingArea {
constructor(x, y, w, h) {
this.loc = {x, y};
this.dim = {w, h};
this.path = [];
}
draw() {
//console.log(this.path);
fill(200);
rect(this.loc.x, this.loc.y, this.dim.w, this.dim.h);
stroke(50);
beginShape();
for ( let i of this.path ) {
vertex(this.loc.x + i.x, this.loc.y + i.y);
}
endShape();
}
mouseEvent() {
if ( mouseX >= this.loc.x && mouseX <= this.loc.x + this.dim.w &&
mouseY >= this.loc.y && mouseY <= this.loc.y + this.dim.h) {
this.path.push({x: mouseX - this.loc.x, y: mouseY - this.loc.y});
if ( this.path.length >= 200) {
createFourier(this.path);
this.path.shift();
path = [];
}
}
}
}
function mousePressed() {
area.mouseEvent();
}
function mouseDragged() {
area.mouseEvent();
}