Advertisement
Guest User

TriangleArea

a guest
May 12th, 2014
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.67 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class TriangleArea {
  4.  
  5.     public static void main(String[] args) {
  6.         Scanner sc = new Scanner(System.in);
  7.         int[] points = new int[6];
  8.         double[] lengths = new double[3];
  9.         for(int i = 0; i < 3; i++) {
  10.             System.out.print("Enter coords of point " + (char)('A' + i) + ": ");
  11.             points[i * 2] = sc.nextInt();
  12.             points[i * 2 + 1] = sc.nextInt();
  13.             System.out.println();
  14.         }
  15.         for (int i = 0; i < 2; i++) {
  16.             lengths[i] = getLength(points[i * 2], points[i * 2 + 1],
  17.                                   points[(i + 1) * 2], points[(i + 1) * 2 + 1]);
  18.         }
  19.         lengths[2] = getLength(points[0], points[1], points[4], points[5]);
  20.        
  21.         if(isTrianlge(lengths)) {
  22.             System.out.println(Math.round(heronArea(lengths)));
  23.         }
  24.         else {
  25.             System.out.println(0);
  26.         }
  27.  
  28.     }
  29.  
  30.     private static double heronArea(double[] lengths) {
  31.         double perimeter = lengths[0] + lengths[1] + lengths[2];
  32.         double semiperimeter = perimeter / 2;
  33.         double area = Math.sqrt( semiperimeter *
  34.                                  (semiperimeter - lengths[0]) *
  35.                                  (semiperimeter - lengths[1]) *
  36.                                  (semiperimeter - lengths[2]) );
  37.         return area;
  38.     }
  39.  
  40.     private static boolean isTrianlge(double[] lengths) {
  41.         boolean isTriangle;
  42.         isTriangle = (lengths[0] < lengths[1] + lengths[2]
  43.                       || lengths[1] < lengths[0] + lengths[2]
  44.                       || lengths[2] < lengths[0] + lengths[1]);
  45.         return isTriangle;
  46.     }
  47.  
  48.     private static double getLength(int firstPointX, int firstPointY, int secondPointX, int secondPointY) {
  49.         double length = Math.sqrt((firstPointX - secondPointX) * (firstPointX - secondPointX)
  50.                                 + (firstPointY - secondPointY) * (firstPointY - secondPointY));
  51.         return length;
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement