# Java Program to Compute the Area of a Triangle Using Determinants

## Introduction

The Java program to compute the area of a triangle using determinants is a concise and efficient program that calculates the area of a triangle given the coordinates of its three vertices.

This program is useful for anyone studying or working with geometry, as it demonstrates how to use basic arithmetic and algebraic calculations in Java, and how to read in user input using the Scanner class. The program prompts the user to enter the coordinates of three points of the triangle, which are then read in and used to calculate the determinant of the matrix of coordinates. The absolute value of the determinant is used to ensure that the area is always positive, and the area of the triangle is then calculated using the formula and displayed to the user. This program can be easily modified to accept input in different formats or to perform additional calculations, making it a versatile tool for geometry calculations.

## Determinants

Determinants are a mathematical concept that are used to determine certain properties of a matrix. In linear algebra, a determinant is a scalar value that can be calculated from the elements of a square matrix. The determinant can be used to determine whether a matrix has an inverse, whether a system of linear equations has a unique solution, and the area or volume of a parallelogram or parallelepiped.

### Syntax

area = |determinant|/2


### Algorithm

• Import the Scanner class.

• Define a public class named TriangleArea.

• Inside the class, define a main method.

• Create a Scanner object to read user input.

• Prompt the user to enter the coordinates of three points separated by a space.

• Read the user input for the coordinates and store them in six double variables (x1, y1, x2, y2, x3, y3).

• Calculate the determinant of the matrix of coordinates using the formula −

• | x1 y1 1 |
| x2 y2 1 | = x1*y2 + x2*y3 + x3*y1 - y1*x2 - y2*x3 - y3*x1
| x3 y3 1 |

• Then we calculate the area of the triangle using the formula −

area = |determinant|/2


## Example 1

### Approach

• First, we prompt the user to enter the coordinates of three points of the triangle.

• We use the Scanner class to read in the user input for the coordinates and store them in six double variables (x1, y1, x2, y2, x3, y3).

• Next, we calculate the determinant of the matrix of coordinates using the formula −

| x1 y1 1 |
| x2 y2 1 | = x1*y2 + x2*y3 + x3*y1 - y1*x2 - y2*x3 - y3*x1
| x3 y3 1 |

• Then we calculate the area of the triangle using the formula −

area = |determinant|/2


Here is a Java program to compute the area of a triangle using determinants −

import java.util.Scanner;

public class TriangleArea {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// Prompt the user to enter the coordinates of three points
System.out.println("Enter the coordinates of three points separated by a space:");
double x1 = scanner.nextDouble();
double y1 = scanner.nextDouble();
double x2 = scanner.nextDouble();
double y2 = scanner.nextDouble();
double x3 = scanner.nextDouble();
double y3 = scanner.nextDouble();

// Compute the area of the triangle using determinants
double determinant = x1 * y2 + x2 * y3 + x3 * y1 - y1 * x2 - y2 * x3 - y3 * x1;
double area = Math.abs(determinant / 2);

// Display the area of the triangle
System.out.println("The area of the triangle is " + area);
}
}


### Explanation

Note that the Math.abs() function is used to ensure that the area is always positive, since the determinant can be negative if the vertices are listed in a counterclockwise order.

### Output

Enter the coordinates of three points separated by a space:
4 3
2 6
7 4
The area of the triangle is 5.5


## Example 2

This approach works for any triangle, regardless of its orientation or size. The program assumes that the user enters valid numeric coordinates for the three points, otherwise, it may throw an exception if the input is not valid.

Here is a Java program to compute the area of a triangle using determinants −

import java.util.Scanner;

public class TriangleArea {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the coordinates of the first point: ");
double x1 = sc.nextDouble();
double y1 = sc.nextDouble();

System.out.print("Enter the coordinates of the second point: ");
double x2 = sc.nextDouble();
double y2 = sc.nextDouble();

System.out.print("Enter the coordinates of the third point: ");
double x3 = sc.nextDouble();
double y3 = sc.nextDouble();

double area = calculateTriangleArea(x1, y1, x2, y2, x3, y3);

System.out.println("The area of the triangle is " + area);
}

public static double calculateTriangleArea(double x1, double y1, double x2, double y2, double x3, double y3) {
double determinant = x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2);
return Math.abs(determinant) / 2.0;
}
}


### Explanation

This program prompts the user to enter the coordinates of three points that form a triangle, and then uses the calculateTriangleArea() method to compute the area of the triangle using the determinant formula. Finally, it prints the computed area to the console.

### Output

Enter the coordinates of the first point: 0 0
Enter the coordinates of the second point: 4 0
Enter the coordinates of the third point: 0 3
The area of the triangle is 6.0


## Conclusion

The Java program to compute the area of a triangle using determinants is a simple and efficient way to calculate the area of a triangle given its coordinates. The program uses basic arithmetic and algebraic calculations to determine the determinant of a matrix of coordinates, and then uses this determinant to calculate the area of the triangle using a simple formula. The program demonstrates the use of the Scanner class for user input, the Math class for mathematical operations, and the use of methods for code organization and modularity.

The time complexity of the program is constant time, which means that it performs a fixed number of operations regardless of the size of the input. This makes it a fast and efficient program for calculating the area of a triangle. The space complexity of the program is also constant, as it only uses a fixed amount of memory to store its variables and does not require any additional memory allocation.

Updated on: 10-Apr-2023

98 Views 