How to find the centroid of a triangle – Java Program to Find Centroid of a Triangle

How to find the centroid of a triangle: In the previous article, we have discussed about Java Program to Check Orientation of 3 Ordered Points

In this article we are going to see how to to find centroid of a triangle using Java programming language.

Java Program to Find Centroid of a Triangle

How to find centroid of triangle: Before Jumping into the program directly let’s see how to find centroid of a triangle.

Explanation:

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

Now,

Centroid of the triangle is given as:

X = (x1+x2+x3)/2

Y = (y1+y2+y3)/2

Example:

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

X = (x1+x2+x3)/2 = 4.5
Y = (y1+y2+y3)/2 = 6

Centroid of triangle PQR = (x,y) = (4.5, 6)

Let’s see different ways to find centroid of a triangle.

Method-1: Java Program to Find Centroid of a Triangle 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 centroid of the triangle using the formula X = (x1+x2+x3)/2 and Y = (y1+y2+y3)/2
  • Print the result.

Program:

import java.util.*;
public class Main
{
   public static void main(String[] args)
   {
       //points of the triangle are declared
       //point-1
      double x1 = 1;
      double y1 = 2;
      //point-2
      double x2 = 3;
      double y2 = 4;
      //point-3
      double x3 = 5;
      double y3 = 6;
      // formula to find centroid of the triangle
      double x = (x1+x2+x3)/2;
      double y = (y1+y2+y3)/2;
      System.out.println("The centroid of the triangle PQR is (" + x + "," + y + ")");
   }
}
Output:

The centroid of the triangle PQR is (4.5, 6.0)

Method-2: Java Program to Find Centroid of a Triangle By Using User Defined 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 centroid of the triangle using the formula X = (x1+x2+x3)/2 and Y = (y1+y2+y3)/2
  • Print the result.

Program:

import java.util.*;
public class Main
{
   public static void main(String[] args)
   {
      //Scanner class object created
      Scanner s = new Scanner(System.in);
      //taking input of 3 points of the triangle 
      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();

      // formula to find centroid of the triangle
      double x = (x1+x2+x3)/2;
      double y = (y1+y2+y3)/2;
      System.out.println("The centroid of the triangle PQR is (" + x + "," + y + ")");
   }
}
Output:

Enter the x coordinate of point P
2
Enter the y coordinate of point P
3
Enter the x coordinate of point Q
7
Enter the y coordinate of point Q
7
Enter the x coordinate of point R
4
Enter the y coordinate of point R
4
The centroid of the triangle PQR is (6.5,7.0)

Enhancing programming skills is very important no matter what language you have chosen. So, practice frequently with these simple java programs examples and excel in coding the complex logic.

Related Java Programs: