# Check if a line passes through the origin in C++

C++Server Side ProgrammingProgramming

In this section we will see, how to check a line segment is passes through the origin or not. We have two coordinate points to represent the endpoints of the line segment.

The approach is simple. If we can form the equation of the straight line, and by placing (0, 0) into the equation, and the equation satisfies, then the line passes through the origin.

Suppose the points are and So the equation of line passes through these two lines is −

$$y-y_{1}=\left(\frac{y_{2}-y_{1}}{x_{2}-x_{1}}\right)*\lgroup x-x_{1}\rgroup+c$$

Putting x = 0 and y = 0, we get

$$x_{1}\lgroup y_{2}-y_{1}\rgroup=y_{1}\lgroup x_{2}-x_{1}\rgroup$$

## Example

Live Demo

#include<iostream>
using namespace std;
bool checkPassOrigin(int x1, int y1, int x2, int y2) {
return (x1 * (y2 - y1) == y1 * (x2 - x1));
}
int main() {
if (checkPassOrigin(10, 0, 20, 0) == true)
cout << "Passes Through Origin";
else
cout << "Not Passing Through Origin";
}

## Output

Passes Through Origin
Published on 27-Sep-2019 08:20:24