# Turbo C - Check whether given point lies on a Line Segment

Here is the Turbo C program to check whether given point lies on a line segment or not.

Find out the equation for the line seperately Y = MX + C

Where M = Slope of a Line and C = Intercept

To Find the slope of a line

slope = (y2 - y1) / (x2 - x1)

To Find the intercept of the line, intercept = y1 - (slope) * x1

Where c1 is the intercept of a line and m1 is the slope for the line.

## Source Code

#include <stdio.h>

#include <math.h>

void main()

{

float slope, intercept;

float x1, y1, x2, y2;

float px, py;

float left, top, right, bottom; // Bounding Box For Line Segment

float dx, dy;

printf("Program to find whether the given point lies with in line segment:\n");

printf("Enter X1: ");

scanf("%f", &x1);

printf("Enter Y1: ");

scanf("%f", &y1);

printf("Enter X2: ");

scanf("%f", &x2);

printf("Enter Y2: ");

scanf("%f", &y2);

printf("Enter Point X: ");

scanf("%f", &px);

printf("Enter Point Y: ");

scanf("%f", &py);

dx = x2 - x1;

dy = y2 - y1;

slope = dy / dx;

// y = mx + c

// intercept c = y - mx

intercept = y1 - slope * x1; // which is same as y2 - slope * x2

// For Bounding Box

if(x1 < x2)

{

left = x1;

right = x2;

}

else

{

left = x2;

right = x1;

}

if(y1 < y2)

{

top = y1;

bottom = y2;

}

else

{

top = y1;

bottom = y2;

}

printf("Equation of the line: %.2f X %c %.2f\n", slope, ((intercept < 0) ? ' ' : '+'), intercept);

if( slope * px + intercept > (py - 0.01) &&

slope * px + intercept < (py + 0.01))

{

if( px >= left && px <= right &&

py >= top && py <= bottom )

{

printf("Given point lies in the line segment\n");

}

else

printf("Given point is outside the line segment\n");

}

else

printf("The point is outside the line segment\n");

}

## Output

Program to find whether the given point lies with in line segment:

Enter X1: 1

Enter Y1: 2

Enter X2: 5

Enter Y2: 7

Enter Point X: 2.33

Enter Point Y: 3.67

Equation of the line: 1.25X +0.75

Given point lies in the line segment

Press any key to continue . . .

Program to find whether the given point lies with in line segment:

Enter X1: 1

Enter Y1: 2

Enter X2: 5

Enter Y2: 7

Enter Point X: 2.3

Enter Point Y: 3.6

Equation of the line: 1.25X +0.75

The point is outside the line segment

Press any key to continue . . .