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++
#include <bits/stdc++.h>
using namespace std;
bool checkOrigin( int x1, int y1, int x2, int y2)
{
return (x1 * (y2 - y1) == y1 * (x2 - x1));
}
int main()
{
if (checkOrigin(1, 28, 2, 56) == true )
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.io.*;
class GFG {
static boolean checkOrigin( int x1, int y1,
int x2, int y2)
{
return (x1 * (y2 - y1) == y1 * (x2 - x1));
}
public static void main (String[] args)
{
if (checkOrigin( 1 , 28 , 2 , 56 ) == true )
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def checkOrigin(x1, y1, x2, y2):
return (x1 * (y2 - y1) = = y1 * (x2 - x1))
if (checkOrigin( 1 , 28 , 2 , 56 ) = = True ):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG {
static bool checkOrigin( int x1, int y1,
int x2, int y2)
{
return (x1 * (y2 - y1) == y1 * (x2 - x1));
}
public static void Main()
{
if (checkOrigin(1, 28, 2, 56) == true )
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
PHP
<?php
function checkOrigin( $x1 , $y1 ,
$x2 , $y2 )
{
return ( $x1 * ( $y2 - $y1 ) ==
$y1 * ( $x2 - $x1 ));
}
if (checkOrigin(1, 28, 2, 56) == true)
echo ( "Yes" );
else
echo ( "No" );
?>
|
Javascript
<script>
function checkOrigin(x1, y1, x2, y2)
{
return (x1 * (y2 - y1) == y1 * (x2 - x1));
}
if (checkOrigin(1, 28, 2, 56) == true )
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
20 Feb, 2023
Like Article
Save Article