Socket
Book a DemoInstallSign in
Socket

@hscmap/linear-fit

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@hscmap/linear-fit

* High performance linear fit * [Working Demo](https://michitaro.github.io/linear-fit)

latest
Source
npmnpm
Version
0.0.0
Version published
Maintainers
1
Created
Source

Linear Fit

  • High performance linear fit
  • Working Demo

Install

npm instasll --save @hscmap/linear-fit

Example (source code of the working demo)

import { linearFit } from "@hscmap/linear-fit"


window.addEventListener('load', e => {
    const canvas = document.querySelector('canvas')!
    const ctx = canvas.getContext('2d')!

    const samples = generateRandomPoints(canvas)
    for (const [x, y] of samples) {
        ctx.fillRect(x, y, 1, 1)
    }

    const [a0, a1, a2] = linearFit(samples)
    ctx.strokeStyle = '#f00'
    ctx.beginPath()
    for (let x = 0; x < canvas.width; ++x) {
        const y = a0 + a1 * x + a2 * x ** 2
        ctx.lineTo(x, y)
    }
    ctx.stroke()
})


function generateRandomPoints(size: { width: number, height: number }) {
    const samples: [number, number][] = []
    const w0 = Math.random() - 0.5
    const c = Math.random() * 0.5
    for (let i = 0; i < 800; ++i) {
        const x = size.width * Math.random()
        const w = 2 * (x / size.width) - 1
        const y = 0.5 * size.height * ((w - w0) ** 2 + 0.2 * Math.random() + c)
        samples.push([x, y])
    }
    return samples
}

FAQs

Package last updated on 25 Aug 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts