Bilanced simulation to make knots not untangle by themself
parent
56096644a0
commit
7259b590ba
@ -1,137 +1,146 @@
|
|||||||
export const Vec2 = {
|
export const Vec2 = {
|
||||||
Mutate: {
|
Mutate: {
|
||||||
add(target, [x, y]) {
|
add(target, [x, y]) {
|
||||||
target[0] += x
|
target[0] += x;
|
||||||
target[1] += y
|
target[1] += y;
|
||||||
|
|
||||||
return target
|
return target;
|
||||||
},
|
},
|
||||||
scale(target, factor) {
|
scale(target, factor) {
|
||||||
target[0] *= factor
|
target[0] *= factor;
|
||||||
target[1] *= factor
|
target[1] *= factor;
|
||||||
|
|
||||||
return target
|
return target;
|
||||||
},
|
},
|
||||||
normalize(target) {
|
normalize(target) {
|
||||||
const norm = Vec2.norm2([x, y])
|
const norm = Vec2.norm2([x, y]);
|
||||||
target[0] /= norm
|
target[0] /= norm;
|
||||||
target[1] /= norm
|
target[1] /= norm;
|
||||||
|
|
||||||
return target
|
return target;
|
||||||
},
|
},
|
||||||
set(target, [x, y]) {
|
set(target, [x, y]) {
|
||||||
target[0] = x
|
target[0] = x;
|
||||||
target[1] = y
|
target[1] = y;
|
||||||
return target
|
return target;
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
perpendicular([x, y]) {
|
perpendicular([x, y]) {
|
||||||
return [-y, x]
|
return [-y, x];
|
||||||
},
|
},
|
||||||
distance2(v1, v2) {
|
distance2(v1, v2) {
|
||||||
return Vec2.norm2(Vec2.sub(v1, v2))
|
return Vec2.norm2(Vec2.sub(v1, v2));
|
||||||
},
|
},
|
||||||
add([x1, y1], [x2, y2]) {
|
add([x1, y1], [x2, y2]) {
|
||||||
return [x1 + x2, y1 + y2]
|
return [x1 + x2, y1 + y2];
|
||||||
},
|
},
|
||||||
sub([x1, y1], [x2, y2]) {
|
sub([x1, y1], [x2, y2]) {
|
||||||
return [x1 - x2, y1 - y2]
|
return [x1 - x2, y1 - y2];
|
||||||
},
|
},
|
||||||
dot([x1, y1], [x2, y2]) {
|
dot([x1, y1], [x2, y2]) {
|
||||||
return x1 * x2 + y1 * y2
|
return x1 * x2 + y1 * y2;
|
||||||
},
|
},
|
||||||
scale([x, y], factor) {
|
scale([x, y], factor) {
|
||||||
return [x * factor, y * factor]
|
return [x * factor, y * factor];
|
||||||
},
|
},
|
||||||
norm2([x, y]) {
|
norm2([x, y]) {
|
||||||
return Math.sqrt(x ** 2 + y ** 2)
|
return Math.sqrt(x ** 2 + y ** 2);
|
||||||
},
|
},
|
||||||
normalize([x, y]) {
|
normalize([x, y]) {
|
||||||
const norm = Vec2.norm2([x, y])
|
const norm = Vec2.norm2([x, y]);
|
||||||
return [x / norm, y / norm]
|
return [x / norm, y / norm];
|
||||||
},
|
},
|
||||||
|
|
||||||
findNearest(curve, pt) {
|
findNearest(curve, pt) {
|
||||||
let index = -1
|
let index = -1;
|
||||||
let minDist = +Infinity
|
let minDist = +Infinity;
|
||||||
|
|
||||||
for (let i = 0; i < curve.length; i++) {
|
for (let i = 0; i < curve.length; i++) {
|
||||||
const d = Vec2.distance2(curve[i], pt)
|
const d = Vec2.distance2(curve[i], pt);
|
||||||
|
|
||||||
if (d < minDist) {
|
if (d < minDist) {
|
||||||
index = i
|
index = i;
|
||||||
minDist = d
|
minDist = d;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return [index, minDist]
|
return [index, minDist];
|
||||||
},
|
},
|
||||||
}
|
};
|
||||||
|
|
||||||
export const Vec3 = {
|
export const Vec3 = {
|
||||||
Mutate: {
|
Mutate: {
|
||||||
add(target, [x, y, z]) {
|
add(target, [x, y, z]) {
|
||||||
target[0] += x
|
target[0] += x;
|
||||||
target[1] += y
|
target[1] += y;
|
||||||
target[2] += z
|
target[2] += z;
|
||||||
|
|
||||||
return target
|
return target;
|
||||||
},
|
},
|
||||||
scale(target, factor) {
|
scale(target, factor) {
|
||||||
target[0] *= factor
|
target[0] *= factor;
|
||||||
target[1] *= factor
|
target[1] *= factor;
|
||||||
target[2] *= factor
|
target[2] *= factor;
|
||||||
|
|
||||||
return target
|
return target;
|
||||||
},
|
},
|
||||||
normalize(target) {
|
normalize(target) {
|
||||||
const norm = Vec3.norm2(target)
|
const norm = Vec3.norm2(target);
|
||||||
target[0] /= norm
|
target[0] /= norm;
|
||||||
target[1] /= norm
|
target[1] /= norm;
|
||||||
target[2] /= norm
|
target[2] /= norm;
|
||||||
|
|
||||||
return target
|
return target;
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
withMaxLength(v, max) {
|
||||||
|
const d = Vec3.norm2(v);
|
||||||
|
|
||||||
|
if (d > max) {
|
||||||
|
return Vec3.scale(v, max / d);
|
||||||
|
}
|
||||||
|
|
||||||
|
return v;
|
||||||
|
},
|
||||||
distance2(v1, v2) {
|
distance2(v1, v2) {
|
||||||
return Vec3.norm2(Vec3.sub(v1, v2))
|
return Vec3.norm2(Vec3.sub(v1, v2));
|
||||||
},
|
},
|
||||||
add([x1, y1, z1], [x2, y2, z2]) {
|
add([x1, y1, z1], [x2, y2, z2]) {
|
||||||
return [x1 + x2, y1 + y2, z1 + z2]
|
return [x1 + x2, y1 + y2, z1 + z2];
|
||||||
},
|
},
|
||||||
sub([x1, y1, z1], [x2, y2, z2]) {
|
sub([x1, y1, z1], [x2, y2, z2]) {
|
||||||
return [x1 - x2, y1 - y2, z1 - z2]
|
return [x1 - x2, y1 - y2, z1 - z2];
|
||||||
},
|
},
|
||||||
dot([x1, y1, z1], [x2, y2, z2]) {
|
dot([x1, y1, z1], [x2, y2, z2]) {
|
||||||
return x1 * x2 + y1 * y2 + z1 * z2
|
return x1 * x2 + y1 * y2 + z1 * z2;
|
||||||
},
|
},
|
||||||
scale([x, y, z], factor) {
|
scale([x, y, z], factor) {
|
||||||
return [x * factor, y * factor, z * factor]
|
return [x * factor, y * factor, z * factor];
|
||||||
},
|
},
|
||||||
norm2([x, y, z]) {
|
norm2([x, y, z]) {
|
||||||
return Math.sqrt(x ** 2 + y ** 2 + z ** 2)
|
return Math.sqrt(x ** 2 + y ** 2 + z ** 2);
|
||||||
},
|
},
|
||||||
normalize([x, y, z]) {
|
normalize([x, y, z]) {
|
||||||
const norm = Vec3.norm2([x, y, z])
|
const norm = Vec3.norm2([x, y, z]);
|
||||||
return [x / norm, y / norm, z / norm]
|
return [x / norm, y / norm, z / norm];
|
||||||
},
|
},
|
||||||
findNearest(curve, pt) {
|
findNearest(curve, pt) {
|
||||||
let index = -1
|
let index = -1;
|
||||||
let minDist = +Infinity
|
let minDist = +Infinity;
|
||||||
|
|
||||||
for (let i = 0; i < curve.length; i++) {
|
for (let i = 0; i < curve.length; i++) {
|
||||||
const d = Vec3.distance2(curve[i], pt)
|
const d = Vec3.distance2(curve[i], pt);
|
||||||
|
|
||||||
if (d < minDist) {
|
if (d < minDist) {
|
||||||
index = i
|
index = i;
|
||||||
minDist = d
|
minDist = d;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return [index, minDist]
|
return [index, minDist];
|
||||||
},
|
},
|
||||||
}
|
};
|
||||||
|
|
||||||
export function clamp(min, value, max) {
|
export function clamp(min, value, max) {
|
||||||
return Math.min(max, Math.max(min, value))
|
return Math.min(max, Math.max(min, value));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue