📜  检查三条直线是否并发

📅  最后修改于: 2021-10-23 09:00:58             🧑  作者: 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 ) + 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


Javascript


输出:
Yes