Java Program to Check Orientation of 3 Given Ordered Points

In the previous article, we have discussed about Java Program to Count Integral Points Inside a Triangle

In this article we are going to see how to check orientation of 3 ordered points using Java programming language.

Java Program to Check Orientation of 3 Given Ordered Points

Before Jumping into the program directly let’s see how to check orientation of 3 ordered points.

Explanation:

Lets take 3 points P(x1,y1) , Q(x2,y2), R(x3,y3)

Now,

Slope of line segment (P,Q) = S1 = (y2 - y1)/(x2 - x1)

Slope of line segment (Q,R) = S2 = (y3 - y2)/(x3 - x2)

If  S1 > S2, then orientation is clockwise

If  S1 < S2, then orientation is counter-clockwise

Using above values of S1 and S2, it is clear that, the orientation depends on sign of  below expression:

Value = (y2 - y1)*(x3 - x2) - (y3 - y2)*(x2 - x1)

  • If value = 0, then orientation is co-linear
  • If value > 0, then orientation is clockwise
  • If value < 0, then orientation is counter-clockwise

Example:

P = (1,2)
Q = (3,4)
R = (5,6)

Value = (y2 - y1)*(x3 - x2) - (y3 - y2)*(x2 - x1) = 0

Since value = 0, hence orientation is co-linear.

Let’s see different ways to check orientation of 3 given ordered points.

Method-1: Java Program to Check Orientation of 3 Given Ordered Points By Using Static Value

Approach:

  • Declare an double variable say ‘x1’ and assign the value to it, which holds the x coordinate of point P
  • Declare an double variable say ‘y1’ and assign the value to it, which holds the y coordinate of point P
  • Declare an double variable say ‘x2’ and assign the value to it, which holds the x coordinate of point Q
  • Declare an double variable say ‘y2’ and assign the value to it, which holds the y coordinate of point Q
  • Declare an double variable say ‘x3’ and assign the value to it, which holds the x coordinate of point R
  • Declare an double variable say ‘y3’ and assign the value to it, which holds the y coordinate of point R
  • Find the orientation using the formula (y2 - y1)*(x3 - x2) - (y3 - y2)*(x2 - x1)
  • If value = 0, then orientation is co-linear OR If value > 0, then orientation is clockwise OR If value < 0, then orientation is counter-clockwise.
  • Print the result.

Program:

import java.util.*;
public class Main
{
   public static void main(String[] args)
   {
       //coordinates declared
      double x1 = 1;
      double y1 = 2;
      double x2 = 3;
      double y2 = 4;
      double x3 = 5;
      double y3 = 6;
        // relation between the slopes of PQ, QR
        double value = (y2 - y1)*(x3 - x2) - (y3 - y2)*(x2 - x1);
        // checking condition for orientation
        if(value == 0)
            System.out.println("Point P,Q,R are co-linear");
        else if(value > 0)
            System.out.println("Point P,Q,R are clockwise");
        else
            System.out.println("Point P,Q,R are counter-clockwise");
   }
}
Output:

Point P,Q,R are co-linear

Method-2: Java Program to Check Orientation of 3 Given Ordered Points By Using User Input Value

Approach:

  • Declare an double variable say ‘x1’ which holds the x coordinate of point P
  • Declare an double variable say ‘y1’ which holds the y coordinate of point P
  • Declare an double variable say ‘x2’ which holds the x coordinate of point Q
  • Declare an double variable say ‘y2’ which holds the y coordinate of point Q
  • Declare an double variable say ‘x3’ which holds the x coordinate of point R
  • Declare an double variable say ‘y3’ which holds the y coordinate of point R
  • Then we will take the value of “x1”, “y1”, “x2”, “y2”, “x3”, “y3” as user input using scanner class.
  • Find the orientation using the formula (y2 - y1)*(x3 - x2) - (y3 - y2)*(x2 - x1)
  • If value = 0, then orientation is co-linear OR If value > 0, then orientation is clockwise OR If value < 0, then orientation is counter-clockwise.
  • Print the result.

Program:

import java.util.*;
public class Main
{
   public static void main(String[] args)
   {
       //Scanner class objectv created
      Scanner s = new Scanner(System.in);
      //Taking input of points
      System.out.println("Enter the x coordinate of point P: ");
      double x1 = s.nextDouble();
      System.out.println("Enter the y coordinate of point P: ");
      double y1 = s.nextDouble();
      System.out.println("Enter the x coordinate of point Q: ");
      double x2 = s.nextDouble();
      System.out.println("Enter the y coordinate of point Q: ");
      double y2 = s.nextDouble();
      System.out.println("Enter the x coordinate of point R: ");
      double x3 = s.nextDouble();
      System.out.println("Enter the y coordinate of point R: ");
      double y3 = s.nextDouble();

        // relation between the slopes of PQ, QR
        double value = (y2 - y1)*(x3 - x2) - (y3 - y2)*(x2 - x1);
        // checking condition for orientation
        if(value == 0)
            System.out.println("Point P,Q,R are co-linear");
        else if(value > 0)
            System.out.println("Point P,Q,R are clockwise");
        else
            System.out.println("Point P,Q,R are counter-clockwise");
   }
}
Output:

Enter the x coordinate of point P: 
0
Enter the y coordinate of point P: 
0
Enter the x coordinate of point Q: 
4
Enter the y coordinate of point Q: 
4
Enter the x coordinate of point R: 
1
Enter the y coordinate of point R: 
2
Point P,Q,R are counter-clockwise

Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.

Related Java Programs: