-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
95 lines (86 loc) · 2.58 KB
/
utils.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
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
/**
* Randomly sorts provided array **in-place**
*
* https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm
* https://bost.ocks.org/mike/shuffle/
*
* @param array {T[]}
* @returns {T[]}
*/
const shuffle = array => {
// for i from n−1 down to 1 do:
for (let j, i = array.length - 1; i > 0; i--) {
// j := random integer such that 0 <= j <= i (inclusive)
j = randomInt(i + 1);
// exchange a[j] and a[i]
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
/**
* Returns random int up to n.
* Constraint: 0 < n < 2^32.
*
* @param n {number}
* @returns {number} random int from the interval [0; n-1]
*/
const randomInt = n => (n * Math.random()) >>> 0;
/**
* Produce CSV-formatted string from array
* Or tab-separated TSV:
* const tsv = a => csv(a, '\t');
*
* @param array {T[][]}
* @param separator {string} comma by default
* @returns {string}
*/
const csv = (array, separator=',') => array.reduce(
(csv, row) => `${csv}${row.join(separator)}\n`, ''
);
/**
* Converts array of Curves to array of polygon coordinates [x, y] represented by them.
*
* Accepts LineCurve, CubicBezierCurve and QuadraticBezierCurve.
* Bézier curves are treated as lines like all control points are collinear.
* Other Curve types (ArcCurve, EllipseCurve, SplineCurve) are ignored.
*
* Usage:
* const SvgResult = (new SVGLoader()).parse('<svg>...</svg>');
* SvgResult.paths.forEach(path => path.subPaths.forEach(subpath => {
* const coords = LineCurvesToPolygon(subpath.curves);
* console.log(csv(coords,'\t\t'));
* }));
*
* @param curves {(LineCurve|CubicBezierCurve|QuadraticBezierCurve)[]}
* @returns {number[][]}
*/
const LineCurvesToPolygon = (curves) => curves.reduce((acc, curve) => {
let lineFrom, lineTo;
switch (curve.type) {
case 'LineCurve':
lineFrom = curve.v1.toArray();
lineTo = curve.v2.toArray();
break;
case 'CubicBezierCurve':
lineFrom = curve.v0.toArray();
lineTo = curve.v3.toArray();
break;
case 'QuadraticBezierCurve':
lineFrom = curve.v0.toArray();
lineTo = curve.v2.toArray();
break;
default:
return acc;
}
if (acc.length) {
const lastPoint = acc[acc.length-1];
if (lineFrom[0] !== lastPoint[0] || lineFrom[1] !== lastPoint[1]) {
acc.push(lineFrom);
}
} else {
acc.push(lineFrom);
}
acc.push(lineTo);
return acc;
}, []);
export {csv, randomInt, shuffle, LineCurvesToPolygon};