Find the equation of the straight line passing through the given points

Given an array arr containing N coordinate points in a plane, the task is to check whether the coordinate points lie on a straight line or not. If they lie on a straight line then print Yes and also the equation of that line otherwise print No.
Example:
Input: arr[] = {{1, 1}, {2, 2}, {3, 3}}Â
Output:Â
Yes
1x- 1y=0Input: arr[] = {{0, 1}, {2, 0}}
Output:
Yes
2y+x-2 = 0Input: arr[] = {{1, 5}, {2, 2}, {4, 6}, {3, 5}}
Output: No
Approach: The idea is to find the equation of the line that can be formed using any one pair of points given in the array and if all other points satisfy the equation of the line formed using the pair of the points, then all these points together form a straight line. So, if all points satisfy the equation of the line, then print Yes followed by the equation of the line, otherwise print No.
Below is the implementation of the above approach:Â
C++
// C++ program for the above approachÂ
#include <bits/stdc++.h>using namespace std;Â
// Function to check if a straight line// can be formed using N pointsvoid isStraightLinePossibleEq(    vector<pair<int, int> > arr,    int n){    // First pair of point (x0, y0)    int x0 = arr[0].first;    int y0 = arr[0].second;Â
    // Second pair of point (x1, y1)    int x1 = arr[1].first;    int y1 = arr[1].second;Â
    int dx = x1 - x0,        dy = y1 - y0,        c = dy * x0 - dx * y0;Â
    // Loop to iterate over the points    // and check whether they satisfy    // the equation or not.    for (int i = 2; i < n; i++) {        int x = arr[i].first, y = arr[i].second;        if ((dx * y) - (dy * x) != c) {            cout << "No";            return;        }    }    cout << "Yes" << endl;    cout << dy << "x-" << dx         << "y=" << c << "\n";}Â
// Driver Codeint main(){    // Array of points    vector<pair<int, int> > arr        = { { 0, 0 }, { 1, 1 }, { 3, 3 }, { 2, 2 } };    int N = 2;Â
    // Function Call    isStraightLinePossibleEq(arr, N);    return 0;} |
Java
// Java program for the above approachimport java.util.*;Â
public class GFG{Â
// Function to check if a straight line// can be formed using N pointsstatic void isStraightLinePossibleEq(int arr[][], int n){Â Â Â Â // First pair of point (x0, y0)Â Â Â Â int x0 = arr[0][0];Â Â Â Â int y0 = arr[0][1];Â
    // Second pair of point (x1, y1)    int x1 = arr[1][0];    int y1 = arr[1][1];Â
    int dx = x1 - x0,        dy = y1 - y0,        c = dy * x0 - dx * y0;Â
    // Loop to iterate over the points    // and check whether they satisfy    // the equation or not.    for (int i = 2; i < n; i++) {        int x = arr[i][0], y = arr[i][1];        if ((dx * y) - (dy * x) != c) {            System.out.print("No");            return;        }    }    System.out.print("Yes" + "\n");    System.out.print(dy + "x-" + dx         + "y=" + c + "\n");}Â
// Driver Codepublic static void main(String args[]){         // Array of points    int arr[][] = {{ 0, 0 }, { 1, 1 }, { 3, 3 }, { 2, 2 }};    int N = 2;Â
    // Function Call    isStraightLinePossibleEq(arr, N);Â
}}// This code is contributed by Samim Hossain Mondal. |
Python3
# Python code for the above approachÂ
# Function to check if a straight line# can be formed using N pointsdef isStraightLinePossibleEq(arr, n):Â
    # First pair of point (x0, y0)    x0 = arr[0][0]    y0 = arr[0][1]Â
    # Second pair of point (x1, y1)    x1 = arr[1][0]    y1 = arr[1][1]Â
    dx = x1 - x0    dy = y1 - y0    c = dy * x0 - dx * y0Â
       # Loop to iterate over the points       # and check whether they satisfy       # the equation or not.    for i in range(2, n):        x = arr[i][0], y = arr[i][1]        if (dx * y) - (dy * x) != c:            print("No")            returnÂ
    print("Yes")    print(str(dy)+ "x" +"-" + str(dx)+ "y"+ "="+ str(c))Â
    # Driver CodeÂ
Â
    # Array of pointsarr = [[0, 0], [1, 1], [3, 3], [2, 2]]N = 2Â
# Function CallisStraightLinePossibleEq(arr, N)Â
# This code is contributed by Potta Lokesh |
C#
// C# program for the above approachusing System;using System.Collections;using System.Collections.Generic;Â
class GFG{Â
// Function to check if a straight line// can be formed using N pointsstatic void isStraightLinePossibleEq(int [,]arr, int n){Â Â Â Â // First pair of point (x0, y0)Â Â Â Â int x0 = arr[0, 0];Â Â Â Â int y0 = arr[0, 1];Â
    // Second pair of point (x1, y1)    int x1 = arr[1, 0];    int y1 = arr[1, 1];Â
    int dx = x1 - x0,        dy = y1 - y0,        c = dy * x0 - dx * y0;Â
    // Loop to iterate over the points    // and check whether they satisfy    // the equation or not.    for (int i = 2; i < n; i++) {        int x = arr[i, 0], y = arr[i, 1];        if ((dx * y) - (dy * x) != c) {            Console.Write("No");            return;        }    }    Console.Write("Yes" + "\n");    Console.Write(dy + "x-" + dx         + "y=" + c + "\n");}Â
// Driver Codepublic static void Main(){         // Array of points    int[,] arr = new int[4, 2] {{ 0, 0 }, { 1, 1 }, { 3, 3 }, { 2, 2 }};    int N = 2;Â
    // Function Call    isStraightLinePossibleEq(arr, N);Â
}}Â
// This code is contributed by Samim Hossain Mondal. |
Javascript
<script>Â Â Â Â // JavaScript program for the above approachÂ
    // Function to check if a straight line    // can be formed using N points    const isStraightLinePossibleEq = (arr, n) => {             // First pair of point (x0, y0)        let x0 = arr[0][0];        let y0 = arr[0][1];Â
        // Second pair of point (x1, y1)        let x1 = arr[1][0];        let y1 = arr[1][1];Â
        let dx = x1 - x0,            dy = y1 - y0,            c = dy * x0 - dx * y0;Â
        // Loop to iterate over the points        // and check whether they satisfy        // the equation or not.        for (let i = 2; i < n; i++) {            let x = arr[i][0], y = arr[i][1];            if ((dx * y) - (dy * x) != c) {                cout << "No";                return;            }        }        document.write("Yes<br/>");        document.write(`${dy}x-${dx}y=${c}<br/>`);    }Â
    // Driver CodeÂ
    // Array of points    let arr = [[0, 0], [1, 1], [3, 3], [2, 2]];    let N = 2;Â
    // Function Call    isStraightLinePossibleEq(arr, N);Â
    // This code is contributed by rakeshsahniÂ
</script> |
Â
Â
Yes 1x-1y=0
Â
Time Complexity: O(N)
Auxiliary Space: O(1)
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



