Line x and y n-intercept point / C++
Can someone please help me. I understand the equation of a line and how to solve for the zero intercept on paper, but I have trouble converting it to code. More specifically, I need to calculate the point at which a line intercepts any given X or Y coordinate with two different functions...
double CalcLineYIntercept(LINE *line, double yintercept) { }
double CalcLineXIntercept(LINE *line, double xintercept) { }
So, CalcLineYIntercept
would return the X coordinate of the point where the line intercepts yintercept
(not necessarily zero). I have trouble converting algebraic equations to code (and yes I understand that C++ is an algebraic language, but the code itself does not simply and isolate variables). Is there a simple way to accomplish this?
Thank you ve开发者_C百科ry much
double CalcLineYIntercept(LINE *line, double yintercept)
{
dx = line->x2 - line->x1;
dy = line->y2 - line->y1;
deltay = yintercept - line->y2;
if (dy != 0) {
//dy very close to 0 will be numerically unstable, account for that
intercept = line->x2 + (dx/dy) * deltay;
}
else {
//line is parrallel to x-axis, will never reach yintercept
intercept = NaN;
}
}
Reverse x and y to get the other function.
Subtract yintercept
from the line's y-coordinates, then use the math you already know to solve for x when y = 0. Mutatis Mutandis for xintercept
.
精彩评论