String x and y n-intercept point / C ++

Can someone please help me. I understand the equation of the line and how to solve for zero interception on paper, but I have problems converting it to code. In particular, I need to calculate the point at which the 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, it CalcLineYInterceptreturns the X coordinate of the point where the line intercepts yintercept(not necessarily zero). I have problems converting algebraic equations into code (and yes, I understand that C ++ is an algebraic language, but the code itself is not simple and isolates variables). Is there an easy way to do this?

Many thanks

+3
source share
2
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;
    }
}

x y, .

+3

yintercept y-, , , x, y = 0. Mutatis Mutandis xintercept.

0

Source: https://habr.com/ru/post/1772103/


All Articles