Comprobar si dos rectas son ortogonales o no

Dados dos segmentos de línea AB y CD que tienen A(x 1 , y 1 ), B(x 2 , y 2 ), C(x 3 , y 3 ) y D(x 4 , y 4 ). La tarea es verificar si estas dos líneas son ortogonales o no. Dos rectas se llaman ortogonales si son perpendiculares en el punto de intersección.
 

Ejemplos: 
 

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

Enfoque: si las pendientes de las dos líneas son m 1 y m 2 , entonces para que sean ortogonales, debemos verificar si: 
 

  • Ambas rectas tienen pendiente infinita, entonces la respuesta es no.
  • Una línea tiene pendiente infinita y si otra línea tiene pendiente 0, la respuesta es sí, de lo contrario, no.
  • Ambas rectas tienen pendiente finita y su producto es -1 entonces la respuesta es sí.

A continuación se muestra la implementación del enfoque anterior:
 

C++

// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if two straight
// lines are orthogonal or not
bool checkOrtho(int x1, int y1, int x2, int y2,
                int x3, int y3, int x4, int y4)
{
 
    int m1, m2;
 
    // Both lines have infinite slope
    if (x2 - x1 == 0 && x4 - x3 == 0)
        return false;
 
    // Only line 1 has infinite slope
    else if (x2 - x1 == 0) {
 
        m2 = (y4 - y3) / (x4 - x3);
 
        if (m2 == 0)
            return true;
        else
            return false;
    }
 
    // Only line 2 has infinite slope
    else if (x4 - x3 == 0) {
 
        m1 = (y2 - y1) / (x2 - x1);
 
        if (m1 == 0)
            return true;
        else
            return false;
    }
 
    else {
        // Find slopes of the lines
        m1 = (y2 - y1) / (x2 - x1);
        m2 = (y4 - y3) / (x4 - x3);
 
        // Check if their product is -1
        if (m1 * m2 == -1)
            return true;
        else
            return false;
    }
}
 
// Driver code
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

//Java implementation of above approach
 
import java.io.*;
 
class GFG {
     
    // Function to check if two straight
    // lines are orthogonal or not
    static boolean checkOrtho(int x1, int y1, int x2, int y2,
                    int x3, int y3, int x4, int y4)
    {
 
        int m1, m2;
     
        // Both lines have infinite slope
        if (x2 - x1 == 0 && x4 - x3 == 0)
            return false;
 
        // Only line 1 has infinite slope
        else if (x2 - x1 == 0)
        {
            m2 = (y4 - y3) / (x4 - x3);
            if (m2 == 0)
                return true;
            else
                return false;
        }
 
        // Only line 2 has infinite slope
        else if (x4 - x3 == 0)
        {
             m1 = (y2 - y1) / (x2 - x1);
            if (m1 == 0)
                return true;
            else
                return false;
        }
 
        else
        {
            // Find slopes of the lines
            m1 = (y2 - y1) / (x2 - x1);
            m2 = (y4 - y3) / (x4 - x3);
 
            // Check if their product is -1
            if (m1 * m2 == -1)
                return true;
            else
                return false;
        }
    }
 
    // Driver code
    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" );
    }
}
 
//This code is contributed by akt_mit..

Python3

# Python 3 implementation of above approach
 
# Function to check if two straight
# lines are orthogonal or not
def checkOrtho(x1, y1, x2, y2, x3, y3, x4, y4):
     
    # Both lines have infinite slope
    if (x2 - x1 == 0 and x4 - x3 == 0):
        return False
 
    # Only line 1 has infinite slope
    elif (x2 - x1 == 0):
        m2 = (y4 - y3) / (x4 - x3)
 
        if (m2 == 0):
            return True
        else:
            return False
 
    # Only line 2 has infinite slope
    elif (x4 - x3 == 0):
        m1 = (y2 - y1) / (x2 - x1);
 
        if (m1 == 0):
            return True
        else:
            return False
 
    else:
         
        # Find slopes of the lines
        m1 = (y2 - y1) / (x2 - x1)
        m2 = (y4 - y3) / (x4 - x3)
 
        # Check if their product is -1
        if (m1 * m2 == -1):
            return True
        else:
            return False
     
