How to use the @mathigon/fermat.clamp 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
arrange(positions: SimplePoint[] = []) {
    let center: Point|undefined = undefined;

    for (const [i, v] of this.vertices.entries()) {
      v.posn = positions[i] || v.posn;

      if (this.options.bound) {
        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;
github mathigon / textbooks / content / chaos / components / water-ripples.ts View on Github external
updateWater() {
    let noChange = true;

    for (let x = 0; x < this.sx; x++) {
      for (let y = 0; y < this.sy; y++) {

        const left = (x === 0) ? 0 : this.depthMap1[x - 1][y];
        const right = (x === this.sx - 1) ? 0 : this.depthMap1[x + 1][y];
        const top = (y === 0) ? 0 : this.depthMap1[x][y - 1];
        const bottom = (y === this.sy - 1) ? 0 : this.depthMap1[x][y + 1];

        let val = left + right + bottom + top;
        val = (val / 2 - this.depthMap2[x][y]) * this.damping;  // Damping
        val = clamp(val, -this.clipping, this.clipping);  // Clipping

        this.depthMap2[x][y] = val;
        if (Math.abs(val) > THRESHOLD) noChange = false;
      }
    }

    // Swap buffer references
    [this.depthMap1, this.depthMap2] = [this.depthMap2, this.depthMap1];

    return noChange;
  }
github mathigon / textbooks / content / graph-theory / components / graph.ts View on Github external
arrange(positions: SimplePoint[] = []) {
    let center: Point|undefined = undefined;

    for (const [i, v] of this.vertices.entries()) {
      v.posn = positions[i] || v.posn;

      if (this.options.bound) {
        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);
github mathigon / boost.js / src / svg.ts View on Github external
export function angleSize(angle: Angle, options: SVGDrawingOptions = {}) {
  if (angle.isRight && !options.round) return 20;
  return 24 + 20 * (1 - clamp(angle.rad, 0, Math.PI) / Math.PI);
}
github mathigon / textbooks / content / chaos / components / water-ripples.ts View on Github external
updateCanvas() {
    if (!this.imagePixels) return;

    const imgDataOut = this.$canvas.ctx.getImageData(0, 0, this.rx, this.ry);
    const pixelsOut = imgDataOut.data;

    for (let i = 0; i < pixelsOut.length; i += 4) {
      const x = (i / 4) % this.rx;
      const y = (i / 4 - x) / this.rx;

      const strength = this.depthMap1[Math.floor(x / 2)][Math.floor(y / 2)];
      const refraction = Math.round(strength * this.refraction);
      const reflection = 1 + strength * this.reflection;

      const x1 = clamp(x + refraction, 0, this.rx - 1);
      const y1 = clamp(y + refraction, 0, this.ry - 1);
      const i1 = ((y1 * this.rx) + x1) * 4;

      pixelsOut[i] = this.imagePixels[i1] * reflection;
      pixelsOut[i + 1] = this.imagePixels[i1 + 1] * reflection;
      pixelsOut[i + 2] = this.imagePixels[i1 + 2] * reflection;
    }

    this.$canvas.ctx.putImageData(imgDataOut, 0, 0);
  }
github mathigon / textbooks / content / chaos / components / water-ripples.ts View on Github external
updateCanvas() {
    if (!this.imagePixels) return;

    const imgDataOut = this.$canvas.ctx.getImageData(0, 0, this.rx, this.ry);
    const pixelsOut = imgDataOut.data;

    for (let i = 0; i < pixelsOut.length; i += 4) {
      const x = (i / 4) % this.rx;
      const y = (i / 4 - x) / this.rx;

      const strength = this.depthMap1[Math.floor(x / 2)][Math.floor(y / 2)];
      const refraction = Math.round(strength * this.refraction);
      const reflection = 1 + strength * this.reflection;

      const x1 = clamp(x + refraction, 0, this.rx - 1);
      const y1 = clamp(y + refraction, 0, this.ry - 1);
      const i1 = ((y1 * this.rx) + x1) * 4;

      pixelsOut[i] = this.imagePixels[i1] * reflection;
      pixelsOut[i + 1] = this.imagePixels[i1 + 1] * reflection;
      pixelsOut[i + 2] = this.imagePixels[i1 + 2] * reflection;
    }

    this.$canvas.ctx.putImageData(imgDataOut, 0, 0);
  }
github mathigon / textbooks / content / statistics / functions.ts View on Github external
function compute(str: string) {
  const max = clamp(Math.floor(str.length / 2), 1, 8);
  const poss = generatePossibilitiesCached(max);
  let result = 1;

  for (let i = 2; i <= max; ++i) {
    let values = poss[i - 1];
    let count = values.length;

    let observed = values.map(v => findCount(str, v));
    let sum = total(observed);

    let chi = total(observed.map(o => ((o - sum / count) ** 2) / sum * count));
    let deg = count - 1;

    result = Math.min(result, Random.chiCDF(chi, deg));
  }