Advertisement
TzvetanIG

Points-inside-thet-House

May 14th, 2014
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.17 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class _09_PointsInsideTheHouse {
  4.  
  5.     public static void main(String[] args) {
  6.  
  7.         double[] point = readPoint();
  8.  
  9.         String result = "Outside";
  10.  
  11.         if (isInsideLeftRectangle(point) || isInsideRightRectangle(point)
  12.                 || isInsideTriangle(point)) {
  13.  
  14.             result = "Inside";
  15.  
  16.         }
  17.  
  18.         System.out.println(result);
  19.  
  20.     }
  21.  
  22.     public static boolean isInsideTriangle(double[] point) {
  23.  
  24.         double[] p1 = { 12.5, 8.5 };
  25.  
  26.         double[] p2 = { 17.5, 3.5 };
  27.  
  28.         double valueOfLineFunction1 = getValueOfLineFunction(p1, p2, point[0]);
  29.  
  30.         double[] p3 = { 22.5, 8.5 };
  31.  
  32.         double valueOfLineFunction2 = getValueOfLineFunction(p3, p2, point[0]);
  33.  
  34.         boolean result = false;
  35.  
  36.         if (valueOfLineFunction1 <= point[1]
  37.                 && valueOfLineFunction2 <= point[1] && point[1] <= 8.5) {
  38.  
  39.             result = true;
  40.  
  41.         }
  42.  
  43.         return result;
  44.     }
  45.  
  46.     public static boolean isInsideLeftRectangle(double[] point) {
  47.  
  48.         boolean result = false;
  49.  
  50.         if (isRange(point[0], 12.5, 17.5) && isRange(point[1], 8.5, 13.5)) {
  51.  
  52.             result = true;
  53.  
  54.         }
  55.  
  56.         return result;
  57.  
  58.     }
  59.  
  60.     public static boolean isInsideRightRectangle(double[] point) {
  61.  
  62.         boolean result = false;
  63.  
  64.         if (isRange(point[0], 20, 22.5) && isRange(point[1], 8.5, 13.5)) {
  65.  
  66.             result = true;
  67.  
  68.         }
  69.  
  70.         return result;
  71.     }
  72.  
  73.     // check whether the number is in a range min max.
  74.     public static boolean isRange(double number, double min, double max) {
  75.  
  76.         return number >= min && number <= max;
  77.  
  78.     }
  79.  
  80.     // Read a point. Return a array with a two member. The first member is X
  81.     // (index 0). The second is Y (index 1)
  82.     public static double[] readPoint() {
  83.  
  84.         Scanner input = new Scanner(System.in);
  85.  
  86.         String[] inputArr = input.nextLine().split(" ");
  87.  
  88.         double[] point = new double[2];
  89.  
  90.         point[0] = Double.parseDouble(inputArr[0]);
  91.  
  92.         point[1] = Double.parseDouble(inputArr[1]);
  93.  
  94.         return point;
  95.  
  96.     }
  97.  
  98.     public static double getValueOfLineFunction(double[] point1,
  99.             double[] point2, double x) {
  100.  
  101.         double a = (point2[1] - point1[1]) / (point2[0] - point1[0]);
  102.  
  103.         double b = point1[1]
  104.                 - ((point2[1] - point1[1]) / (point2[0] - point1[0]))
  105.                 * point1[0];
  106.  
  107.         double y = a * x + b;
  108.  
  109.         return y;
  110.     }
  111.  
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement