📜  检查三个直线是否同时存在

📅  最后修改于: 2021-04-26 09:57:48             🧑  作者: Mango

给定三行方程,
a 1 x + b 1 y + c 1 = 0
a 2 x + b 2 y + c 2 = 0
a 3 x + b 3 y + c 3 = 0

任务是检查给定的三行是否并发。如果三个直线通过一个点,即它们在一个点处相遇,则被认为是并发的。

例子:

Input : a1 = 2, b1 = -3, c1 = 5
        a2 = 3, b2 = 4, c2 = -7
        a3 = 9, b3 = -5, c3 = 8
Output : Yes

Input : a1 = 2, b1 = -3, c1 = 5
        a2 = 3, b2 = 4, c2 = -7
        a3 = 9, b3 = -5, c3 = 4
Output : No


a 1 x + b 1 y + c 1 = 0………。 (1)
a 2 x + b 2 y + c 2 = 0…………。 (2)
a 3 x + b 3 y + c 3 = 0…………。 (3)

假设等式(i)和(ii)在(x 1 ,y 1 )处交集。然后(x 1 ,y 1 )将满足这两个方程。
因此,使用交叉乘法求解(i)和(ii),我们得到,
(x 1 / b 1 c 2 – b 2 c 1 )=(y 1 / c 1 a 2 – c 2 a 1 )=(1 / a 1 b 2 – a 2 b 1 )

所以,
x 1 =(b 1 c 2 – b 2 c 1 / a 1 b 2 – a 2 b 1 )并且
y 1 =(c 1 a 2 – c 2 a 1 / a 1 b 2 – a 2 b 1 ),a 1 b 2 – a 2 b 1 != 0

因此,线(i)和(ii)的相交点的所需余数为
(b 1 c 2 – b 2 c 1 / a 1 b 2 – a 2 b 1 ,c 1 a 2 – c 2 a 1 / a 1 b 2 – a 2 b 1 )

对于要同时存在的三行,(x 1 ,y 1 )也必须满足方程式(iii)。
所以,
a 3 x + b 3 y + c 3 = 0
=> a 3 (b 1 c 2 – b 2 c 1 / a 1 b 2 – a 2 b 1 )+ b 3 (c 1 a 2 – c 2 a 1 / a 1 b 2 – a 2 b 1 )+ c 3 = 0
=> a 3 (b 1 c 2 – b 2 c 1 )+ b 3 (c 1 a 2 – c 2 a 1 )+ c 3 (a 1 b 2 – a 2 b 1 )= 0

因此,我们只需要检查以上条件是否满足。

以下是此方法的实现:

C++
// CPP Program to check if three straight 
// line are concurrent or not
#include 
using namespace std;
  
// Return true if three line are concurrent,
// else false.
bool checkConcurrent(int a1, int b1, int c1, 
                      int a2, int b2, int c2, 
                      int a3, int b3, int c3)
{
    return (a3 * (b1 * c2 - b2 * c1) + 
            b3 * (c1 * a2 - c2 * a1) +  
            c3 * (a1 * b2 - a2 * b1) == 0);
}
  
// Driven Program
int main()
{
    int a1 = 2, b1 = -3, c1 = 5;
    int a2 = 3, b2 = 4, c2 = -7;
    int a3 = 9, b3 = -5, c3 = 8;
  
    (checkConcurrent(a1, b1, c1, a2, b2, c2, 
     a3, b3, c3) ? (cout << "Yes") : (cout << "No"));
    return 0;
}


Java
// Java Program to check if three straight 
// line are concurrent or no
import java.io.*;
  
class GFG {
  
    // Return true if three line are concurrent,
    // else false.
    static boolean checkConcurrent(int a1, int b1,
                   int c1, int a2, int b2, int c2, 
                           int a3, int b3, int c3)
    {
        return (a3 * (b1 * c2 - b2 * c1) + 
                b3 * (c1 * a2 - c2 * a1) + 
                c3 * (a1 * b2 - a2 * b1) == 0);
    }
      
    // Driven Program
    public static void main (String[] args) 
    {
        int a1 = 2, b1 = -3, c1 = 5;
        int a2 = 3, b2 = 4, c2 = -7;
        int a3 = 9, b3 = -5, c3 = 8;
      
        if(checkConcurrent(a1, b1, c1, a2, b2,
                               c2, a3, b3, c3)) 
            System.out.println( "Yes");
        else
            System.out.println( "No");
    }
}
  
// This code is contributed by anuj_67.


Python 3
# Python3 Program to check if three straight 
# line are concurrent or not
  
# Return true if three line are concurrent,
# else false.
def checkConcurrent(a1, b1, c1, a2, b2, c2,
                                 a3, b3, c3):
  
    return (a3 * (b1 * c2 - b2 * c1) +
            b3 * (c1 * a2 - c2 * a1) +
            c3 * (a1 * b2 - a2 * b1) == 0)
  
  
# Driven Program
a1 = 2
b1 = -3
c1 = 5
a2 = 3
b2 = 4
c2 = -7
a3 = 9
b3 = -5
c3 = 8
  
if(checkConcurrent(a1, b1, c1, a2, b2, c2,
                               a3, b3, c3)):
    print("Yes")
else:
    print("No")
  
# This code is contributed by Smitha


C#
// C# Program to check if three straight 
// line are concurrent or no
using System;
  
class GFG {
  
    // Return true if three line are concurrent,
    // else false.
    static bool checkConcurrent(int a1, int b1,
                int c1, int a2, int b2, int c2, 
                        int a3, int b3, int c3)
    {
        return (a3 * (b1 * c2 - b2 * c1) + 
                b3 * (c1 * a2 - c2 * a1) + 
                c3 * (a1 * b2 - a2 * b1) == 0);
    }
      
    // Driven Program
    public static void Main () 
    {
        int a1 = 2, b1 = -3, c1 = 5;
        int a2 = 3, b2 = 4, c2 = -7;
        int a3 = 9, b3 = -5, c3 = 8;
      
        if(checkConcurrent(a1, b1, c1, a2, b2,
                            c2, a3, b3, c3)) 
            Console.WriteLine( "Yes");
        else
            Console.WriteLine( "No");
    }
}
  
// This code is contributed by anuj_67.


PHP


输出:
Yes