simonradev

TriangleArea

Dec 23rd, 2016
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.12 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.Scanner;
  4.  
  5. /**
  6.  * Created by simonradev on 23-Dec-16.
  7.  */
  8. public class TriangleArea {
  9.     public static void main(String[] args) {
  10.         Scanner scan = new Scanner(System.in);
  11.  
  12.         int[][] coordinates = new int[3][2];
  13.  
  14.         int lenOfMatrice = coordinates.length;
  15.         for (int i = 0; i < lenOfMatrice; i++) {
  16.  
  17.             String[] currCoordinates = scan.nextLine().toString().split(" ");
  18.  
  19.             for (int j = 0; j < 2; j++) {
  20.                 coordinates[i][j] = (Integer.parseInt(currCoordinates[j]));
  21.             }
  22.         }
  23.  
  24.         double areaOfTriangle = Math.round(calculateTheArea(coordinates));
  25.  
  26.         System.out.printf("%.0f", areaOfTriangle);
  27.     }
  28.  
  29.     private static double calculateTheArea(int[][] coordinates) {
  30.  
  31.         double areaToReturn = Math.abs(coordinates[0][0]*(coordinates[1][1] - coordinates[2][1]) +
  32.                               coordinates[1][0]*(coordinates[2][1] - coordinates[0][1]) +
  33.                               coordinates[2][0]*(coordinates[0][1] - coordinates[1][1])) / 2.0;
  34.  
  35.         return areaToReturn;
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment