Skip to content

Instantly share code, notes, and snippets.

@hpohlmeyer
Created December 11, 2017 12:55
Show Gist options
  • Save hpohlmeyer/40bdfc015942daee0a739fb1c3802462 to your computer and use it in GitHub Desktop.
Save hpohlmeyer/40bdfc015942daee0a739fb1c3802462 to your computer and use it in GitHub Desktop.
Get a point on a line, defined by two other points
// Line Interpolation
function createLineInterpolationFunction(p1, p2) {
const slope = (p2.y - p1.y) / (p2.x - p1.x);
const yIntercept = p1.y - (slope * p1.x);
return (x) => slope * x + yIntercept;
}
// Example
const getY = createLineInterpolationFunction({ x: 200, y: 20}, { x: 1440, y: 60 });
const point = { x: 100, y: getY(100) };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment