Skip to content

Instantly share code, notes, and snippets.

@gosoccerboy5
Last active April 21, 2022 20:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gosoccerboy5/83337ae880007d3318c9f80f0861b11f to your computer and use it in GitHub Desktop.
Save gosoccerboy5/83337ae880007d3318c9f80f0861b11f to your computer and use it in GitHub Desktop.
Random math crap that helps you with tedious school math
// Find a point between two points that fits a certain ratio of distance to both of them
function findMidpointFromPointsAndRatio(a, b, c, d, f = 1, g = 1) {
/**
Example: The coordinates of the endpoints of Line ST are S(10, 2) and T(17, 16).
Point U is on ST and divides it such that SU:TU is 2:5.
How the function would be called: findMidpointFromPointsAndRatio(10, 2, 17, 16, 2, 5);
What it should return: Array [12, 6]
**/
const h = f+g;
return [a + f*(c-a)/h, b + f*(d-b)/h];
}
function quadraticFromPoints(r1, r2, p) {
// Supply root 1, root 2, and an extra point outside of those 2
var aValue = p[1]/(p[0]**2-r1*p[0]-r2*p[0]+r1*r2);
var bValue = -aValue*(r1+r2);
var cValue = aValue*r1*r2;
return `${aValue === 1 ? "" : aValue}x^2${(bValue >= 0 ? "+" : "") + bValue}x${(cValue >= 0 ? "+" : "") + cValue}`;
}
console.log(quadraticFromPoints(-1, -1, [4, 125]));
// This should return 5x^2+10x+5. Edit the values to your liking, then test if they're correct
function quadraticFromVertex(vertex, point) {
// Supply [x, y] for both the vertex and point
var h = vertex[0];
var k = vertex[1];
var x = point[0];
var y = point[1];
var aValue = (y-k)/(x**2-2*x*h+h**2);
var bValue = aValue * -2 * h;
var cValue = aValue * h**2 + k;
return `${aValue === 1 ? "" : aValue}x^2${(bValue >= 0 ? "+" : "") + bValue}x${(cValue >= 0 ? "+" : "") + cValue}`;
}
console.log(quadraticFromVertex([-3, 4], [-2, 2]));
// Should return -2x^2-12x-14. Tweak the values to your liking
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment