How to calculate area of triangle in Java - Program

Writing a Java program to calculate the area of a triangle is one of the basic programming exercises to develop coding sense on beginner programmers. Like many mathematical conceptual programs e.g. square root, factorial, or prime number this also serves a good exercise for beginners. Now, if you remember in maths you might have seen two main ways to calculate the area of a triangle, using vertices and using base and height. In this program, I have created two methods to calculate the area of a triangle using both ways. In the first method area(Point a, Point b, Point c)  we expect coordinates of three vertices of triangle and then we calculate area of triangle using the formula (Ax(By -Cy) + Bx(Cy -Ay) + Cx(Ay - By))/2, while in second method, area(int base, int height) we expect value of base and height and then we calculate are of triangle using formula (base * height) / 2.



Calculating area of triangle using 3 points

In this program, you need to write method which accepts three points, you can create a Point class to encapsulate both X and Y co-ordinates and then calculate the area of triangle using following formula:

Area of triangle =  (Ax(By -Cy) + Bx(Cy -Ay) + Cx(Ay - By))/2

Here A, B,and C are three vertices of the triangle and x and y represent coordinates. 




Calculating area of triangle using base and height

This one is easier to remember as you might have used this formula lot many times before. In this part of the program, you write a method which expects two integer values to capture base and height and return a float which is the area of a triangle.

We'll use the following formula:

Area of triangle = (base * height) / 2

Where base is the length of base and height is the height of triangle as shown in the following diagram:

Java Program to calculate area of triangle


Now that you know the formulas to calculate the area of a triangle in Java, we'll see the code which implements these formulas. Remember, you can use these formulas to calculate the area of any type of triangle e.g. right angle triangle, equilateral triangle etc. These are generic formulas and work for all types of triangle.



Java Program to calculate the area of a triangle

Here is our complete Java program to find the area of a triangle given base and height or points of three vertices. I have created a Point class to represent a point which has both X and Y co-ordinates and two overloaded area() methods to calculate the area of a triangle. The first area() method expects three parameters, which are points of three vertices and then it return a float value which is the area of a triangle. The second area() method takes base and height and return a float value which is the area of a triangle.

How to calculate area of triangle in Java - Program




/*
 * Java Program to calculate area of triangle using co-ordinates of vertices
 * or by using base and height. 
 */

public class Main {

  public static void main(String[] args) {
    Point A = new Point(13, 34);
    Point B = new Point(22, 21);
    Point C = new Point(11, 19);
    System.out.println("area of triangle using formula 1: "
        + Triangle.area(A, B, C));
    System.out.println("area of triangle using formula 2: "
        + Triangle.area(3, 5));

  }

}

class Point {
  int x;
  int y;

  public Point(int x, int y) {
    this.x = x;
    this.y = y;
  }

}

class Triangle {

  /**
   * Java method to return area of triangle using vertices as per following
   * formula area = (Ax(By -Cy) + Bx(Cy -Ay) + Cx(Ay - By))/2
   * 
   * @return
   */
  public static float area(Point A, Point B, Point C) {
    float area = (A.x * (B.y - C.y) + B.x * (C.y - A.y) + C.x * (A.y - B.y)) / 2.0f;
    return Math.abs(area);
  }

  /**
   * 
   * @param base
   * @param height
   * @return
   */
  public static float area(int base, int height) {
    return (base * height) / 2.0f;
  }
}

Output
area of triangle using formula 1: 80.5
area of triangle using formula 2: 7.5


That's all about how to calculate the area of a triangle in Java. This is a good exercise to learn to program along with many others which I have shared below. We have learned both ways to calculate the area of a triangle in program i.e. using 3 points of vertices as well as by using the base and height formula. You can further read Concrete Mathematics: A Foundation for Computer Science to learn more about how mathematics plays an important role in Computer Science and programming.


Othe Java Programs you may like to practice


Reference
Area of Triangle 

No comments:

Post a Comment