- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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

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

#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

- Related Questions & Answers
- How to create a dashed line that passes through Y = 1 in base R?
- How to create a line that passes through specified points in an R plot?
- Check If It Is a Straight Line in C++
- Check if a line touches or intersects a circle in C++
- Check if two line segments intersect
- Program to find line passing through 2 Points in C++
- Check if a line at 45 degree can divide the plane into two equal weight parts in C++
- How to plot the regression line starting from origin using ggplot2 in R?
- Furthest From Origin in C++
- Check if Caps Lock and Num Lock is on or off through Console in C#
- What is Compiler Passes?
- Check if a File exists in C#
- Determine the number of squares of unit area that a line will pass through in C++?
- Check if the SortedSet contains a specific element in C#
- Check if the Hashtable contains a specific Key in C#

Advertisements