Visual C++ - Check whether given point lies on a Line (Y = MX + C)
Here is the Visual C++ program to check whether given point lies on a line or not.
We directly got the equation for the two lines Y = MX + C as input
Where M = Slope of a Line and C = Intercept
Solve the given point px and py with Y = MX + C.
NOTE: To check the given point lies on a line segment, click here
Source Code
#include <iostream>
#include <math.h>
void main()
{
float slope, intercept;
float px, py;
std::cout << "Program to find whether the given point lies on a line:\n";
std::cout << "Enter Line1 - Slope: ";
std::cin >> slope;
std::cout << "Enter Line1 - Intercept: ";
std::cin >> intercept;
std::cout << "Enter Point X: ";
std::cin >> px;
std::cout << "Enter Point Y: ";
std::cin >> py;
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))
{
std::cout << "Given point lies on the line\n";
}
else
std::cout << "Given point is outside the line\n";
}
Output
Program to find whether the given point lies on a line:
Enter Line1 - Slope: 1.25
Enter Line1 - Intercept: 0.75
Enter Point X: 2.33
Enter Point Y: 3.67
Equation of the line: 1.25X +0.75
Given point lies on the line
Press any key to continue . . .
|