Java Program to Find the Area of a Triangle using Determinants

This is a Java Program to find the area of a triangle using determinant method.
Formula for the area of a triangle using determinants

                    |x1 y1 1|
Area = ±1/2 |x2 y2 1|
                    |x3 y3 1|

The plus/minus in this case is meant to take whichever sign is needed so the answer is positive (non-negative). Do not say the area is both positive and negative.

Here is the source code of the Java Program to Compute the Area of a Triangle Using Determinants. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. //This is a java program to find the area of triangle using method of determinants
  2. import java.util.Random;
  3.  
  4. public class Area_Triangle_Determinants
  5. {
  6.     public static double determinant(double A[][], int N)
  7.     {
  8.         double det = 0;
  9.         if (N == 1)
  10.         {
  11.             det = A[0][0];
  12.         } else if (N == 2)
  13.         {
  14.             det = A[0][0] * A[1][1] - A[1][0] * A[0][1];
  15.         } else
  16.         {
  17.             det = 0;
  18.             for (int j1 = 0; j1 < N; j1++)
  19.             {
  20.                 double[][] m = new double[N - 1][];
  21.                 for (int k = 0; k < (N - 1); k++)
  22.                 {
  23.                     m[k] = new double[N - 1];
  24.                 }
  25.                 for (int i = 1; i < N; i++)
  26.                 {
  27.                     int j2 = 0;
  28.                     for (int j = 0; j < N; j++)
  29.                     {
  30.                         if (j == j1)
  31.                             continue;
  32.                         m[i - 1][j2] = A[i][j];
  33.                         j2++;
  34.                     }
  35.                 }
  36.                 det += Math.pow(-1.0, 1.0 + j1 + 1.0) * A[0][j1]
  37.                         * determinant(m, N - 1);
  38.             }
  39.         }
  40.         return det;
  41.     }
  42.  
  43.     public static void main(String args[])
  44.     {
  45.         Random random = new Random();
  46.         int x1, x2, x3, y1, y2, y3;
  47.         x1 = random.nextInt(10);
  48.         x2 = random.nextInt(10);
  49.         x3 = random.nextInt(10);
  50.         y1 = random.nextInt(10);
  51.         y2 = random.nextInt(10);
  52.         y3 = random.nextInt(10);
  53.  
  54.         double[][] mat = new double[3][3];
  55.         mat[0][0] = x1;
  56.         mat[0][1] = y1;
  57.         mat[0][2] = 1;
  58.         mat[1][0] = x2;
  59.         mat[1][1] = y2;
  60.         mat[1][2] = 1;
  61.         mat[2][0] = x3;
  62.         mat[2][1] = y3;
  63.         mat[2][2] = 1;
  64.  
  65.         System.out
  66.                 .println("The matrix formed by the coordinates of the triangle is: ");
  67.         for (int i = 0; i < 3; i++)
  68.         {
  69.             for (int j = 0; j < 3; j++)
  70.                 System.out.print(mat[i][j] + " ");
  71.             System.out.println();
  72.         }
  73.  
  74.         double det = determinant(mat, 3) * 0.5;
  75.         if (det < 0)
  76.             System.out.println("The Area of the triangle formed by (" + x1
  77.                     + "," + y1 + "), (" + x2 + "," + y2 + "), (" + x3 + ","
  78.                     + y3 + ") = " + (det * -1));
  79.         else
  80.             System.out.println("The Area of the triangle formed by (" + x1
  81.                     + "," + y1 + "), (" + x2 + "," + y2 + "), (" + x3 + ","
  82.                     + y3 + ") = " + det);
  83.     }
  84. }

Output:

advertisement
advertisement
$ javac Area_Triangle_Determinants.java
$ java Area_Triangle_Determinants
 
The matrix formed by the coordinates of the triangle is: 
3.0 4.0 1.0 
6.0 4.0 1.0 
3.0 9.0 1.0 
The Area of the triangle formed by (3,4), (6,4), (3,9) = 7.5

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.