
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Advertisements