# Driver code
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")
 
# This code is contributed by
# Shashank_Sharma

C#

// C# implementation of above approach
using System;
 
class GFG
{
     
    // Function to check if two straight
    // lines are orthogonal or not
    static bool checkOrtho(int x1, int y1, int x2, int y2,
                    int x3, int y3, int x4, int y4)
    {
 
        int m1, m2;
     
        // Both lines have infinite slope
        if (x2 - x1 == 0 && x4 - x3 == 0)
            return false;
 
        // Only line 1 has infinite slope
        else if (x2 - x1 == 0)
        {
            m2 = (y4 - y3) / (x4 - x3);
            if (m2 == 0)
                return true;
            else
                return false;
        }
 
        // Only line 2 has infinite slope
        else if (x4 - x3 == 0)
        {
            m1 = (y2 - y1) / (x2 - x1);
            if (m1 == 0)
                return true;
            else
                return false;
        }
 
        else
        {
            // Find slopes of the lines
            m1 = (y2 - y1) / (x2 - x1);
            m2 = (y4 - y3) / (x4 - x3);
 
            // Check if their product is -1
            if (m1 * m2 == -1)
                return true;
            else
                return false;
        }
    }
 
    // Driver code
    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" );
    }
}
 
// This code is contributed by Ryuga

PHP

<?php
// PHP implementation of above approach
 
// Function to check if two straight
// lines are orthogonal or not
function checkOrtho($x1, $y1, $x2, $y2,
                    $x3, $y3, $x4, $y4)
{
 
    // Both lines have infinite slope
    if ($x2 - $x1 == 0 && $x4 - $x3 == 0)
        return false;
 
    // Only line 1 has infinite slope
    else if ($x2 - $x1 == 0)
    {
        $m2 = (int)(($y4 - $y3) / ($x4 - $x3));
 
        if ($m2 == 0)
            return true;
        else
            return false;
    }
 
    // Only line 2 has infinite slope
    else if ($x4 - $x3 == 0)
    {
        $m1 = (int)(($y2 - $y1) / ($x2 - $x1));
 
        if ($m1 == 0)
            return true;
        else
            return false;
    }
 
    else
    {
         
        // Find slopes of the lines
        $m1 = (int)(($y2 - $y1) / ($x2 - $x1));
        $m2 = (int)(($y4 - $y3) / ($x4 - $x3));
 
        // Check if their product is -1
        if ($m1 * $m2 == -1)
            return true;
        else
            return false;
    }
}
 
// Driver code
$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");
 
// This code is contributed by chandan_jnu
?>

Javascript

<script>
    // Javascript implementation of above approach
     
    // Function to check if two straight
    // lines are orthogonal or not
    function checkOrtho(x1, y1, x2, y2, x3, y3, x4, y4)
    {
   
        let m1, m2;
       
        // Both lines have infinite slope
        if (x2 - x1 == 0 && x4 - x3 == 0)
            return false;
   
        // Only line 1 has infinite slope
        else if (x2 - x1 == 0)
        {
            m2 = parseInt((y4 - y3) / (x4 - x3), 10);
            if (m2 == 0)
                return true;
            else
                return false;
        }
   
        // Only line 2 has infinite slope
        else if (x4 - x3 == 0)
        {
            m1 = parseInt((y2 - y1) / (x2 - x1), 10);
            if (m1 == 0)
                return true;
            else
                return false;
        }
   
        else
        {
            // Find slopes of the lines
            m1 = parseInt((y2 - y1) / (x2 - x1), 10);
            m2 = parseInt((y4 - y3) / (x4 - x3), 10);
   
            // Check if their product is -1
            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>
Producción: 

Yes

 

Complejidad de tiempo: O(1)

Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por saurabh_shukla y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *