Advertisement
M_Andreev

PointsInsideTheHouse

Sep 7th, 2014
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.50 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class _09_PointsInsideTheHouse {
  4.  
  5.     public static void main(String[] args) {
  6.         Scanner sc = new Scanner(System.in);
  7.         System.out.print("Ax: ");
  8.         double ax = sc.nextDouble();
  9.         System.out.print("Ay: ");
  10.         double ay = sc.nextDouble();
  11.  
  12.         if ((isInSquare(ax, ay) || isInTriangle(ax, ay) || isInRec(ax, ay)) == true) {
  13.             System.out.print("Inside");
  14.         } else {
  15.             System.out.println("Outside");
  16.         }
  17.     }
  18.  
  19.     public static boolean isInSquare(double ax, double ay) {
  20.         boolean result = false;
  21.  
  22.         if (ax >= 12.5 && ax <= 17.5) {
  23.             if (ay >= 8.5 && ay <= 13.5) {
  24.                 result = true;
  25.             }
  26.         }
  27.         return result;
  28.     }
  29.  
  30.     public static boolean isInTriangle(double ax, double ay) {
  31.                
  32.         double x1 = 12.5;
  33.         double y1 = 8.5;
  34.         double x2 = 17.5;
  35.         double y2 = 3.5;
  36.         double x3 = 22.5;
  37.         double y3 = 8.5;
  38.        
  39.         double a = Math.abs (x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2));
  40.         double b = Math.abs (x1 * (y2 - ay) + x2 * (ay - y1) + ax * (y1 - y2));
  41.         double c = Math.abs (x1 * (ay - y3) + ax * (y3 - y1) + x3 * (y1 - ay));
  42.         double d = Math.abs (ax * (y2 - y3) + x2 * (y3 - ay) + x3 * (ay - y2));
  43.         boolean isInTriangle = Math.round(a + b + c) == d;
  44.        
  45.         if (isInTriangle == true) {
  46.             return true;
  47.         }
  48.         else {
  49.             return false;
  50.         }
  51.     }
  52.  
  53.     public static boolean isInRec(double ax, double ay) {
  54.         boolean result = false;
  55.  
  56.         if (ax >= 20 && ax <= 22.5) {
  57.             if (ay >= 8.5 && ay <= 13.5) {
  58.                 result = true;
  59.             }
  60.         }
  61.         return result;
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement