3

有人可以帮帮我吗。我了解直线方程以及如何解决纸上的零截距,但我无法将其转换为代码。更具体地说,我需要用两个不同的函数计算一条线与任何给定的 X 或 Y 坐标相交的点......

double CalcLineYIntercept(LINE *line, double yintercept) { }
double CalcLineXIntercept(LINE *line, double xintercept) { }

因此,CalcLineYIntercept将返回直线相交点的 X 坐标yintercept(不一定为零)。我无法将代数方程转换为代码(是的,我知道 C++ 是一种代数语言,但代码本身并不能简单地隔离变量)。有没有一种简单的方法可以做到这一点?

非常感谢

4

2 回答 2

3
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 以获得另一个函数。

于 2010-10-29T12:14:12.653 回答
0

yintercept从直线的y坐标中减去,然后使用您已经知道的数学来求解y = 0时的x 。 Mutatis Mutandis for 。xintercept

于 2010-10-29T12:08:38.960 回答