Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
physics() {
let positions: SimplePoint[] = [];
let totalMoved = 0;
for (const [i, v] of this.vertices.entries()) {
if (this.options.static || v === this.dragging) continue;
let force = {x: 0, y: 0};
for (const u of this.vertices) {
if (u === v) continue;
// Coulomb's Repulsion between Vertices
let d = (v.posn.x - u.posn.x) ** 2 + (v.posn.y - u.posn.y) ** 2;
if (nearlyEquals(d, 0, 0.001)) d = 0.001;
let coul = this.repulsion / d;
force.x += coul * (v.posn.x - u.posn.x);
force.y += coul * (v.posn.y - u.posn.y);
}
for (const u of v.neighbours) {
// Hook's attraction between Neighbours
force.x += this.attraction * (u.posn.x - v.posn.x);
force.y += this.attraction * (u.posn.y - v.posn.y);
}
// Additional Force towards center of svg
force.x += this.gravity * (0.5 - v.posn.x / this.width);
force.y += this.gravity * (0.5 - v.posn.y / this.height);
v.v.x = (v.v.x + force.x) * this.damping;
_this.vertices.forEach(function(u) {
if (u === v) return;
// Coulomb's Repulsion between Vertices
let d = square(v.posn.x - u.posn.x) + square(v.posn.y - u.posn.y);
if (nearlyEquals(d, 0, 0.001)) d = 0.001;
let coul = _this.repulsion / d;
force.x += coul * (v.posn.x - u.posn.x);
force.y += coul * (v.posn.y - u.posn.y);
});
$geopad.on('add:path', (path: GeoPath) => {
if (!path.val) return;
if (isLineLike(path.val)) {
if (!segment0) {
segment0 = path as any as GeoElement;
path.$el.setAttr('target', 'a b');
$geopad.setActiveTool('circle');
$geopad.animatePoint(path.points[0].name, new Point(60, 260));
$geopad.animatePoint(path.points[1].name, new Point(260, 260));
return $step.score('segment0');
} else if (nearlyEquals(segment0.val!.length, path.val.length)) {
if (path.val.p1.equals(segment0.val!.p1) ||
path.val.p2.equals(segment0.val!.p1)) {
path.$el.setAttr('target', 'a');
return $step.score('segment1');
} else if (path.val.p1.equals(segment0.val!.p2) ||
path.val.p2.equals(segment0.val!.p2)) {
path.$el.setAttr('target', 'b');
return $step.score('segment2');
}
}
} else if (segment0 && isCircle(path.val)) {
if (nearlyEquals(segment0.val!.length, path.val.r)) {
if (path.val.c.equals(segment0.val!.p1)) {
return $step.score('circle1');
} else if (path.val.c.equals(segment0.val!.p2)) {
return $step.score('segment0');
} else if (nearlyEquals(segment0.val!.length, path.val.length)) {
if (path.val.p1.equals(segment0.val!.p1) ||
path.val.p2.equals(segment0.val!.p1)) {
path.$el.setAttr('target', 'a');
return $step.score('segment1');
} else if (path.val.p1.equals(segment0.val!.p2) ||
path.val.p2.equals(segment0.val!.p2)) {
path.$el.setAttr('target', 'b');
return $step.score('segment2');
}
}
} else if (segment0 && isCircle(path.val)) {
if (nearlyEquals(segment0.val!.length, path.val.r)) {
if (path.val.c.equals(segment0.val!.p1)) {
return $step.score('circle1');
} else if (path.val.c.equals(segment0.val!.p2)) {
return $step.score('circle2');
}
}
}
$step.addHint('incorrect');
path.remove();
});
function zeros(a: number, b: number, c: number) {
let disc = b * b - 4 * a * c;
if (disc < 0) return [];
if (nearlyEquals(disc, 0, 0.1)) return [-b / (2 * a)];
let x1 = (-b + Math.sqrt(disc)) / (2 * a);
let x2 = (-b - Math.sqrt(disc)) / (2 * a);
return [x1, x2];
}
function makeTickmarks(min, max, major, minor, generate) {
for (let i = min; i <= max; i += minor) {
i = round(i, 3);
generate(i, nearlyEquals(i % major, 0));
}
}
function zeros(a,b,c) {
let disc = b * b - 4 * a * c;
if (disc < 0) return '';
if (nearlyEquals(disc, 0, 0.1)) {
let x = -b/(2*a);
return `${x},${q(x,a,b,c)}`
}
let x1 = (-b + Math.sqrt(disc))/(2*a);
let x2 = (-b - Math.sqrt(disc))/(2*a);
return `${x1},${q(x1,a,b,c)}|${x2},${q(x2,a,b,c)}`
}