Java Program to Find Type of Triangle from given Coordinates

In the previous article, we have discussed about Java Program to Check if Point Lies in Rectangle

In this article we are going to see how to find type of triangle from given coordinates using Java programming language.

Java Program to Find Type of Triangle from given Coordinates

Before Jumping into the program directly let’s see how to find type of triangle from given coordinates.

Suppose the 3 coordinates of a triangle are given as Q(x1,y1), R(x2,y2) P(x3,y3)

Now, we need to classify this triangle on the basis of sides and angle between them.

Distance formula for 2 point = sqrt((x2-x1)2 + (y2-y1)2)

For sides:

If all 3 sides are equal then equilateral triangle

If any 2 sides are equal then isosceles triangle

If no sides are equal then scalene triangle

For Angles: (by Pythagoras theorem)

if sum of square of 2 sides = square of the 3rd side, then right angle triangle

if sum of square of 2 sides < square of the 3rd side, then acute angle triangle

if sum of square of 2 sides > square of the 3rd side, then obtuse angle triangle

Example:

X1 = 0, y1 = 0

x2 = 10, y2 = 8

X3 = 1, y3 = 5

Scalene triangle

Acute angle triangle

Method-1: Java Program to Find Type of Triangle from given Coordinates By Using Static Input Value

Approach:

  • Declare an int variable say ‘x1’ and assign the value to it, which holds the x coordinate of point Q.
  • Declare an int variable say ‘y1’ and assign the value to it, which holds the y coordinate of point Q.
  • Declare an int variable say ‘x2’ and assign the value to it, which holds the x coordinate of point R.
  • Declare an int variable say ‘y2’ and assign the value to it, which holds the y coordinate of point R.
  • Declare an int variable say ‘x3’ and assign the value to it, which holds the x coordinate of point P.
  • Declare an int variable say ‘y3’ and assign the value to it, which holds the y coordinate of point P.
  • Check the condition using the 2point distance formula and Pythagoras theorem.
  • Print the result.

Program:

public class Main
{
   public static void main(String[] args)
   {
   int x1 = 0;
   int y1 = 0;
   int x2 = 10;
   int y2 = 8;
   int x3 = 1;
   int y3 = 5;
       // formula to find distance between 2 points
       double a = Math.sqrt(Math.pow(x2-x1,2)+Math.pow(y2-y1,2));
       double b = Math.sqrt(Math.pow(x3-x2,2)+Math.pow(y3-y2,2));
       double c = Math.sqrt(Math.pow(x3-x1,2)+Math.pow(y3-y1,2));
       // side checking 
       if (a == b && b == c)
    	   System.out.println("Equilateral triangle");
       else if (a == b || b == c)
    	   System.out.println("Isosceles triangle");
       else
    	   System.out.println("Scalene triangle");
       // angle checking using Pythagoras theorem
       if (a + b > c)
    	   System.out.println("Acute angle triangle"); 
       else if (a + b == c)
    	   System.out.println("Right angle triangle");
       else
    	   System.out.println("Obtuse angle triangle"); 
   }
}

Output:

Scalene triangle
Acute angle triangle

Method-2: Java Program to Find Type of Triangle from given Coordinates By Using User Input Value

Approach:

  • Declare an int variable say ‘x1’ which holds the x coordinate of point Q.
  • Declare an int variable say ‘y1’ which holds the y coordinate of point Q.
  • Declare an int variable say ‘x2’ which holds the x coordinate of point R.
  • Declare an int variable say ‘y2’ which holds the y coordinate of point R.
  • Declare an int variable say ‘x3’ which holds the x coordinate of point P.
  • Declare an int variable say ‘y3’ which holds the y coordinate of point P.
  • Then we will take the value of “x1”, “y1”, “x2”, “y2”, “x3”, “y3” as user input using scanner class.
  • Check the condition using the formula the 2point distance formula and Pythagoras theorem.
  • 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 Q");
      // Read user input
      double x1 = s.nextDouble();
      System.out.println("Enter the y coordinate of 1st point Q");
      double y1 = s.nextDouble();
      System.out.println("Enter the x coordinate of 2nd point R");
      double x2 = s.nextDouble();
      System.out.println("Enter the y coordinate of 2nd point R");
      double y2 = s.nextDouble();
      System.out.println("Enter the x coordinate of 3rd point P");
      double x3 = s.nextDouble();
      System.out.println("Enter the y coordinate of 3rd point P");
      double y3 = s.nextDouble();
    // formula to find distance between 2 points
       double a = Math.sqrt(Math.pow(x2-x1,2)+Math.pow(y2-y1,2));
       double b = Math.sqrt(Math.pow(x3-x2,2)+Math.pow(y3-y2,2));
       double c = Math.sqrt(Math.pow(x3-x1,2)+Math.pow(y3-y1,2));
       // side checking 
       if (a == b && b == c)
    	   System.out.println("Equilateral triangle");
       else if (a == b || b == c)
    	   System.out.println("Isosceles triangle");
       else
    	   System.out.println("Scalene triangle");
       // angle checking using Pythagoras theorem
       if (a + b > c)
    	   System.out.println("Acute angle triangle"); 
       else if (a + b == c)
    	   System.out.println("Right angle triangle");
       else
    	   System.out.println("Obtuse angle triangle"); 
   }
}
Output:

Enter the x coordinate of 1st point Q
1
Enter the y coordinate of 1st point Q
2
Enter the x coordinate of 2nd point R
3
Enter the y coordinate of 2nd point R
4
Enter the x coordinate of 3rd point P
5
Enter the y coordinate of 3rd point P
6
Isosceles triangle
Right angle triangle

If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.

Related Java Programs: