Advertisement
Guest User

09. Points inside the House

a guest
Aug 29th, 2015
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.66 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class _09_PointsInsideTheHouse {
  4.     private static final double X1 = 12.5;
  5.     private static final double X2 = 8.5;
  6.     private static final double X3 = 22.5;
  7.     private static final double X4 = 20;
  8.     private static final double Y1 = 8.5;
  9.     private static final double Y2 = 17.5;
  10.     private static final double Y3 = 3.5;
  11.     private static final double Y4 = 13.5;
  12.  
  13.     public static void main(String[] args) {
  14.         Scanner scanner = new Scanner(System.in);
  15.         double x = scanner.nextDouble();
  16.         double y = scanner.nextDouble();
  17.  
  18.         boolean isInsideFirstFigure = (
  19.                 x >= X1 && x <= Y2)
  20.                 && (y >= X2 && y <= Y4);
  21.  
  22.         boolean isInsideSecondFigure = (
  23.                 x >= X4 && x <= X3)
  24.                 && (y >= Y1 && y <= Y4);
  25.  
  26.         boolean isInsideThirdFigure = isInsideTriangle(x, y, X1, X2, X3, Y1, Y2, Y3);
  27.  
  28.         if (isInsideFirstFigure || isInsideSecondFigure || isInsideThirdFigure) {
  29.             System.out.println("Inside");
  30.         }
  31.     else {
  32.             System.out.println("Outside");
  33.         }
  34.     }
  35.  
  36.     private static boolean isInsideTriangle(double x, double y, double x1, double y1, double x2, double y2, double x3, double y3) {
  37.         double ABC = Math.abs(x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2));
  38.         double ABP = Math.abs(x1 * (y2 - y) + x2 * (y - y1) + x * (y1 - y2));
  39.         double APC = Math.abs(x1 * (y - y3) + x * (y3 - y1) + x3 * (y1 - y));
  40.         double PBC = Math.abs(x * (y2 - y3) + x2 * (y3 - y) + x3 * (y - y2));
  41.  
  42.         boolean isInTriangle = ABP + APC + PBC == ABC;
  43.         return isInTriangle;
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement