Advertisement
dimipan80

Triangle Area

Aug 17th, 2014
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.25 KB | None | 0 0
  1. /* Write a program that enters 3 points in the plane (as integer x and y coordinates),
  2.  * calculates and prints the area of the triangle composed by these 3 points.
  3.  * Round the result to a whole number.
  4.  * In case the three points do not form a triangle, print "0" as result. */
  5.  
  6. import java.util.Locale;
  7. import java.util.Scanner;
  8.  
  9. public class _02_TriangleArea {
  10.  
  11.     public static void main(String[] args) {
  12.         // TODO Auto-generated method stub
  13.         Locale.setDefault(Locale.ROOT);
  14.         Scanner scan = new Scanner(System.in);
  15.         System.out.println("You must enters 3 points of the plane (x, y), which formed a Triangle!");
  16.  
  17.                 // Here will using defined class Point:
  18.         Point[] threePoints = new Point[3];
  19.         for (int point = 0; point < threePoints.length; point++) {
  20.             System.out.print("Enter next Point (2 Integer numbers for X and Y): ");
  21.             double x = scan.nextInt();
  22.             double y = scan.nextInt();
  23.             threePoints[point] = new Point(x, y);
  24.         }
  25.  
  26.         boolean isTriangle = checkGivenThreePointsIsFormTriangle(threePoints);
  27.  
  28.         long triangleArea = 0;
  29.         if (isTriangle) {
  30.             triangleArea = Math.round(calculateTriangleAreaFromGiven3Points(threePoints));
  31.         }
  32.  
  33.         System.out.println("The Area of the Triangle with these 3 points is: " + triangleArea);
  34.     }
  35.  
  36.     private static double calculateTriangleAreaFromGiven3Points(Point[] threePoints) {
  37.         // TODO Auto-generated method stub
  38.         double area = 0;
  39.         for (int i = 0; i < threePoints.length; i++) {
  40.             Point point1 = threePoints[i % 3];
  41.             Point point2 = threePoints[(i + 1) % 3];
  42.             Point point3 = threePoints[(i + 2) % 3];
  43.             area += point1.getX() * (Point.minusY(point2, point3));
  44.         }
  45.  
  46.         area = Math.abs(area / 2);
  47.         return area;
  48.     }
  49.  
  50.     private static boolean checkGivenThreePointsIsFormTriangle(
  51.             Point[] threePoints) {
  52.         // TODO Auto-generated method stub
  53.         double[] collinearFactor = new double[2];
  54.         for (int i = 0; i + 1 < threePoints.length; i++) {
  55.             Point currentPoint = threePoints[i];
  56.             Point nextPoint = threePoints[i + 1];
  57.             double diffY = Point.minusY(nextPoint, currentPoint);
  58.             double diffX = Point.minusX(nextPoint, currentPoint);
  59.             collinearFactor[i] = (diffY / diffX);
  60.         }
  61.  
  62.         if (collinearFactor[1] == collinearFactor[0]) {
  63.             return false;
  64.         }
  65.  
  66.         return true;
  67.     }
  68.  
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement