How to use the @mathigon/fermat.Vector 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
v.posn.y = clamp(v.posn.y, distance, this.height - distance);
      }

      if (this.options.icon) {
        v.$el.translate(v.posn.x, v.posn.y);
      } else {
        v.$el.setCenter(v.posn);
      }
    }

    for (const e of this.edges) {
      if (e.vertices[0] === e.vertices[1]) {
        // Connected to self
        if (!center) center = Point.average(...this.vertices.map(v => v.posn));

        const v = new Vector(e.vertices[0].posn.x - center.x, e.vertices[0].posn.y - center.y).unitVector;
        const v0 = new Vector(v[0] + v[1], v[1] - v[0]).scale(40);
        const v1 = new Vector(v[0] - v[1], v[1] + v[0]).scale(40);

        e.$el.setAttr('d', `M${e.vertices[0].posn.x},${e.vertices[0].posn.y}c${v0[0]},${v0[1]},${v1[0]},${v1[1]},0,0`);

      } else if (this.options.arc) {
        // Arcs
        const dx = e.vertices[1].posn.x - e.vertices[0].posn.x;
        const dy = e.vertices[1].posn.y - e.vertices[0].posn.y;
        const dr = Math.sqrt(dx * dx + dy * dy);
        e.$el.setAttr('d', `M${e.vertices[0].posn.x},${e.vertices[0].posn.y}A${dr},${dr},0,0,1,${e.vertices[1].posn.x},${e.vertices[1].posn.y}`);

      } else {
        e.$el.setLine(e.vertices[0].posn, e.vertices[1].posn);
      }
    }
github mathigon / textbooks / content / graph-theory / components / graph.ts View on Github external
}

      if (this.options.icon) {
        v.$el.translate(v.posn.x, v.posn.y);
      } else {
        v.$el.setCenter(v.posn);
      }
    }

    for (const e of this.edges) {
      if (e.vertices[0] === e.vertices[1]) {
        // Connected to self
        if (!center) center = Point.average(...this.vertices.map(v => v.posn));

        const v = new Vector(e.vertices[0].posn.x - center.x, e.vertices[0].posn.y - center.y).unitVector;
        const v0 = new Vector(v[0] + v[1], v[1] - v[0]).scale(40);
        const v1 = new Vector(v[0] - v[1], v[1] + v[0]).scale(40);

        e.$el.setAttr('d', `M${e.vertices[0].posn.x},${e.vertices[0].posn.y}c${v0[0]},${v0[1]},${v1[0]},${v1[1]},0,0`);

      } else if (this.options.arc) {
        // Arcs
        const dx = e.vertices[1].posn.x - e.vertices[0].posn.x;
        const dy = e.vertices[1].posn.y - e.vertices[0].posn.y;
        const dr = Math.sqrt(dx * dx + dy * dy);
        e.$el.setAttr('d', `M${e.vertices[0].posn.x},${e.vertices[0].posn.y}A${dr},${dr},0,0,1,${e.vertices[1].posn.x},${e.vertices[1].posn.y}`);

      } else {
        e.$el.setLine(e.vertices[0].posn, e.vertices[1].posn);
      }
    }
github mathigon / textbooks / content / graph-theory / components / graph.ts View on Github external
if (this.options.icon) {
        v.$el.translate(v.posn.x, v.posn.y);
      } else {
        v.$el.setCenter(v.posn);
      }
    }

    for (const e of this.edges) {
      if (e.vertices[0] === e.vertices[1]) {
        // Connected to self
        if (!center) center = Point.average(...this.vertices.map(v => v.posn));

        const v = new Vector(e.vertices[0].posn.x - center.x, e.vertices[0].posn.y - center.y).unitVector;
        const v0 = new Vector(v[0] + v[1], v[1] - v[0]).scale(40);
        const v1 = new Vector(v[0] - v[1], v[1] + v[0]).scale(40);

        e.$el.setAttr('d', `M${e.vertices[0].posn.x},${e.vertices[0].posn.y}c${v0[0]},${v0[1]},${v1[0]},${v1[1]},0,0`);

      } else if (this.options.arc) {
        // Arcs
        const dx = e.vertices[1].posn.x - e.vertices[0].posn.x;
        const dy = e.vertices[1].posn.y - e.vertices[0].posn.y;
        const dr = Math.sqrt(dx * dx + dy * dy);
        e.$el.setAttr('d', `M${e.vertices[0].posn.x},${e.vertices[0].posn.y}A${dr},${dr},0,0,1,${e.vertices[1].posn.x},${e.vertices[1].posn.y}`);

      } else {
        e.$el.setLine(e.vertices[0].posn, e.vertices[1].posn);
      }
    }

    this.trigger('update');