判断直线是平行的、重合的还是相交的。如果它们相交,找到交点
Find if lines are parallel, coincident or they intersect. If they intersect, find point of intersection
我正在尝试创建一个程序,您可以在其中输入两条线(a1x+b1 和 a2x+b2)的系数,然后计算它们是否重合、平行或者相交,然后找到交点.我无法成功比较系数,程序的输出总是这样:它们相交于点 (0.00,0.00)。我做错了什么?
#include <stdio.h>
#include <math.h>
#define epsilon 0.001
int main() {
float a1, b1, a2, b2;
float x = 0;
float y = 0;
printf("Insert a1,b1,a2,b2: ");
scanf("%f %f %f %f", &a1, &b1, &a2, &b2);
if (fabs(a1 - a2) < epsilon && fabs(b1 - b2) < epsilon) {
printf("Coincident");
} else if (fabs(a1 - a2) < epsilon && fabs(b1 - b2) > epsilon) {
printf("Parallel");
} else if (((fabs(a1 - a2) > epsilon && fabs(b1 - b2) > epsilon) ||
(fabs(a1 - a2) > epsilon && fabs(b1 - b2) < epsilon))) {
x = (b2 - b1) / (a1 - a2);
y = a1 * x + b1;
printf("They intersect at point (%.2f, %.2f)", x, y);
}
return 0;
}
right demo
是right.Maybe,你输入的是wrong.like这个~wrong demo
我正在尝试创建一个程序,您可以在其中输入两条线(a1x+b1 和 a2x+b2)的系数,然后计算它们是否重合、平行或者相交,然后找到交点.我无法成功比较系数,程序的输出总是这样:它们相交于点 (0.00,0.00)。我做错了什么?
#include <stdio.h>
#include <math.h>
#define epsilon 0.001
int main() {
float a1, b1, a2, b2;
float x = 0;
float y = 0;
printf("Insert a1,b1,a2,b2: ");
scanf("%f %f %f %f", &a1, &b1, &a2, &b2);
if (fabs(a1 - a2) < epsilon && fabs(b1 - b2) < epsilon) {
printf("Coincident");
} else if (fabs(a1 - a2) < epsilon && fabs(b1 - b2) > epsilon) {
printf("Parallel");
} else if (((fabs(a1 - a2) > epsilon && fabs(b1 - b2) > epsilon) ||
(fabs(a1 - a2) > epsilon && fabs(b1 - b2) < epsilon))) {
x = (b2 - b1) / (a1 - a2);
y = a1 * x + b1;
printf("They intersect at point (%.2f, %.2f)", x, y);
}
return 0;
}
right demo
是right.Maybe,你输入的是wrong.like这个~wrong demo