Check if three straight lines are concurrent or not - GeeksforGeeks

05 May, 2021
Given three lines equation, a1x + b1y + c1 = 0 a2x + b2y + c2 = 0 a3x + b3y + c3 = 0The task is to check whether the given three lines are concurrent or not. Three straight lines are said to be concurrent if they pass through a point i.e., they meet at a point.Examples:  
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
Let a1x + b1y + c1 = 0 .......... (1) a2x + b2y + c2 = 0 .......... (2) a3x + b3y + c3 = 0 .......... (3)Suppose the eqn (i) and (ii) intersects at (x1, y1). Then (x1, y1) will satisfy both the equations. Therefore, solving (i) and (ii) using method of cross-multiplication, we get, (x1/b1c2 – b2c1) = (y1/c1a2 – c2a1) = (1/a1b2 – a2b1)Therefore, x1 = (b1c2 – b2c1/a1b2 – a2b1) and y1 = (c1a2 – c2a1/a1b2 – a2b1), a1b2 – a2b1 != 0Therefore, the required coordinates of the point of intersection of the lines (i) and (ii) are (b1c2 – b2c1/a1b2 – a2b1, c1a2 – c2a1/a1b2 – a2b1)For, three of line to be concurrent, (x1, y1) must satisfy the equation (iii) as well. So, a3x + b3y + c3 = 0 => a3(b1c2 – b2c1/a1b2 – a2b1) + b3(c1a2 – c2a1/a1b2 – a2b1) + c3 = 0 => a3(b1c2 – b2c1) + b3(c1a2 – c2a1) + c3(a1b2 – a2b1) = 0So, we only need to check if above condition satisfy or not.Below is the implementation of this approach:  
C++
Java
Python 3
C#
PHP
Javascript
// CPP Program to check if three straight// line are concurrent or not#include <bits/stdc++.h>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 Programint 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 Program to check if three straight// line are concurrent or noimport 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.
# 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 Programa1 = 2b1 = -3c1 = 5a2 = 3b2 = 4c2 = -7a3 = 9b3 = -5c3 = 8 if(checkConcurrent(a1, b1, c1, a2, b2, c2,                               a3, b3, c3)):    print("Yes")else:    print("No") # This code is contributed by Smitha
// C# Program to check if three straight// line are concurrent or nousing 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// PHP Program to check if three straight// line are concurrent or not // Return true if three line are// concurrent, else false.function 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);}       // Driver Code    $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))        echo "Yes";    else        echo "No"; // This code is contributed by anuj_67.?>
<script> // Javascript Program to check if three straight// line are concurrent or not // Return true if three line are concurrent,// else false.function 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 Programa1 = 2, b1 = -3, c1 = 5;a2 = 3, b2 = 4, c2 = -7;a3 = 9, b3 = -5, c3 = 8;(checkConcurrent(a1, b1, c1, a2, b2, c2, a3, b3, c3) ? (document.write( "Yes")) : (document.write( "No"))); </script>
Yes
 
vt_m
Smitha Dinesh Semwal
rutvik_56
Geometric
Mathematical
Mathematical
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Convex Hull using Divide and Conquer Algorithm
Orientation of 3 ordered points
Equation of circle when three points on the circle are given
Program to find slope of a line
Program to find line passing through 2 Points
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7