Java Program to Check if Two Points are on the Same Side

In the previous article, we have discussed about Java Program to Return -1 if Point is on Left and 1 if Point is on Right

In this article we are going to see how to check if two points are on the same side by using Java programming language.

Java Program to Check if Two Points are on the Same Side

Before jumping into the program directly let’s see how to check if two points are on the same side.

Suppose there is given 2 points having coordinates x1,y1 & x2,y2. So from 2 points we can define line equation as

Y = mX + C

Where m = (y2-y1)/(x2-x1)

C = Y – mX (lets put Y = y1 and X = x1)

C = y1 – (m*x1)

Now lets assume there is another 2 points P,Q with their x-y coordinates px,py and qx,qy

To check whether 2 points are on same side or in opposite side

if ((py >= m * px + c AND qy >= m * qx + c) OR (py <= m * px + c AND qy <= m *qx + c))

Then we can say point P, Q are on same side, Else point P, Q are on opposite side.

Example:

x1 = 0, y1 = 5, x2 = 1, y2 = 2

px = -2, py = 11, qx = -1, qy = 8

m = (y2-y1)/(x2-x1) = -3

c = y2 – (m*x2) = 5

(py >= m * px + c AND qy >= m * qx + c) OR (py <= m * px + c AND qy <= m *qx + c)

(11 >= 11 And 8 >= 8) OR (11 <= 11 AND 8<= 8 )

(T AND T) OR (T AND T)

T OR T

T

Hence, 2 points lies on the same side.

Let’s see different ways to check if two points are on the same side or not.

Method-1: Java Program to Check if Two Points are on the Same Side By Using Static Input Value

Approach:

  • Declare an double variable say ‘x1’ and assign the value to it, which holds the x coordinate of 1st point
  • Declare an double variable say ‘y1’ and assign the value to it, which holds the y coordinate of 1st point
  • Declare an double variable say ‘x2’ and assign the value to it, which holds the x coordinate of 2nd point
  • Declare an double variable say ‘y2’ and assign the value to it, which holds the y coordinate of 2nd point
  • Declare an double variable say ‘px’ and assign the value to it, which holds the x coordinate of point P
  • Declare an double variable say ‘py’ and assign the value to it, which holds the y coordinate of point Q
  • Declare an double variable say ‘qx’ and assign the value to it, which holds the x coordinate of point Q
  • Declare an double variable say ‘qy’ and assign the value to it, which holds the y coordinate of point P
  • Check the condition using the formula (py >= m * px + c AND qy >= m * qx + c) OR (py <= m * px + c AND qy <= m *qx + c)
  • Print the result.

Program:

public class Main
{
   public static void main(String[] args)
   {
       int x1 = 0;
       int y1 = 5;
       int x2 = 1;
       int y2 = 2;
       int px = -2;
       int py = 11;
       int qx = -1;
       int qy = 8;
       double m = (y2-y1)/(x2-x1); // formula to find slope
       double c = y2 - (m*x2); // formula to find the constant of the line equation
       if ((py >= m * px + c && qy >= m * qx + c) || (py <= m * px + c && qy <= m *qx + c))
           System.out.println("points P, Q lies on same side");
       else
           System.out.println("points P, Q lies on opposite side");  
   }
}
Output:

points P, Q lies on same side

Method-2: Java Program to Check if Two Points are on the Same Side By Using User Input Value

Approach:

  • Declare an double variable say ‘x1’ which holds the x coordinate of 1st point
  • Declare an double variable say ‘y1’ which holds the y coordinate of 1st point
  • Declare an double variable say ‘x2’ which holds the x coordinate of 2nd point
  • Declare an double variable say ‘y2’ which holds the y coordinate of 2nd point
  • Declare an double variable say ‘px’ which holds the x coordinate of point P
  • Declare an double variable say ‘py’ which holds the y coordinate of point Q
  • Declare an double variable say ‘qx’ which holds the x coordinate of point Q
  • Declare an double variable say ‘qy’ which holds the y coordinate of point P
  • Then we will take the value of “x1”, “y1”, “x2”, “y2”, “px”, “py”, “qx”, “qy” as user input using scanner class.
  • Check the condition using the formula (py >= m * px + c AND qy >= m * qx + c) OR (py <= m * px + c AND qy <= m *qx + c)
  • Print the result.

Program:

import java.util.*;
public class Main 
{
   public static void main(String[] args)
   {
       Scanner s = new Scanner(System.in);
       System.out.println("Enter the x coordinate of 1st point");
      double x1 = s.nextDouble();
      System.out.println("Enter the y coordinate of 1st point");
      double y1 = s.nextDouble();
      System.out.println("Enter the x coordinate of 2nd point");
      double x2 = s.nextDouble();
      System.out.println("Enter the y coordinate of 2nd point");
      double y2 = s.nextDouble();
      System.out.println("Enter the x coordinate of point P");
      double px = s.nextDouble();
      System.out.println("Enter the y coordinate of point P");
      double py = s.nextDouble();
      System.out.println("Enter the x coordinate of point Q");
      double qx = s.nextDouble();
      System.out.println("Enter the y coordinate of point Q");
      double qy = s.nextDouble();
       double m = (y2-y1)/(x2-x1);
       double c = y2 - (m*x2);
       if ((py >= m * px + c && qy >= m * qx + c) || (py <= m * px + c && qy <= m *qx + c))
           System.out.println("points P, Q lies on same side");
       else
           System.out.println("points P, Q lies on opposite side");
   }
}
Output:

Enter the x coordinate of 1st point
0
Enter the y coordinate of 1st point
5
Enter the x coordinate of 2nd point
1
Enter the y coordinate of 2nd point
2
Enter the x coordinate of point P
-1
Enter the y coordinate of point P
8
Enter the x coordinate of point Q
-2
Enter the y coordinate of point Q
11
points P, Q lies on same side

Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples

Related Java Programs: