Skip to content

Instantly share code, notes, and snippets.

@bit-hack
Created March 31, 2020 21:28
Show Gist options
  • Save bit-hack/fb08b5338d83888475b77e1ca5fcf9db to your computer and use it in GitHub Desktop.
Save bit-hack/fb08b5338d83888475b77e1ca5fcf9db to your computer and use it in GitHub Desktop.
point on line solver given one component
// for the line from [x0,y0] -> [x1,y1]
// given y solve for sx (point on line)
// given x solve for sy (point on line)
const float dx = x1 - x0;
const float dy = y1 - y0;
const float sx = x0 + (y*dx - y0*dx) / dy;
const float sy = y0 + (x*dy - x0*dy) / dx;
// or
const float sx = x0 + (y - y0) * (dx/dy);
const float sy = y0 + (x - x0) * (dy/dx);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment