JAVA Program to Check if a Point is On the Left or Right Side of a Line


A line consists of an infinite number of points. In two-dimensional coordinate system we can define every point with two values i.e. X and Y. A point basically situated on the left side or right side or it can be on the line itself and this can be defined only if we have the coordinates of it.

In this program we are going to use the cross-product method to find the direction of the point. Cross- Product method is used to find the third vector from two vectors by cross multiplying both the vectors. In our case if we cross product between the origin points of the line and the point which is given for finding the direction. Then either we get a positive value or a negative value or zero.

If we get a positive value then we can ensure that the point is situated on the right side of the line. If we get negative value then we can ensure that the point is situated on the left side of the line. If zero, then the point is laid somewhere on the line.

Formula to calculate the cross-product of two points A(x, y) and B(x, y)−

Cross-Product = [A(x) X B(y)] – [B(x) X A(y)]

In this article we will see how we can find whether the point is on the left side or on the right side of a line using Java.

To show you some instances −

Instance-1

The input point of line:
A(x, y) = (20, -20)
B(x, y) = (-30, -23)
The coordinate of the point:
P(x, y) = (4, 5) 
The coordinates of origin point and the observed points are:
O1(x, y) = [(B(x)-A(x)), (B(y)-A(y))] = (-50,-3)
O2(x, y) = [(P(x)-A(x)), (P(y)-A(y))] = (-16,-25)
The cross-product between the O1 and O2 = [O1(x) X O2(y)] – [O2(x) X O1(y)]
                                        =[-50 X -25] – [-16 X -3]
                                        = 1250 – 48 = 1202
                                        
As the output is positive, hence the point is situated on the right side of the line

Instance-2

The input point of line:
A(x, y) = (-2, 4)
B(x, y) = (7, 8)
The coordinate of the point:
P(x, y) = (10,-20)
The coordinates of origin point and the observed points are:
O1(x, y) = [(B(x)-A(x)), (B(y)-A(y))] = (9, 4)
O2(x, y) = [(P(x)-A(x)), (P(y)-A(y))] = (12, -24)
The cross-product between the O1 and O2 = [O1(x) X O2(y)] – [O2(x) X O1(y)]
                                       = [9 X -24] – [12 X 4]
                                       = -216 – 48 = -264
As the output is negative, hence the point is situated on the left side of the line.

Algorithm

Step-1 − Get the coordinates of two points of the line and the point either by user input or static input.

Step-2 − Find the origin between two points of the line as well as the origin between the one of the points of line and the point.

Step-3 − Then calculate the cross- product between two origin points.

Step-4 − If the result of the above calculation is positive or negative or zero then print the point is situated on right or left or on line respectively.

Multiple Approaches

We have provided the solution in different approaches.

  • By User Defined Method with Static Input Values.

  • By User Defined Method with User Input Values.

Let’s see the program along with its output one by one.

Approach-1: By Using User Defined Method with Static Input Value

In this approach, we declare the points of the line and the point co-ordinated by static input method and pass those values as an argument to our user-defined method. Then by using the algorithm inside the method we can find the direction of the point with respect to the line.

Example

public class Main{
   public static void main(String[] args) {
      pnt first = new pnt();
      pnt second = new pnt();
      pnt point = new pnt();
      first.x = -20;
      first.y = 15; 
      // first(-20, 15)
      second.x = 31;
      second.y = -18; 
      // second(31, -18)
      point.x = 32;
      point.y = 45; 
      // point(32, 45)
      int dir = dirOfPoint(first, second, point);
      if (dir == 1)
         System.out.println("The point is on Right Direction of the line.");
      else if (dir == -1)
         System.out.println("The point is on Left Direction of the line.");
      else
         System.out.println("Point is somewhere on the Line.");
   }
   static class pnt{
      int x, y;
   };
   static int R = 1, L = -1, Z = 0;
   static int dirOfPoint(pnt first,pnt second, pnt point) {
      second.x -= first.x;
      second.y -= first.y;
      point.x -= first.x;
      point.y -= first.y;
      int crs_prod = second.x * point.y - second.y * point.x;
      if (crs_prod > 0)
         return R;
      if (crs_prod < 0)
         return L;
         return Z;
   }
}

Output

The point is on Right Direction of the line.

Approach-2: By Using User Defined Method with User Input Value

In this approach, we declare the points of the line and the point co-ordinated by user input method and pass those values as an argument to our user-defined method. Then by using the algorithm inside the method we can find the direction of the point with respect to the line.

Example

import java.util.*;
public class Main{
   public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
      pnt first = new pnt();
      pnt second = new pnt();
      pnt point = new pnt();
      System.out.println("Enter the coordinates of first point of the line: ");
      System.out.println("--------------------------------------------------");
      System.out.print("X- value: ");
      first.x = sc.nextInt();
      System.out.print("Y- value: ");
      first.y = sc.nextInt();
      System.out.println("Enter the coordinates of second point of the line:");
      System.out.println("--------------------------------------------------");
      System.out.print("X value: ");
      second.x = sc.nextInt();
      System.out.print("Y value: ");
      second.y = -sc.nextInt();
      System.out.println("Enter the coordinates of the point which you want to navigate: ");
      System.out.println("--------------------------------------------------");
      System.out.print("X value: ");
      point.x = sc.nextInt();
      System.out.print("Y value: ");
      point.y = sc.nextInt();
      int dir = dirOfPoint(first, second, point);
      if (dir == 1)
         System.out.println("The point is on Right Direction of the line.");
      else if (dir == -1)
         System.out.println("The point is on Left Direction of the line.");
      else
         System.out.println("Point is somewhere on the Line.");
   }
   static class pnt{
      int x, y;
   };
   static int R = 1, L = -1, Z = 0;
   static int dirOfPoint(pnt first,pnt second, pnt point){
      second.x -= first.x;
      second.y -= first.y;
      point.x -= first.x;
      point.y -= first.y;
      int crs_prod = second.x * point.y - second.y * point.x;
      if (crs_prod > 0)
         return R;
      if (crs_prod < 0)
         return L;
         return Z;
   }
}

Output

Enter the coordinates of first point of the line:
---------------------------------------------------
X- value: 10
Y- value: 20
Enter the coordinates of second point of the line:
----------------------------------------------------
X value: -11
Y value: 12
Enter the coordinates of the point which you want to navigate:
----------------------------------------------------------------
X value: 4
Y value: 6
The point is on Right Direction of the line.

In this article, we explored how to check if a point is on the left or right side of a line in Java by using different approaches.

Updated on: 27-Dec-2022

553 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements