用C语言,已知一条直线的一点和斜率,令一条直线的两点,如何求两条直线的交点x,y
展开全部
首先判断第2条直线是否是垂直于x轴的,如果是,单独处理。
再求出第二条直线斜率k,并判断k是否和第一条直线斜率相同,若相同,则不存在焦点,或者有无穷多个交点。
以上条件都不满足则直接解方程求出交点
#include <stdio.h>
#include <math.h>
#define EQUAL(x, y) (fabs((x) - (y)) <= 1e-6)
int main()
{
double k1, b1, k2, b2;
double x1, y1, x2, y2;
double resultx, resulty;
printf("Input the k and b: ");
scanf("%llf %llf", &k1, &b1);
printf("Input the first point's x and y cand: ");
scanf("%llf %llf", &x1, &y1);
printf("Input the first point's x and y cand: ");
scanf("%llf %llf", &x2, &y2);
if (EQUAL(x1, x2))
{
printf("The point is (%.2llf, %.2llf).\n", x1, x1 * k1 + b1);
}
else
{
k2 = (y1 - y2) / (x1 - x2);
b2 = (y1 * x2 - x1 * y2) / (x2 - x1);
if (EQUAL(k2, k1))
{
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;
}
再求出第二条直线斜率k,并判断k是否和第一条直线斜率相同,若相同,则不存在焦点,或者有无穷多个交点。
以上条件都不满足则直接解方程求出交点
#include <stdio.h>
#include <math.h>
#define EQUAL(x, y) (fabs((x) - (y)) <= 1e-6)
int main()
{
double k1, b1, k2, b2;
double x1, y1, x2, y2;
double resultx, resulty;
printf("Input the k and b: ");
scanf("%llf %llf", &k1, &b1);
printf("Input the first point's x and y cand: ");
scanf("%llf %llf", &x1, &y1);
printf("Input the first point's x and y cand: ");
scanf("%llf %llf", &x2, &y2);
if (EQUAL(x1, x2))
{
printf("The point is (%.2llf, %.2llf).\n", x1, x1 * k1 + b1);
}
else
{
k2 = (y1 - y2) / (x1 - x2);
b2 = (y1 * x2 - x1 * y2) / (x2 - x1);
if (EQUAL(k2, k1))
{
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;
}
追问
能不能说一下每一个变量是啥意思啊
追答
不好意思,第一遍没看好题意:
#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;
}
展开全部
嘛都不用判断,直接来计算。
程序算法简单,计算麻烦,所幸是计算机来完成的。
#include<stdio.h>
int main()
{
double x,y,x0,y0,x1,y1,x2,y2,k1,k2;
printf("请输入x0,y0,x1,y1,x2,y2,k1的值,以空格隔开");
scanf("%lf %lf %lf %lf %lf %lf %lf ",&x0,&y0,&x1,&y1,&x2,&y2,&k1);
x=(k1*x0*x2-x2*y0-k1*x0*x1+x1*y0+x2*y1-x1*y2)/(y1-y2-k1*x2+k1*x1);
y=(y0*y2-y0*y1+k1*x0*y1-k1*x0*y2+k1*x1*y2-k1*x2*y1)/(k1*x2-k1*x1-y1+y2);
printf("%lf %lf\n",x,y);
return 0;
}
程序算法简单,计算麻烦,所幸是计算机来完成的。
#include<stdio.h>
int main()
{
double x,y,x0,y0,x1,y1,x2,y2,k1,k2;
printf("请输入x0,y0,x1,y1,x2,y2,k1的值,以空格隔开");
scanf("%lf %lf %lf %lf %lf %lf %lf ",&x0,&y0,&x1,&y1,&x2,&y2,&k1);
x=(k1*x0*x2-x2*y0-k1*x0*x1+x1*y0+x2*y1-x1*y2)/(y1-y2-k1*x2+k1*x1);
y=(y0*y2-y0*y1+k1*x0*y1-k1*x0*y2+k1*x1*y2-k1*x2*y1)/(k1*x2-k1*x1-y1+y2);
printf("%lf %lf\n",x,y);
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
不需要精确解,用数值的方法可以吗?
追问
可以,只要算出来就行
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
令一条直线的两点?
麻烦说清楚一点,好给你解决数学问题哦。
麻烦说清楚一点,好给你解决数学问题哦。
追问
就是已知直线1的k1和(x1,y1),直线2的两点(x21,y21),(x22,y22),求出直线1和直线2 的交点(x,y)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询