Check whether two points (x1, y1) and (x2, y2) lie on same side of a given line or not

Given three integers a, b and c which represents coefficients of the equation of a line a * x + b * y – c = 0. Given two integer points (x1, y1) and (x2, y2). The task is to determine whether the points (x1, y1) and (x2, y2) lie on the same side of the given line or not.

Examples:

Input : a = 1, b = 1, c = 1, x1 = 1, y1 = 1, x2 = 1, y2 = 2
Output : yes
On applying (x1, y1) and (x2, y2) on a * x + b * y – c, gives 1 and 2 respectively both of which have the same sign, hence both the points lie on same side of the line.



Input : a = 1, b = 1, c = 1, x1 = 1, y1 = 1, x2 = 0, y2 = 0
Output : no

Approach : Apply both the points on given line equation and check if the obtained values belong to same parity or not.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to check if two points 
// lie on the same side or not
#include <bits/stdc++.h>
using namespace std;
  
// Function to check if two points 
// lie on the same side or not
bool pointsAreOnSameSideOfLine(int a, int b, int c, 
                        int x1,    int y1, int x2, int y2)
{
    int fx1; // Variable to store a * x1 + b * y1 - c
    int fx2; // Variable to store a * x2 + b * y2 - c
  
    fx1 = a * x1 + b * y1 - c;
    fx2 = a * x2 + b * y2 - c;
  
    // If fx1 and fx2 have same sign
    if ((fx1 * fx2) > 0)
        return true;
  
    return false;
}
  
// Driver code
int main()
{
    int a = 1, b = 1, c = 1;
    int x1 = 1, y1 = 1;
    int x2 = 2, y2 = 1;
  
    if (pointsAreOnSameSideOfLine(a, b, c, x1, y1, x2, y2))
        cout << "Yes";
    else
        cout << "No";
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to check if two points 
// lie on the same side or not
import java.util.*;
  
class GFG
{
  
// Function to check if two points 
// lie on the same side or not
static boolean pointsAreOnSameSideOfLine(int a, int b, 
                                         int c, int x1, 
                                         int y1, int x2, 
                                         int y2)
{
    int fx1; // Variable to store a * x1 + b * y1 - c
    int fx2; // Variable to store a * x2 + b * y2 - c
  
    fx1 = a * x1 + b * y1 - c;
    fx2 = a * x2 + b * y2 - c;
  
    // If fx1 and fx2 have same sign
    if ((fx1 * fx2) > 0)
        return true;
  
    return false;
}
  
// Driver code
public static void main(String[] args)
{
    int a = 1, b = 1, c = 1;
    int x1 = 1, y1 = 1;
    int x2 = 2, y2 = 1;
  
    if (pointsAreOnSameSideOfLine(a, b, c, x1, y1, x2, y2))
        System.out.println("Yes");
    else
        System.out.println("No");
}
}
  
// This code is contributed by 29AjayKumar

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to check if two points
# lie on the same side or not
  
# Function to check if two points
# lie on the same side or not
def pointsAreOnSameSideOfLine(a, b, c, x1, y1, x2, y2):
    fx1 = 0 # Variable to store a * x1 + b * y1 - c
    fx2 = 0 # Variable to store a * x2 + b * y2 - c
  
    fx1 = a * x1 + b * y1 - c
    fx2 = a * x2 + b * y2 - c
  
    # If fx1 and fx2 have same sign
    if ((fx1 * fx2) > 0):
        return True
  
    return False
  
# Driver code
a, b, c = 1, 1, 1
x1, y1 = 1, 1
x2, y2 = 2, 1
  
if (pointsAreOnSameSideOfLine(a, b, c, 
                              x1, y1, x2, y2)):
    print("Yes")
else:
    print("No")
  
# This code is contributed by Mohit Kumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to check if two points 
// lie on the same side or not
using System;
class GFG
{
  
// Function to check if two points 
// lie on the same side or not
static bool pointsAreOnSameSideOfLine(int a, int b, 
                                      int c, int x1, 
                                      int y1, int x2, 
                                      int y2)
{
    int fx1; // Variable to store a * x1 + b * y1 - c
    int fx2; // Variable to store a * x2 + b * y2 - c
  
    fx1 = a * x1 + b * y1 - c;
    fx2 = a * x2 + b * y2 - c;
  
    // If fx1 and fx2 have same sign
    if ((fx1 * fx2) > 0)
        return true;
  
    return false;
}
  
// Driver code
public static void Main()
{
    int a = 1, b = 1, c = 1;
    int x1 = 1, y1 = 1;
    int x2 = 2, y2 = 1;
  
    if (pointsAreOnSameSideOfLine(a, b, c, x1, y1, x2, y2))
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
}
  
// This code is contributed by Code_Mech

chevron_right


Output:

Yes


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.