0
votes

I'm trying to create a program in which you input coefficients of two lines (a1x+b1 and a2x+b2) and it calculates if they are coincident, parallel or if they intersect, it finds the point of intersection. I can't successfully compare the coefficients and the output of the program is always like this: They intersect at point (0.00,0.00). What am I doing wrong?

#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;
}
1
Cannot reproduce. If I enter 1 0 -1 3 it prints (1.50 1.50). And if I enter 2 1 -3 4 it prints (0.60, 2.20).Weather Vane
That works correctly.Jean-Baptiste Yunès
What is wrong is that you use wrong test for Parallel which should only compare the gradients a1 and a2, since you already checked for Coincident. And there is unnecessary conditional test in the last section, since if the lines are not parallel they must intersect.Weather Vane

1 Answers

2
votes

right demo

It is right.Maybe,your input is wrong.like this~wrong demo