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++
#include <bits/stdc++.h>
using namespace std;
bool pointsAreOnSameSideOfLine(int a, int b, int c,
int x1, int y1, int x2, int y2)
{
int fx1;
int fx2;
fx1 = a * x1 + b * y1 - c;
fx2 = a * x2 + b * y2 - c;
if ((fx1 * fx2) > 0)
return true;
return false;
}
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";
}
Java
import java.util.*;
class GFG
{
static boolean pointsAreOnSameSideOfLine(int a, int b,
int c, int x1,
int y1, int x2,
int y2)
{
int fx1;
int fx2;
fx1 = a * x1 + b * y1 - c;
fx2 = a * x2 + b * y2 - c;
if ((fx1 * fx2) > 0)
return true;
return false;
}
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");
}
}
Python3
def pointsAreOnSameSideOfLine(a, b, c, x1, y1, x2, y2):
fx1 = 0
fx2 = 0
fx1 = a * x1 + b * y1 - c
fx2 = a * x2 + b * y2 - c
if ((fx1 * fx2) > 0):
return True
return False
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")
C#
using System;
class GFG
{
static bool pointsAreOnSameSideOfLine(int a, int b,
int c, int x1,
int y1, int x2,
int y2)
{
int fx1;
int fx2;
fx1 = a * x1 + b * y1 - c;
fx2 = a * x2 + b * y2 - c;
if ((fx1 * fx2) > 0)
return true;
return false;
}
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");
}
}
Javascript
<script>
function pointsAreOnSameSideOfLine(a, b, c, x1, y1, x2, y2)
{
let fx1;
let fx2;
fx1 = a * x1 + b * y1 - c;
fx2 = a * x2 + b * y2 - c;
if ((fx1 * fx2) > 0)
return true;
return false;
}
let a = 1, b = 1, c = 1;
let x1 = 1, y1 = 1;
let x2 = 2, y2 = 1;
if (pointsAreOnSameSideOfLine(a, b, c, x1, y1, x2, y2))
document.write("Yes");
else
document.write("No");
</script>
Time complexity: O(1) because constant operations are done
Auxiliary space: O(1)
Last Updated :
09 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Add Your Comment
Please Login to comment...