Advertisement
Guest User

Problem 9. Points inside the House

a guest
May 12th, 2014
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.54 KB | None | 0 0
  1. package point.in.house;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class PointInHouse {
  6.  
  7.     public static double x1 = 12.5, y1 = 8.5;
  8.     public static double x2 = 22.5, y2 = 8.5;
  9.     public static double x3 = 17.5, y3 = 3.5;
  10.  
  11.     public static void main(String[] args) {
  12.         Scanner rl = new Scanner(System.in);
  13.  
  14.         String[] points = rl.nextLine().split(" ");
  15.         Float pointX = Float.parseFloat(points[0]);
  16.         Float pointY = Float.parseFloat(points[1]);
  17.  
  18.         if (inMainRectangle(pointX, pointY)) {
  19.             if (onTheDoor(pointX, pointY) || !inTriangle(pointX, pointY)) {
  20.                 System.out.println("Outside");
  21.             } else {
  22.                 System.out.println("Inside");
  23.             }
  24.         } else {
  25.             System.out.println("Outside");
  26.         }
  27.     }
  28.  
  29.     public static boolean inTriangle(double x, double y) {
  30.  
  31.         double ABC = Math.abs(x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2));
  32.         double ABP = Math.abs(x1 * (y2 - y) + x2 * (y - y1) + x * (y1 - y2));
  33.         double APC = Math.abs(x1 * (y - y3) + x * (y3 - y1) + x3 * (y1 - y));
  34.         double PBC = Math.abs(x * (y2 - y3) + x2 * (y3 - y) + x3 * (y - y2));
  35.  
  36.         boolean isInTriangle = ABP + APC + PBC == ABC;
  37.  
  38.         return isInTriangle;
  39.     }
  40.  
  41.     public static boolean inMainRectangle(double x, double y) {
  42.         return (x >= 12.5 && x <= 22.5 && y >= 3.5 && y <= 13.5);
  43.     }
  44.  
  45.     public static boolean onTheDoor(double x, double y) {
  46.         return (x > 17.5 && x < 20 && y > 8.5 && y <= 13.5);
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement