Skip to content

Instantly share code, notes, and snippets.

@andrewluetgers
Created April 2, 2019 14:51
Show Gist options
  • Save andrewluetgers/ee0b72af9d721450785935cda7867e46 to your computer and use it in GitHub Desktop.
Save andrewluetgers/ee0b72af9d721450785935cda7867e46 to your computer and use it in GitHub Desktop.
integration, auc, interpolation fns
// get n linear steps between point{x, y} a and b
function interPoints(a, b, n) {
let int = (t) => ({
x: a.x * (1 - t) + b.x * t,
y: a.y * (1 - t) + b.y * t
}),
step = 1/(n+1),
ret = [];
for (let i=0; i<n; i++) {
ret[i] = int(step*(i+1))
}
return ret;
}
function interpolate(points, interPoints) {
interPoints = interPoints || 1;
let pts = [];
for (var i = 0; i < points.length-1; i++) {
pts = pts.concat(points[i], this.interPoints(points[i], points[i+1], interPoints))
}
return pts.concat(points[points.length-1]);
}
function areaIntegration(points) {
let rectArea = 0;
for (var i = 0; i < points.length-1; i++) {
let a = points[i],
b = points[i+1],
mid = this.interPoints(a, b, 1);
rectArea += (b.x - a.x) * mid[0].y;
}
return rectArea;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment