作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
谁能帮帮我。我了解直线方程以及如何求解纸上的零截距,但我无法将其转换为代码。更具体地说,我需要用两个不同的函数计算一条线截取任何给定 X 或 Y 坐标的点...
double CalcLineYIntercept(LINE *line, double yintercept) { }
double CalcLineXIntercept(LINE *line, double xintercept) { }
因此,CalcLineYIntercept
将返回直线截取 yintercept
的点的 X 坐标(不一定为零)。我在将代数方程式转换为代码时遇到了麻烦(是的,我知道 C++ 是一种代数语言,但代码本身并不能简单地隔离变量)。有没有一种简单的方法可以做到这一点?
非常感谢
最佳答案
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 得到另一个函数。
关于c++ - 直线x和y n-截点/C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4051515/
我是一名优秀的程序员,十分优秀!