Software & Finance





Turbo C - Finding the Intersection of two Lines Given Slope and Intercept of Two Lines





Here is the Turbo C program for Finding the Intersection of two Lines Given Slope and Intercept of Two Lines.

 

We directly got the equation for the two lines Y = MX + C as input.

 

Where M = Slope of a Line and C = Intercept

 

If the difference between the slope of two lines are ZERO, then there is no intersection.

Otherwise the intersecting point is calculated using the following formula:

 

intersection_X = (c2 - c1) / (m1 - m2);

intersection_Y = m1 * intersection_X + c1;

 

Where c1 and c2 are intercepts of two lines and m1 and m2 are slope for the two lines.

 

NOTE: This program does not guarantee that the intersecting point lies on the line or not. It just informs us the projected lines from the either direction using the line equation will meet at some point. To get the intersecting point of two line segments in which the intersecting point lies on the two lines, click here.

 


Source Code


#include <stdio.h>

#include <math.h>

 

void main()

{

    float m1, c1, m2, c2;

    float dx, dy;

    float intersection_X, intersection_Y;

 

    printf("Program to find the intersecting point of two lines:\n");

 

    printf("Enter Line1 - Slope: ");

    scanf("%f", &m1);

 

    printf("Enter Line1 - Intercept: ");

    scanf("%f", &c1);

 

    printf("Enter Line1 - Slope: ");

    scanf("%f", &m2);

 

    printf("Enter Line1 - Intercept: ");

    scanf("%f", &c2);

 

    printf("Equation of line1: Y = %.2fX %c %.2f\n", m1, (c1 < 0) ? ' ' : '+',  c1);

    printf("Equation of line2: Y = %.2fX %c %.2f\n", m2, (c2 < 0) ? ' ' : '+',  c2);

 

    if( (m1 - m2) == 0)

        printf("No Intersection between the lines\n");

    else

    {

        intersection_X = (c2 - c1) / (m1 - m2);

        intersection_Y = m1 * intersection_X + c1;

        printf("Intersecting Point: = %.2f, %.2f\n", intersection_X, intersection_Y);

    }

}

Output


 

Program to find the intersecting point of two lines:

Enter Line1 - Slope: 1.25

Enter Line1 - Intercept: 0.75

Enter Line2 - Slope: 2

Enter Line2 - Intercept: -3

Equation of line1: 1.25X +0.75

Equation of line2: 2X  -3

Intersecting Point: = 5,7

Press any key to continue . . .