Check whether two straight lines are orthogonal or not
Given two line segments AB and CD having A(x1, y1), B(x2, y2), C(x3, y3) and D(x4, y4). The task is to check whether these two lines are orthogonal or not. Two lines are called orthogonal if they are perpendicular at the point of intersection.
Examples:
Input: x1 = 0, y1 = 3, x2 = 0, y2 = -5
x3 = 2, y3 = 0, x4 = -1, y4 = 0
Output: Yes
Input: x1 = 0, y1 = 4, x2 = 0, y2 = -9
x3 = 2, y3 = 0, x4 = -1, y4 = 0
Output: Yes
Approach: If the slopes of the two lines are m1 and m2 then for them to be orthogonal we need to check if:
- Both lines have infinite slope then answer is no.
- One line has infinite slope and if other line has 0 slope then answer is yes otherwise no.
- Both lines have finite slope and their product is -1 then the answer is yes.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool checkOrtho( int x1, int y1, int x2, int y2,
int x3, int y3, int x4, int y4)
{
int m1, m2;
if (x2 - x1 == 0 && x4 - x3 == 0)
return false ;
else if (x2 - x1 == 0) {
m2 = (y4 - y3) / (x4 - x3);
if (m2 == 0)
return true ;
else
return false ;
}
else if (x4 - x3 == 0) {
m1 = (y2 - y1) / (x2 - x1);
if (m1 == 0)
return true ;
else
return false ;
}
else {
m1 = (y2 - y1) / (x2 - x1);
m2 = (y4 - y3) / (x4 - x3);
if (m1 * m2 == -1)
return true ;
else
return false ;
}
}
int main()
{
int x1 = 0, y1 = 4, x2 = 0, y2 = -9;
int x3 = 2, y3 = 0, x4 = -1, y4 = 0;
checkOrtho(x1, y1, x2, y2, x3, y3, x4, y4) ? cout << "Yes"
: cout << "No" ;
return 0;
}
|
Java
import java.io.*;
class GFG {
static boolean checkOrtho( int x1, int y1, int x2, int y2,
int x3, int y3, int x4, int y4)
{
int m1, m2;
if (x2 - x1 == 0 && x4 - x3 == 0 )
return false ;
else if (x2 - x1 == 0 )
{
m2 = (y4 - y3) / (x4 - x3);
if (m2 == 0 )
return true ;
else
return false ;
}
else if (x4 - x3 == 0 )
{
m1 = (y2 - y1) / (x2 - x1);
if (m1 == 0 )
return true ;
else
return false ;
}
else
{
m1 = (y2 - y1) / (x2 - x1);
m2 = (y4 - y3) / (x4 - x3);
if (m1 * m2 == - 1 )
return true ;
else
return false ;
}
}
public static void main (String[] args)
{
int x1 = 0 , y1 = 4 , x2 = 0 , y2 = - 9 ;
int x3 = 2 , y3 = 0 , x4 = - 1 , y4 = 0 ;
if (checkOrtho(x1, y1, x2, y2, x3, y3, x4, y4)== true )
System.out.println ( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def checkOrtho(x1, y1, x2, y2, x3, y3, x4, y4):
if (x2 - x1 = = 0 and x4 - x3 = = 0 ):
return False
elif (x2 - x1 = = 0 ):
m2 = (y4 - y3) / (x4 - x3)
if (m2 = = 0 ):
return True
else :
return False
elif (x4 - x3 = = 0 ):
m1 = (y2 - y1) / (x2 - x1);
if (m1 = = 0 ):
return True
else :
return False
else :
m1 = (y2 - y1) / (x2 - x1)
m2 = (y4 - y3) / (x4 - x3)
if (m1 * m2 = = - 1 ):
return True
else :
return False
if __name__ = = '__main__' :
x1 = 0
y1 = 4
x2 = 0
y2 = - 9
x3 = 2
y3 = 0
x4 = - 1
y4 = 0
if (checkOrtho(x1, y1, x2, y2,
x3, y3, x4, y4)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
static bool checkOrtho( int x1, int y1, int x2, int y2,
int x3, int y3, int x4, int y4)
{
int m1, m2;
if (x2 - x1 == 0 && x4 - x3 == 0)
return false ;
else if (x2 - x1 == 0)
{
m2 = (y4 - y3) / (x4 - x3);
if (m2 == 0)
return true ;
else
return false ;
}
else if (x4 - x3 == 0)
{
m1 = (y2 - y1) / (x2 - x1);
if (m1 == 0)
return true ;
else
return false ;
}
else
{
m1 = (y2 - y1) / (x2 - x1);
m2 = (y4 - y3) / (x4 - x3);
if (m1 * m2 == -1)
return true ;
else
return false ;
}
}
public static void Main ()
{
int x1 = 0, y1 = 4, x2 = 0, y2 = -9;
int x3 = 2, y3 = 0, x4 = -1, y4 = 0;
if (checkOrtho(x1, y1, x2, y2, x3, y3, x4, y4) == true )
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
PHP
<?php
function checkOrtho( $x1 , $y1 , $x2 , $y2 ,
$x3 , $y3 , $x4 , $y4 )
{
if ( $x2 - $x1 == 0 && $x4 - $x3 == 0)
return false;
else if ( $x2 - $x1 == 0)
{
$m2 = (int)(( $y4 - $y3 ) / ( $x4 - $x3 ));
if ( $m2 == 0)
return true;
else
return false;
}
else if ( $x4 - $x3 == 0)
{
$m1 = (int)(( $y2 - $y1 ) / ( $x2 - $x1 ));
if ( $m1 == 0)
return true;
else
return false;
}
else
{
$m1 = (int)(( $y2 - $y1 ) / ( $x2 - $x1 ));
$m2 = (int)(( $y4 - $y3 ) / ( $x4 - $x3 ));
if ( $m1 * $m2 == -1)
return true;
else
return false;
}
}
$x1 = 0; $y1 = 4;
$x2 = 0; $y2 = -9;
$x3 = 2; $y3 = 0;
$x4 = -1; $y4 = 0;
if (checkOrtho( $x1 , $y1 , $x2 , $y2 ,
$x3 , $y3 , $x4 , $y4 ))
print ( "Yes" );
else
print ( "No" );
?>
|
Javascript
<script>
function checkOrtho(x1, y1, x2, y2, x3, y3, x4, y4)
{
let m1, m2;
if (x2 - x1 == 0 && x4 - x3 == 0)
return false ;
else if (x2 - x1 == 0)
{
m2 = parseInt((y4 - y3) / (x4 - x3), 10);
if (m2 == 0)
return true ;
else
return false ;
}
else if (x4 - x3 == 0)
{
m1 = parseInt((y2 - y1) / (x2 - x1), 10);
if (m1 == 0)
return true ;
else
return false ;
}
else
{
m1 = parseInt((y2 - y1) / (x2 - x1), 10);
m2 = parseInt((y4 - y3) / (x4 - x3), 10);
if (m1 * m2 == -1)
return true ;
else
return false ;
}
}
let x1 = 0, y1 = 4, x2 = 0, y2 = -9;
let x3 = 2, y3 = 0, x4 = -1, y4 = 0;
if (checkOrtho(x1, y1, x2, y2, x3, y3, x4, y4) == true )
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
23 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...