线x和y的n截距点 / C++

3

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

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

所以,CalcLineYIntercept将返回线截取yintercept的点的X坐标(不一定为零)。我在将代数方程转换为代码方面遇到了麻烦(是的,我知道C++是一种代数语言,但代码本身并不简单地隔离变量)。有没有简单的方法可以完成这个任务?
非常感谢。

1
我们需要声明 LINE 来回答这个问题。也许您可以写下您想要实现的公式。 - Björn Pollex
LINE只是由两个坐标点(x1,y1)和(x2,y2)组成的简单坐标。y=mx+b是一条直线的方程式,其中y2 - y1 / x2 - x1是该直线的斜率(m),b是偏移量,但我不确定如何将b引入此方程式中,或者如何编写代码来表示它。我需要n截距点(给定坐标的截距点,而不仅仅是零点的截距点)。 - user491232
1
@user491232:请编辑您的问题并添加您在评论中提供的信息。 - Björn Pollex
所以你想传递一个 x=(X)(或 y=(Y)),并找到你的线与调整后的轴相交的位置? - user245019
2个回答

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


如果这个回答解决了你的问题,也许你可以接受这个回答作为答案? - jilles de wit

-1
从该线的y坐标中减去yintercept,然后使用您已知的数学方法解决当y = 0 时的x。对于xintercept同理。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接