Ibrahim Filandra Ibrahim Filandra - 10 months ago 63
C Question

Find if lines are parallel, coincident or they intersect. If they intersect, find point of intersection

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) {

else if(fabs(a1-a2)<epsilon && fabs(b1-b2)>epsilon) {

else if (((fabs(a1-a2)>epsilon && fabs(b1-b2)>epsilon) || (fabs(a1-a2)>epsilon && fabs(b1-b2)<epsilon)) )
printf("They intersect at point (%.2f, %.2f)", x, y);

return 0;

Answer Source

right demo

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