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

Here is the Visual 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 <iostream>

#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;

std::cout << "Program to find whether the given point lies with in line segment:\n";

std::cout << "Enter X1: ";

std::cin >> x1;

std::cout << "Enter Y1: ";

std::cin >> y1;

std::cout << "Enter X2: ";

std::cin >> x2;

std::cout << "Enter Y2: ";

std::cin >> y2;

std::cout << "Enter Point X: ";

std::cin >> px;

std::cout << "Enter Point Y: ";

std::cin >> 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;

}

std::cout << "Equation of the line: ";

std::cout << slope << "X " << ((intercept < 0) ? ' ' : '+') << intercept << "\n";

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

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

{

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

py >= top && py <= bottom )

{

std::cout << "Given point lies in the line segment\n";

}

else

std::cout << "Given point is outside the line segment\n";

}

else

std::cout << "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 . . .