Comprueba si las dos rectas dadas son idénticas o no

Dadas dos rectas con coeficientes de sus ecuaciones a1x + b1y + c1 = 0 y a2x + b2y + c2 = 0 respectivamente, la tarea es comprobar si las rectas son idénticas o no. 
Ejemplos:
 

Entrada: a1 = -2, b1 = 4, c1 = 3, a2 = -6, b2 = 12, c2 = 9 
Salida: Las rectas dadas son idénticas
Entrada: a1 = 12, b1 = 3, c1 = 8, a2 = 7, b2 = -12, c2 = 0 
Salida: Las líneas rectas dadas no son idénticas

Enfoque
 

  1. Dadas las ecuaciones, 
    a1x + b1y + c1 = 0 
    a2x + b2y + c2 = 0 
     
  2. al convertirlos a la forma pendiente-intersección, obtenemos 
    y = (-a1/b1)x +(-c1/b1) 
    y = (-a2/b2)x +(-c2/b2) 
     
  3. ahora, si las líneas son idénticas, entonces la pendiente y las intersecciones deben ser iguales, 
    entonces, 
    -a1/b1 = -a2/b2 
    o, a1/a2 = b1/b2 
    también, 
    -c1/b1 = -c2/b2 
    entonces, c1 /c2 = b1/b2 
     
  4. Entonces, si dos líneas rectas dadas son idénticas, entonces sus coeficientes deberían ser proporcionales. 
    \frac{a_{1}}{a_{2}} = \frac{b_{1}}{b_{2}} = \frac{c_{1}}{c_{2}}
     

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

C++

// C++ program to check if
// given two straight lines
// are identical or not
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if they are identical
void idstrt(double a1, double b1,
            double c1, double a2,
            double b2, double c2)
{
    if ((a1 / a2 == b1 / b2)
        && (a1 / a2 == c1 / c2)
        && (b1 / b2 == c1 / c2))
        cout << "The given straight"
             << " lines are identical"
             << endl;
 
    else
        cout << "The given straight"
             << " lines are not identical"
             << endl;
}
 
// Driver Code
int main()
{
    double a1 = -2, b1 = 4,
           c1 = 3, a2 = -6,
           b2 = 12, c2 = 9;
    idstrt(a1, b1, c1, a2, b2, c2);
    return 0;
}

Java

// Java program to check if
// given two straight lines
// are identical or not
class GFG
{
 
// Function to check if they are identical
static void idstrt(double a1, double b1,
                    double c1, double a2,
                    double b2, double c2)
{
    if ((a1 / a2 == b1 / b2)
        && (a1 / a2 == c1 / c2)
        && (b1 / b2 == c1 / c2))
        System.out.println( "The given straight"
        +" lines are identical");
 
    else
        System.out.println("The given straight"
            + " lines are not identical");
}
 
// Driver Code
public static void main(String[] args)
{
    double a1 = -2, b1 = 4,
            c1 = 3, a2 = -6,
            b2 = 12, c2 = 9;
    idstrt(a1, b1, c1, a2, b2, c2);
}
}
 
// This code has been contributed by 29AjayKumar

Python3

# Python3 program to check if
# given two straight lines
# are identical or not
 
# Function to check if they are identical
def idstrt(a1, b1, c1, a2, b2, c2):
    if ((a1 // a2 == b1 // b2) and
        (a1 // a2 == c1 // c2) and
        (b1 // b2 == c1 // c2)):
        print("The given straight lines",
                         "are identical");
    else:
        print("The given straight lines",
                     "are not identical");
 
# Driver Code
a1, b1 = -2, 4
c1, a2 = 3,-6
b2, c2 = 12,9
idstrt(a1, b1, c1, a2, b2, c2)
 
# This code is contributed
# by mohit kumar

C#

// C# program to check if
// given two straight lines
// are identical or not
using System;
 
class GFG
{
 
// Function to check if they are identical
static void idstrt(double a1, double b1,
                    double c1, double a2,
                    double b2, double c2)
{
    if ((a1 / a2 == b1 / b2)
        && (a1 / a2 == c1 / c2)
        && (b1 / b2 == c1 / c2))
        Console.WriteLine( "The given straight"
        +" lines are identical");
 
    else
        Console.WriteLine("The given straight"
            + " lines are not identical");
}
 
// Driver Code
public static void Main(String[] args)
{
    double a1 = -2, b1 = 4,
            c1 = 3, a2 = -6,
            b2 = 12, c2 = 9;
    idstrt(a1, b1, c1, a2, b2, c2);
}
}
 
// This code contributed by Rajput-Ji

PHP

<?php
 
// PHP program to check if
// given two straight lines
// are identical or not
 
 
// Function to check if they are identical
function idstrt($a1, $b1,
            $c1, $a2,
            $b2, $c2)
{
    if (($a1 / $a2 == $b1 / $b2)
        && ($a1 / $a2 == $c1 / $c2)
        && ($b1 / $b2 == $c1 / $c2))
        echo "The given straight lines are identical","\n";
 
    else
        echo "The given straight lines are not identical","\n";
}
 
    // Driver Code
    $a1 = -2; $b1 = 4;
    $c1 = 3; $a2 = -6;
    $b2 = 12; $c2 = 9;
    idstrt($a1, $b1, $c1, $a2, $b2, $c2);
 
    // This code is contributed by Ryuga
 
?>

Javascript

<script>
 
// javascript program to check if
// given two straight lines
// are identical or not
 
 
// Function to check if they are identical
function idstrt(a1 , b1,
                    c1 , a2,
                    b2 , c2)
{
    if ((a1 / a2 == b1 / b2)
        && (a1 / a2 == c1 / c2)
        && (b1 / b2 == c1 / c2))
        document.write( "The given straight"
        +" lines are identical");
 
    else
        document.write("The given straight"
            + " lines are not identical");
}
 
// Driver Code
  
 
var a1 = -2, b1 = 4,
        c1 = 3, a2 = -6,
        b2 = 12, c2 = 9;
idstrt(a1, b1, c1, a2, b2, c2);
 
// This code contributed by Princi Singh
 
</script>
Producción: 

The given straight lines are identical

 

Complejidad de tiempo: O(1)

Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por IshwarGupta 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 *