How to use the @mathigon/fermat.Point.average 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
let distance = this.options.r || 5;
        v.posn.x = clamp(v.posn.x, distance, this.width - distance);
        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.js View on Github external
this.edges.forEach(function(e) {

      // connected to self
      if (e.vertices[0] === e.vertices[1]) {
        if (!center) center = Point.average(..._this.vertices.map(v => v.posn));

        let v = V(e.vertices[0].posn.x - center.x, e.vertices[0].posn.y - center.y).normal;
        let v0 = V(v[0] + v[1], v[1] - v[0]).scale(40);
        let v1 = V(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');

        // arcs
      } else if (_this.options.arc) {
        let dx = e.vertices[1].posn.x - e.vertices[0].posn.x;
        let dy = e.vertices[1].posn.y - e.vertices[0].posn.y;
        let 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);