# Check if a line passes through the origin

Given two coordinates of a line as (x1, y1) and (x2, y2), find if the line passing through these points also passes through origin or not.

Examples:

Input : (x1, y1) = (10, 0) (x2, y2) = (20, 0) Output : Yes The line passing through these points clearly passes through the origin as the line is x axis. Input : (x1, y1) = (1, 28) (x2, y2) = (2, 56) Output : Yes

**Approach:** Equation of a line passing through two points (x1, y1) and (x2, y2) is given by

y-y1 = ((y2-y1) / (x2-x1))(x-x1) + c

If line is also passing through origin, then c=0, so equation of line becomes

y-y1 = ((y2-y1) / (x2-x1))(x-x1)

Keeping x=0, y=0 in the above equation we get,

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

So above equation must be satisfied if any line passing through two coordinates (x1, y1) and (x2, y2) also passes through origin (0, 0).

## C++

`/* C++ program to find if line passing through ` ` ` `two coordinates also passes through origin ` ` ` `or not */` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `bool` `checkOrigin(` `int` `x1, ` `int` `y1, ` `int` `x2, ` `int` `y2) ` `{ ` ` ` `return` `(x1 * (y2 - y1) == y1 * (x2 - x1)); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `if` `(checkOrigin(1, 28, 2, 56) == ` `true` `) ` ` ` `cout << ` `"Yes"` `; ` ` ` `else` ` ` `cout << ` `"No"` `; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find if line passing through ` `// two coordinates also passes through origin ` `// or not ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` ` ` `static` `boolean` `checkOrigin(` `int` `x1, ` `int` `y1, ` ` ` `int` `x2, ` `int` `y2) ` ` ` `{ ` ` ` `return` `(x1 * (y2 - y1) == y1 * (x2 - x1)); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `if` `(checkOrigin(` `1` `, ` `28` `, ` `2` `, ` `56` `) == ` `true` `) ` ` ` `System.out.println(` `"Yes"` `); ` ` ` `else` ` ` `System.out.println(` `"No"` `); ` ` ` `} ` `} ` ` ` `// This code is contributed by Ajit. ` |

*chevron_right*

*filter_none*

## Python3

`# Python program to find if line ` `# passing through two coordinates ` `# also passes through origin or not ` ` ` `def` `checkOrigin(x1, y1, x2, y2): ` ` ` `return` `(x1 ` `*` `(y2 ` `-` `y1) ` `=` `=` `y1 ` `*` `(x2 ` `-` `x1)) ` ` ` `# Driver code ` `if` `(checkOrigin(` `1` `, ` `28` `, ` `2` `, ` `56` `) ` `=` `=` `True` `): ` ` ` `print` `(` `"Yes"` `) ` `else` `: ` ` ` `print` `(` `"No"` `) ` ` ` `# This code is contributed ` `# by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find if line passing through ` `// two coordinates also passes through origin ` `// or not ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `static` `bool` `checkOrigin(` `int` `x1, ` `int` `y1, ` ` ` `int` `x2, ` `int` `y2) ` ` ` `{ ` ` ` `return` `(x1 * (y2 - y1) == y1 * (x2 - x1)); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `if` `(checkOrigin(1, 28, 2, 56) == ` `true` `) ` ` ` `Console.WriteLine(` `"Yes"` `); ` ` ` `else` ` ` `Console.WriteLine(` `"No"` `); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find if ` `// line passing through ` `// two coordinates also ` `// passes through origin ` `// or not ` ` ` `function` `checkOrigin(` `$x1` `, ` `$y1` `, ` ` ` `$x2` `, ` `$y2` `) ` `{ ` ` ` `return` `(` `$x1` `* (` `$y2` `- ` `$y1` `) == ` ` ` `$y1` `* (` `$x2` `- ` `$x1` `)); ` `} ` ` ` `// Driver code ` `if` `(checkOrigin(1, 28, 2, 56) == true) ` ` ` `echo` `(` `"Yes"` `); ` `else` ` ` `echo` `(` `"No"` `); ` ` ` `// This code is contributed by Ajit. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

Yes

## Recommended Posts:

- Length of the normal from origin on a straight line whose intercepts are given
- Check whether the point (x, y) lies on a given line
- Check whether two points (x1, y1) and (x2, y2) lie on same side of a given line or not
- How to check if two given line segments intersect?
- Check if a line touches or intersects a circle
- Check if it is possible to draw a straight line with the given direction cosines
- Check if a line at 45 degree can divide the plane into two equal weight parts
- Program to calculate area of inner circle which passes through center of outer circle and touches its circumference
- Find K Closest Points to the Origin
- Lexicographically Kth smallest way to reach given coordinate from origin
- Find the maximum possible distance from origin using given points
- Number of jump required of given length to reach a point of form (d, 0) from origin in 2D plane
- Slope of the line parallel to the line with the given slope
- Slope of perpendicular to line
- Find the other end point of a line with given one end and mid

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.