Java Program to Find Line Angle from Two Point

In the previous article, we have discussed about Java Program to Check if Two Points are on the Same Side

In this article we are going to see how to find line angle from two point by using Java programming language.

Java Program to Find Line Angle from Two Point

Before Jumping into the program directly let’s see how to find line angle from two point.

Suppose there are 2 point having coordinates x1,y1 and x2,y2

The angle of the line between (x1,y1) and (x2,y2) is the same as the angle of the line between (x2−x1,y2−y1)(x2−x1,y2−y1) and the origin.

So the line angle is θ=tan-1((y2−y1)/(x2−x1))

Let’s see different ways to find line angle from two point.

Method-1: Java Program to Find Line Angle from Two Point 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.
  • Find the line angle using the formula tan-1((y2−y1)/(x2−x1))
  • 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;
      // formula to find slope
       double m = (y2-y1)/(x2-x1); 
       // formula to find the line angle
       double a = Math.atan(m); 
        System.out.println("the line angle of 2 points is " + a);  
   }
}
Output:

the line angle of 2 points is -1.2490457723982544

Method-2: Java Program to Find Line Angle from Two Point 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.
  • Then we will take the value of “x1”, “y1”, “x2”, “y2” as user input using scanner class.
  • Check the condition using the formula tan-1((y2−y1)/(x2−x1))
  • Print the result.

Program:

import java.util.*;
public class Main
{
   public static void main(String[] args)
   {
       // Create a Scanner object
       Scanner s = new Scanner(System.in);
       System.out.println("Enter the x coordinate of 1st point");
      // Read user input
      double x1 = s.nextDouble();
      System.out.println("Enter the y coordinate of 1st point");
      // Read user input
      double y1 = s.nextDouble();
      System.out.println("Enter the x coordinate of 2nd point");
      // Read user input
      double x2 = s.nextDouble();
      System.out.println("Enter the y coordinate of 2nd point");
     // Read user input
      double y2 = s.nextDouble();
       // formula to find slope
       double m = (y2-y1)/(x2-x1); 
       // formula to find the line angle
       double a = Math.atan(m); 
      System.out.println("the line angle of 2 points is " + a);  
   }
}
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
the line angle of 2 points is -1.2490457723982544

The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.

Related Java Programs: