Home >Java >javaTutorial >How to check the direction of three ordered points in Java?

How to check the direction of three ordered points in Java?

PHPz
PHPzforward
2023-09-14 14:21:04866browse

How to check the direction of three ordered points in Java?

In this article, we will find the direction of 3 ordered points. Direction here refers to whether a given point forms a clockwise, counterclockwise, or collinear shape in space.

In the above figure, a, b, c are the three points to check the direction of the shape in space. We find the direction of three given points by calculating the slope.

Calculate the slope and calculate the direction of 3 ordered points.

Slope of line segment

Slope of line segment$(a,b):\theta=\left ( y_{b}-y_{a} \right )/\left ( x_{b} -x_ {a}\right )$

Slope of line segment $(b,c)$: $\phi=\left ( y_{c} -y_{b}\right )/(x_{c}-x_{b})$

Therefore, the direction depends on the following expression:

$$(y_{b}-y_{a})(x_{c}-x_{b})-(y_{c}-y_{b})(x_{b}-x_{a}) \:or\:(y2-y1)*(x3-x2)-(y3-y2)*(x2-x1)$$

That is, whether it is a positive number, a negative number or

  • If the expression is zero, then θ = φ. Therefore the directions are collinear.

  • If the expression is negative, then θ

  • If the expression is positive, then θ > φ. So the direction is clockwise.

let's start!

Show you some examples

Instance-1

is translated as:

Instance-1

Suppose the 3 ordered points are (0,3), (4,2), (3,1)

After checking the direction of 3 ordered points, the result will be:

The given 3 points are formed: clockwise

The Chinese translation of

Instance-2

is:

Instance-2

Suppose the 3 ordered points are (0,3), (1,2), (9,5)

After checking the direction of 3 ordered points, the result will be:

The given 3 points are formed: counterclockwise

Example 3

Suppose the 3 ordered points are (2,2), (3,3), (4,4)

After checking the direction of 3 ordered points, the result will be:

Given 3-point form: Linear

algorithm

Step 1 - Declare 3 ordered points.

Step 2 - Pass the three given points to the expression, i.e. (b.y - a.y) * (c.x - b.x) - (b.x - a.x) * (c.y - b.y).

Step 3 - Check linear, clockwise and counterclockwise conditions.

Step 4 - Print the results.

Multiple methods

We provide solutions in different ways.

  • Via static input

  • By using user-defined methods

Let’s look at the program and its output one by one.

Method 1: Use static input

In this method, first pass 3 points to the expression to check linear, clockwise and counterclockwise conditions. Then print the result to output.

Example

public class Main{
   //main method
   public static void main(String[] args){
      //Declaring variables
      int x1=0, y1=1;
      int x2=4, y2=3;
      int x3=3, y3=2;
      
      //expression to check for 3 ordered point
      int val = (y2 - y1) * (x3 - x2) - (x2 - x1) * (y3 - y2);
      
      // check for collinear
      if (val == 0){
         //printing collinear orientation
         System.out.print("The given 3 points form : Linear");
      }
      
      //check for clockwise
      else if(val > 0){
         
         //printing clockwise orientation
         System.out.print("The given 3 points form: Clockwise");
      } else {
        
         //printig counter clockwise orientation
         System.out.print("The given 3 points form: CounterClockwise");
      }
   }
}

Output

The given 3 points form: Clockwise

Method 2: Use user-defined methods

In this method, 3 points are first passed to the expression through a user-defined method to check linear, clockwise and counterclockwise conditions. Then print the result to output.

Example

public class Main {
   public static void main(String[] args){
      Point a = new Point(2, 2);
      Point b = new Point(3, 3);
      Point c = new Point(4, 4);
      
      //calling user defined method
      int o = orientation(a, b, c);
     
      //check for Linear orientation
      if (o==0)	
      
         //printing Linear orientation
         System.out.print("The given 3 points form : Linear");
     
      //check for Clockwise orientation
      else if (o == 1)
     
         //printing clockwise orientation
         System.out.print("The given 3 points form : Clockwise");
      else
      
         //printing counter clockwise orientation
         System.out.print("The given 3 points form : CounterClockwise");
   }

   // user defined method
   public static int orientation(Point a, Point b,	Point c){
     
     //expression to check for 3 ordered point
      int val = (b.y - a.y) * (c.x - b.x) - (b.x - a.x) * (c.y - b.y);
      
      // check for collinear
      if (val == 0) return 0; 
      
      // check for clock or counterclock wise
      return (val > 0)? 1: 2;
   }
}
class Point{
   int x, y;
   Point(int x,int y){
      this.x=x;
      this.y=y;
   }
}

Output

The given 3 points form : Linear

In this article, we explore how to determine the direction by checking the direction of 3 ordered points using the Java programming language.

The above is the detailed content of How to check the direction of three ordered points in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete