用C语言,已知一条直线的一点和斜率,令一条直线的两点,如何求两条直线的交点x,y

gamist 1年前 已收到1个回答 举报

winner3120 幼苗

共回答了15个问题采纳率:93.3% 举报

首先判断第2条直线是否是垂直于x轴的,如果是,单独处理.
再求出第二条直线斜率k,并判断k是否和第一条直线斜率相同,若相同,则不存在焦点,或者有无穷多个交点.
以上条件都不满足则直接解方程求出交点
#include
#include
#define EQUAL(x,y) (fabs((x) - (y))

1年前 追问

8

gamist 举报

能不能说一下每一个变量是啥意思啊

举报 winner3120

不好意思,第一遍没看好题意: #include #include #define EQUAL(x, y) (fabs((x) - (y)) <= 1e-6) int main() { double k1, b1, k2, b2; //直线1和2的参数k,b,直线为 y = kx + b,其中直线2的参数待定 double tmpx, tmpy; //第一条直线的一点,如题 double x1, y1, x2, y2; //直线2的两点坐标,如题 double resultx, resulty; //焦点的坐标 //输入k,和一点的坐标x,y,以空格为分隔符 printf("Input the k and x, y cand of the point (separate with space): "); scanf("%llf %llf %llf", &k1, &tmpx, &tmpy); b1 = tmpy - k1 * tmpx; //计算直线1参数b printf("Input the first point's x and y cand: "); //输入直线2的第一点 scanf("%llf %llf", &x1, &y1); printf("Input the first point's x and y cand: "); //输入直线2的第二点 scanf("%llf %llf", &x2, &y2); if (EQUAL(x1, x2)) //若x1和x2相等,也就是说直线2没有斜率的情况 { printf("The point is (%.2llf, %.2llf).n", x1, x1 * k1 + b1); } else { //计算直线2的参数 k2 = (y1 - y2) / (x1 - x2); b2 = (y1 * x2 - x1 * y2) / (x2 - x1); if (EQUAL(k2, k1)) //若斜率相同则要么2直线平行要么2直线重合 { if (EQUAL(b2, b1)) //重合 printf("Two lines overlap!n"); else //平行 printf("None point!n"); } else { //解直线方程 resultx = (b1 - b2) / (k2 - k1); resulty = k1 * resultx + b1; printf("The point is (%.2llf, %.2llf).n", resultx, resulty); } } return 0; }
可能相似的问题
Copyright © 2024 YULUCN.COM - 雨露学习互助 - 17 q. 2.256 s. - webmaster@yulucn.com