-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsketch.js
More file actions
257 lines (222 loc) · 8.03 KB
/
sketch.js
File metadata and controls
257 lines (222 loc) · 8.03 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// Drawing SVGs using DFT
// Jkutkut
// FUNCTIONS:
/**
* Given the discrete values of the function, returns a array of objects with the freq, amp and phase.
* @param {Array} f - Array with the points of the function
* @returns {Array} Array of objects following this format: {freq, amp, phase}
*/
function dft(f) { // Calculate the DFT of the given function f (f is a array of )
const F = []; //The DFF of the given function f
const N = f.length; //Number of discrete coordinates
let re, im, freq, amp, phase; //init variables
for (let k = 0; k < N; k++) { // For each point
re = im = 0; // Reset them
for (let n = 0; n < N; n++) { // For each point
const phi = (2 * Math.PI * k * n) / N;
re += f[n] * cos(phi);
im -= f[n] * sin(phi);
}
re = re / N;
im = im / N;
freq = k;
amp = sqrt(re * re + im * im);
phase = atan2(im, re);
F[k] = {freq, amp, phase};
}
return F;
}
/**
* Draw the epiCycles to visualize the DFT
* @param {number} x - Start horizontal coordinate
* @param {number} y - Start vertical coordinate
* @param {number} rotation - The angle to rotate/The starting angle (use to make the vertical and horizontal circles with the same function)
* @param {Array} fourier - Array from the function dft
* @returns {object} Returns the vector for the next epiCycle
*/
function epiCycles(x, y, rotation, fourier) {
for (let i = 0; i < fourier.length; i++) {
let prevx = x, prevy = y; // Center of the current circle
let freq = fourier[i].freq;
let radius = fourier[i].amp;
let phase = fourier[i].phase;
let phi = freq * time + phase + rotation;
x += radius * cos(phi);
y += radius * sin(phi);
stroke(255, 100); // Draw the circles with a bit of transparency
noFill(); // Draw them empty
ellipse(prevx, prevy, radius * 2); // Circle
stroke(255);
line(prevx, prevy, x, y); // Draw radius
}
return createVector(x, y); // Return the end of the last radius
}
/**
* Setups all the code to generate the new figure
* @param {number} nPoints - The desired amoint of points to use to send to the function dft(f)
*/
function updateFourier(nPoints = 600){
x = []; // x coordinates of the ideal figure
y = []; // y coordinates of the ideal figure
time = 0;
path = []; //clear previous path
drew = false;
let skip = parseInt(drawing.length / nPoints);
skip = (skip > 0)? skip : 1;
for (let i = 0; i < drawing.length; i += skip) {
x.push(drawing[i].x);
y.push(drawing[i].y);
}
fourierX = dft(x); // Do the DFT of the function x(x)
fourierY = dft(y); // Do the DFT of the function y(x)
fourierX.sort((a, b) => b.amp - a.amp); //sort the waves by their amp
fourierY.sort((a, b) => b.amp - a.amp);
}
/**
* When a file is selected, this code evaluates the content
* @param {file} file
* @throws error if not valid svg
*/
function appendFile(file){
// print(file.name);
try {
if (file.type != "image"){
throw "The file must be a svg image";
}
if (file.subtype != "svg+xml" || !RegExp("\.+\\.svg$").test(file.name)){
throw "The file must be a .svg image, not just an image";
}
} catch (error) {
console.warn("Error");
console.warn(error);
return; //end execution of this function
}
// if here, the file should be correct
loadStrings(file.data, function(fileStringArr){
let result = svgToPoints(fileStringArr.join(""), 600, 500);
drawing = result.points;
updateFourier();
});
}
/**
* Given a string with the svg content, returns a array of points.
* @param {string[]} fileText - String with the svg code
* @param {number} nPointsPath - Number of points to generate. Default: 600
* @param {number} sizeX - Optional parameter to set the desired width. If NaN, this is ignored.
* @param {number} sizeY - Optional parameter to set the desired height. If NaN, this is ignored.
* @returns {object[]} Array with the points of the SVG ({x: float, y: float})
*/
function svgToPoints(fileText, nPointsPath = 600, sizeX, sizeY){
var wMax = 0, wMin = Infinity, hMax = 0, hMin = Infinity; //To calculate the properties of the SVG
let doc = new DOMParser().parseFromString(fileText, "text/xml"); //svg as a xml
let points = [];
let paths = doc.getElementsByTagName("path"); // Array of paths on the svg
for (let pi = 0; pi < paths.length; pi++){
let path = paths[pi]; //get the pº path
//Get the points of the path
for ( var i = 0; i < nPointsPath; i++ ){
let p = path.getPointAtLength(i / nPointsPath * path.getTotalLength());
// Update the properties based on this point
if (wMax < p.x){
wMax = p.x;
}
else if (wMin > p.x){
wMin = p.x;
}
if (hMax < p.y){
hMax = p.y;
}
else if (hMin > p.y){
hMin = p.y;
}
points.push(p); //add the point
}
}
points.push(points[0]);
let w = wMax - wMin; //This value is the width of the SVG
let h = hMax - hMin; //This value is the height of the SVG
// Convert the coordinates origin to the center of the SVG
for (let i = 0; i < points.length; i++){
points[i] = {
x: (points[i].x - wMin) - w * 0.5,
y: (points[i].y - hMin) - h * 0.5
}
}
if (sizeX || sizeY){ //If the svg must fit on some dimensions, this code will scale it
let wR = (sizeX)? sizeX / w : sizeY / h; // Ratio
let hR = (sizeY)? sizeY / h : sizeX / w; // Ratio
for (let i = 0; i < points.length; i++){
points[i] = {
x: points[i].x * wR,
y: points[i].y * hR
}
}
w *= wR; //The size of the svg has changed
h *= hR; //The size of the svg has changed
}
let r = {
p: { // Center: {x: 0, y: 0}
width: w,
height: h,
},
points: points //Here are the points
};
return r;
}
// VARIABLES:
var result;
let x = []; // x coordinates of the ideal figure
let y = []; // y coordinates of the ideal figure
let fourierX; // Xn.x coordinates of the fourier figure
let fourierY; // Xn.y coordinates of the fourier figure
let time = 0;
let path = []; // Array of coordinates of points to represent
let vx, vy, v; // (in order) endCoord of the last radius in each function and a vector of the corresponding point
let drew = false; // When all figure represented, this becomes true => the path is showed but no more points are added
// UI:
var fSelector, fileInput;
// CODE:
function setup() {
createCanvas(900, 800);
// fSelector = createSelect();
// fSelector.position(20, 20);
// fSelector.option("Train");
// fSelector.changed(updateFourier); //When changed the element selected, update
fileInput = createFileInput(appendFile, false); //When file selected, execute appendFile (only 1 file per selection)
fileInput.position(20, 20);
// Fourier code:
updateFourier();
}
function draw() {
background(0);
vx = epiCycles(width / 2 + 100, 100, 0, fourierX);
vy = epiCycles(100, height / 2 + 100, HALF_PI, fourierY);
v = createVector(vx.x, vy.y);
if(!drew){
path.unshift(v);
}
line(vx.x, vx.y, v.x, v.y);
line(vy.x, vy.y, v.x, v.y);
beginShape();
noFill();
for (let i = 0; i < path.length; i++) {
vertex(path[i].x, path[i].y);
}
endShape();
const dt = TWO_PI / fourierY.length;
time += dt;
if (time > TWO_PI) {
time = 0;
// path = [];
drew = true;
}
// push();
// strokeWeight(1);
// for (let i = 0; i < drawing.length; i++){
// point(width / 2 + 100 + drawing[i].x, height / 2 + 100 + drawing[i].y);
// }
// pop();
// if (wave.length > 250) {
// wave.pop();
// }
}