How to use the @mathigon/fermat.nearlyEquals function in @mathigon/fermat

To help you get started, we’ve selected a few @mathigon/fermat examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mathigon / textbooks / content / graph-theory / components / graph.ts View on Github external
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;
github mathigon / textbooks / content / graph-theory / components / graph.js View on Github external
_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);
      });
github mathigon / textbooks / content / euclidean-geometry / functions.ts View on Github external
$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)) {
github mathigon / textbooks / content / euclidean-geometry / functions.ts View on Github external
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();
  });
github mathigon / textbooks / content / quadratics / functions.ts View on Github external
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];
}
github mathigon / textbooks / content / linear-functions / components / numberline.js View on Github external
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));
  }
}
github mathigon / textbooks / quadratics / functions.js View on Github external
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)}`
}