Java - Write program to calculate triangle area from coordinates

Requirement

  • Write a program that enters 3 points in the plane as integer x and y coordinates
  • Calculates and prints the area of the triangle composed by these 3 points.
  • Round the result to a whole number.
  • In case the three points do not form a triangle, print "0" as result.

Demo

public class Main {
  public static void main(String[] args) {
    int x1 = 1;//  ww w .j  a v a 2s  .com
    int y1 = 2;
    int x2 = 3;
    int y2 = 4;
    int x3 = 5;
    int y3 = 6;

    // find the distance between three points (sides of triangle)
    double aSide = Math.sqrt(Math.pow(x3 - x2, 2) + Math.pow(y3 - y2, 2));
    double bSide = Math.sqrt(Math.pow(x1 - x3, 2) + Math.pow(y1 - y3, 2));
    double cSide = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));

    double halfPerimeter = (aSide + bSide + cSide) / 2;

    double triangleArea = Math
        .sqrt(halfPerimeter * (halfPerimeter - aSide) * (halfPerimeter - bSide) * (halfPerimeter - cSide));

    System.out.printf("%.0f\n", triangleArea);
  }
}