mirror of
https://github.com/Pomax/BezierInfo-2.git
synced 2025-09-30 10:09:02 +02:00
news section!
This commit is contained in:
@@ -23,8 +23,7 @@ const ZERO = { x: 0, y: 0, z: 0 };
|
||||
*/
|
||||
class Bezier {
|
||||
constructor(coords) {
|
||||
let args =
|
||||
coords && coords.forEach ? coords : Array.from(arguments).slice();
|
||||
let args = coords && coords.forEach ? coords : Array.from(arguments).slice();
|
||||
let coordlen = false;
|
||||
|
||||
if (typeof args[0] === "object") {
|
||||
@@ -46,25 +45,19 @@ class Bezier {
|
||||
if (coordlen) {
|
||||
if (coordlen > 4) {
|
||||
if (arguments.length !== 1) {
|
||||
throw new Error(
|
||||
"Only new Bezier(point[]) is accepted for 4th and higher order curves"
|
||||
);
|
||||
throw new Error("Only new Bezier(point[]) is accepted for 4th and higher order curves");
|
||||
}
|
||||
higher = true;
|
||||
}
|
||||
} else {
|
||||
if (len !== 6 && len !== 8 && len !== 9 && len !== 12) {
|
||||
if (arguments.length !== 1) {
|
||||
throw new Error(
|
||||
"Only new Bezier(point[]) is accepted for 4th and higher order curves"
|
||||
);
|
||||
throw new Error("Only new Bezier(point[]) is accepted for 4th and higher order curves");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const _3d = (this._3d =
|
||||
(!higher && (len === 9 || len === 12)) ||
|
||||
(coords && coords[0] && typeof coords[0].z !== "undefined"));
|
||||
const _3d = (this._3d = (!higher && (len === 9 || len === 12)) || (coords && coords[0] && typeof coords[0].z !== "undefined"));
|
||||
|
||||
const points = (this.points = []);
|
||||
for (let idx = 0, step = _3d ? 3 : 2; idx < len; idx += step) {
|
||||
@@ -390,17 +383,7 @@ class Bezier {
|
||||
c.y /= m;
|
||||
c.z /= m;
|
||||
// rotation matrix
|
||||
const R = [
|
||||
c.x * c.x,
|
||||
c.x * c.y - c.z,
|
||||
c.x * c.z + c.y,
|
||||
c.x * c.y + c.z,
|
||||
c.y * c.y,
|
||||
c.y * c.z - c.x,
|
||||
c.x * c.z - c.y,
|
||||
c.y * c.z + c.x,
|
||||
c.z * c.z,
|
||||
];
|
||||
const R = [c.x * c.x, c.x * c.y - c.z, c.x * c.z + c.y, c.x * c.y + c.z, c.y * c.y, c.y * c.z - c.x, c.x * c.z - c.y, c.y * c.z + c.x, c.z * c.z];
|
||||
// normal vector:
|
||||
const n = {
|
||||
x: R[0] * r1.x + R[1] * r1.y + R[2] * r1.z,
|
||||
@@ -446,14 +429,8 @@ class Bezier {
|
||||
// no shortcut: use "de Casteljau" iteration.
|
||||
const q = this.hull(t1);
|
||||
const result = {
|
||||
left:
|
||||
this.order === 2
|
||||
? new Bezier([q[0], q[3], q[5]])
|
||||
: new Bezier([q[0], q[4], q[7], q[9]]),
|
||||
right:
|
||||
this.order === 2
|
||||
? new Bezier([q[5], q[4], q[2]])
|
||||
: new Bezier([q[9], q[8], q[6], q[3]]),
|
||||
left: this.order === 2 ? new Bezier([q[0], q[3], q[5]]) : new Bezier([q[0], q[4], q[7], q[9]]),
|
||||
right: this.order === 2 ? new Bezier([q[5], q[4], q[2]]) : new Bezier([q[9], q[8], q[6], q[3]]),
|
||||
span: q,
|
||||
};
|
||||
|
||||
@@ -722,12 +699,8 @@ class Bezier {
|
||||
reduced.forEach(function (segment) {
|
||||
slen = segment.length();
|
||||
if (graduated) {
|
||||
fcurves.push(
|
||||
segment.scale(linearDistanceFunction(d1, d3, tlen, alen, slen))
|
||||
);
|
||||
bcurves.push(
|
||||
segment.scale(linearDistanceFunction(-d2, -d4, tlen, alen, slen))
|
||||
);
|
||||
fcurves.push(segment.scale(linearDistanceFunction(d1, d3, tlen, alen, slen)));
|
||||
bcurves.push(segment.scale(linearDistanceFunction(-d2, -d4, tlen, alen, slen)));
|
||||
} else {
|
||||
fcurves.push(segment.scale(d1));
|
||||
bcurves.push(segment.scale(-d2));
|
||||
@@ -766,11 +739,7 @@ class Bezier {
|
||||
const outline = this.outline(d1, d2).curves;
|
||||
const shapes = [];
|
||||
for (let i = 1, len = outline.length; i < len / 2; i++) {
|
||||
const shape = utils.makeshape(
|
||||
outline[i],
|
||||
outline[len - i],
|
||||
curveIntersectionThreshold
|
||||
);
|
||||
const shape = utils.makeshape(outline[i], outline[len - i], curveIntersectionThreshold);
|
||||
shape.startcap.virtual = i > 1;
|
||||
shape.endcap.virtual = i < len / 2 - 1;
|
||||
shapes.push(shape);
|
||||
@@ -786,11 +755,7 @@ class Bezier {
|
||||
if (curve instanceof Bezier) {
|
||||
curve = curve.reduce();
|
||||
}
|
||||
return this.curveintersects(
|
||||
this.reduce(),
|
||||
curve,
|
||||
curveIntersectionThreshold
|
||||
);
|
||||
return this.curveintersects(this.reduce(), curve, curveIntersectionThreshold);
|
||||
}
|
||||
|
||||
lineIntersects(line) {
|
||||
@@ -835,11 +800,7 @@ class Bezier {
|
||||
// step 2: for each pairing, run through the convergence algorithm.
|
||||
let intersections = [];
|
||||
pairs.forEach(function (pair) {
|
||||
const result = utils.pairiteration(
|
||||
pair.left,
|
||||
pair.right,
|
||||
curveIntersectionThreshold
|
||||
);
|
||||
const result = utils.pairiteration(pair.left, pair.right, curveIntersectionThreshold);
|
||||
if (result.length > 0) {
|
||||
intersections = intersections.concat(result);
|
||||
}
|
||||
|
@@ -172,20 +172,7 @@ export default function normalizePath(d) {
|
||||
x = args[a + 4];
|
||||
y = args[a + 5];
|
||||
}
|
||||
normalized +=
|
||||
"C " +
|
||||
cx +
|
||||
" " +
|
||||
cy +
|
||||
" " +
|
||||
cx2 +
|
||||
" " +
|
||||
cy2 +
|
||||
" " +
|
||||
x +
|
||||
" " +
|
||||
y +
|
||||
" ";
|
||||
normalized += "C " + cx + " " + cy + " " + cx2 + " " + cy2 + " " + x + " " + y + " ";
|
||||
}
|
||||
} else if (lop === "s") {
|
||||
for (a = 0; a < alen; a += 4) {
|
||||
@@ -204,20 +191,7 @@ export default function normalizePath(d) {
|
||||
x = args[a + 2];
|
||||
y = args[a + 3];
|
||||
}
|
||||
normalized +=
|
||||
"C " +
|
||||
cx +
|
||||
" " +
|
||||
cy +
|
||||
" " +
|
||||
cx2 +
|
||||
" " +
|
||||
cy2 +
|
||||
" " +
|
||||
x +
|
||||
" " +
|
||||
y +
|
||||
" ";
|
||||
normalized += "C " + cx + " " + cy + " " + cx2 + " " + cy2 + " " + x + " " + y + " ";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -239,22 +213,7 @@ export default function normalizePath(d) {
|
||||
x = args[a + 5];
|
||||
y = args[a + 6];
|
||||
}
|
||||
normalized +=
|
||||
"A " +
|
||||
rx +
|
||||
" " +
|
||||
ry +
|
||||
" " +
|
||||
xrot +
|
||||
" " +
|
||||
lflag +
|
||||
" " +
|
||||
sweep +
|
||||
" " +
|
||||
x +
|
||||
" " +
|
||||
y +
|
||||
" ";
|
||||
normalized += "A " + rx + " " + ry + " " + xrot + " " + lflag + " " + sweep + " " + x + " " + y + " ";
|
||||
}
|
||||
} else if (lop === "z") {
|
||||
normalized += "Z ";
|
||||
|
@@ -222,9 +222,7 @@ const utils = {
|
||||
return {
|
||||
x: (f1 * p[0].x + f2 * p[1].x + f3 * p[2].x + f4 * p[3].x) / d,
|
||||
y: (f1 * p[0].y + f2 * p[1].y + f3 * p[2].y + f4 * p[3].y) / d,
|
||||
z: !_3d
|
||||
? false
|
||||
: (f1 * p[0].z + f2 * p[1].z + f3 * p[2].z + f4 * p[3].z) / d,
|
||||
z: !_3d ? false : (f1 * p[0].z + f2 * p[1].z + f3 * p[2].z + f4 * p[3].z) / d,
|
||||
t: t,
|
||||
};
|
||||
}
|
||||
@@ -251,11 +249,7 @@ const utils = {
|
||||
},
|
||||
|
||||
between: function (v, m, M) {
|
||||
return (
|
||||
(m <= v && v <= M) ||
|
||||
utils.approximately(v, m) ||
|
||||
utils.approximately(v, M)
|
||||
);
|
||||
return (m <= v && v <= M) || utils.approximately(v, m) || utils.approximately(v, M);
|
||||
},
|
||||
|
||||
approximately: function (a, b, precision) {
|
||||
@@ -378,8 +372,7 @@ const utils = {
|
||||
},
|
||||
|
||||
lli8: function (x1, y1, x2, y2, x3, y3, x4, y4) {
|
||||
const nx =
|
||||
(x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4),
|
||||
const nx = (x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4),
|
||||
ny = (x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4),
|
||||
d = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
|
||||
if (d == 0) {
|
||||
@@ -411,16 +404,7 @@ const utils = {
|
||||
y2 = p2.y,
|
||||
dx = (x2 - x1) / 3,
|
||||
dy = (y2 - y1) / 3;
|
||||
return new Bezier(
|
||||
x1,
|
||||
y1,
|
||||
x1 + dx,
|
||||
y1 + dy,
|
||||
x1 + 2 * dx,
|
||||
y1 + 2 * dy,
|
||||
x2,
|
||||
y2
|
||||
);
|
||||
return new Bezier(x1, y1, x1 + dx, y1 + dy, x1 + 2 * dx, y1 + 2 * dy, x2, y2);
|
||||
},
|
||||
|
||||
findbbox: function (sections) {
|
||||
@@ -441,13 +425,7 @@ const utils = {
|
||||
};
|
||||
},
|
||||
|
||||
shapeintersections: function (
|
||||
s1,
|
||||
bbox1,
|
||||
s2,
|
||||
bbox2,
|
||||
curveIntersectionThreshold
|
||||
) {
|
||||
shapeintersections: function (s1, bbox1, s2, bbox2, curveIntersectionThreshold) {
|
||||
if (!utils.bboxoverlap(bbox1, bbox2)) return [];
|
||||
const intersections = [];
|
||||
const a1 = [s1.startcap, s1.forward, s1.back, s1.endcap];
|
||||
@@ -482,13 +460,7 @@ const utils = {
|
||||
bbox: utils.findbbox([start, forward, back, end]),
|
||||
};
|
||||
shape.intersections = function (s2) {
|
||||
return utils.shapeintersections(
|
||||
shape,
|
||||
shape.bbox,
|
||||
s2,
|
||||
s2.bbox,
|
||||
curveIntersectionThreshold
|
||||
);
|
||||
return utils.shapeintersections(shape, shape.bbox, s2, s2.bbox, curveIntersectionThreshold);
|
||||
};
|
||||
return shape;
|
||||
},
|
||||
@@ -685,11 +657,7 @@ const utils = {
|
||||
const qdsum = d.x * d.x + d.y * d.y;
|
||||
|
||||
if (_3d) {
|
||||
num = sqrt(
|
||||
pow(d.y * dd.z - dd.y * d.z, 2) +
|
||||
pow(d.z * dd.x - dd.z * d.x, 2) +
|
||||
pow(d.x * dd.y - dd.x * d.y, 2)
|
||||
);
|
||||
num = sqrt(pow(d.y * dd.z - dd.y * d.z, 2) + pow(d.z * dd.x - dd.z * d.x, 2) + pow(d.x * dd.y - dd.x * d.y, 2));
|
||||
dnm = pow(qdsum + d.z * d.z, 3 / 2);
|
||||
} else {
|
||||
num = d.x * dd.y - d.y * dd.x;
|
||||
@@ -803,15 +771,8 @@ const utils = {
|
||||
r = 100000,
|
||||
threshold = curveIntersectionThreshold || 0.5;
|
||||
|
||||
if (
|
||||
c1b.x.size + c1b.y.size < threshold &&
|
||||
c2b.x.size + c2b.y.size < threshold
|
||||
) {
|
||||
return [
|
||||
(((r * (c1._t1 + c1._t2)) / 2) | 0) / r +
|
||||
"/" +
|
||||
(((r * (c2._t1 + c2._t2)) / 2) | 0) / r,
|
||||
];
|
||||
if (c1b.x.size + c1b.y.size < threshold && c2b.x.size + c2b.y.size < threshold) {
|
||||
return [(((r * (c1._t1 + c1._t2)) / 2) | 0) / r + "/" + (((r * (c2._t1 + c2._t2)) / 2) | 0) / r];
|
||||
}
|
||||
|
||||
let cc1 = c1.split(0.5),
|
||||
@@ -832,9 +793,7 @@ const utils = {
|
||||
if (pairs.length === 0) return results;
|
||||
|
||||
pairs.forEach(function (pair) {
|
||||
results = results.concat(
|
||||
utils.pairiteration(pair.left, pair.right, threshold)
|
||||
);
|
||||
results = results.concat(utils.pairiteration(pair.left, pair.right, threshold));
|
||||
});
|
||||
|
||||
results = results.filter(function (v, i) {
|
||||
|
Reference in New Issue
Block a user