Advertisement
dimipan80

Points inside the House

Sep 6th, 2014
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.95 KB | None | 0 0
  1. /* Write a program to check whether a point is inside or outside the house below.
  2.  * The point is given as a pair of floating-point numbers, separated by a space.
  3.  * Your program should print "Inside" or "Outside". */
  4.  
  5. import java.util.Locale;
  6. import java.util.Scanner;
  7.  
  8. public class _09_PointInsideTheHouse {
  9.  
  10.     public static void main(String[] args) {
  11.         // TODO Auto-generated method stub
  12.         Locale.setDefault(Locale.ROOT);
  13.         Scanner scan = new Scanner(System.in);
  14.         System.out
  15.                 .println("Enter 2 Real numbers for coordinates of your Point (x, y) :");
  16.         double pointX = scan.nextDouble();
  17.         double pointY = scan.nextDouble();
  18.  
  19.         boolean pointIsInsideTheFloor = checkThePointIsInsideInTheHouseFloor(
  20.                 pointX, pointY);
  21.         if (!pointIsInsideTheFloor) {
  22.             boolean pointIsInsideTheRoof = checkTheGivenPointIsInsideInTheRoof(
  23.                     pointX, pointY);
  24.  
  25.             String result = pointIsInsideTheRoof ? "Inside" : "Outside";
  26.  
  27.             System.out.printf("That point is %s of the House!%n", result);
  28.         } else {
  29.             System.out.println("That point is Inside of the House!");
  30.         }
  31.     }
  32.  
  33.     private static boolean checkThePointIsInsideInTheHouseFloor(double pX,
  34.             double pY) {
  35.         double minX = 12.5, maxX = 17.5;
  36.         double minY = 8.5, maxY = 13.5;
  37.         boolean pointInsideLeft = pX >= minX && pX <= maxX && pY >= minY
  38.                 && pY <= maxY;
  39.  
  40.         minX = 20;
  41.         maxX = 22.5;
  42.         boolean pointInsideRight = pX >= minX && pX <= maxX && pY >= minY
  43.                 && pY <= maxY;
  44.  
  45.         return (pointInsideLeft || pointInsideRight);
  46.     }
  47.  
  48.     private static boolean checkTheGivenPointIsInsideInTheRoof(double pX,
  49.             double pY) {
  50.         if (pX == 17.5 && pY == 3.5) {
  51.             return true;
  52.         }
  53.  
  54.         // Now will using defined class Point:
  55.         Point givenPoint = new Point(pX, pY);
  56.         Point[] trianglePoints = { new Point(12.5, 8.5), new Point(17.5, 3.5),
  57.                 new Point(22.5, 8.5) };
  58.         for (int i = 0; i < trianglePoints.length; i++) {
  59.             Point pointA = trianglePoints[i % 3];
  60.             Point pointB = trianglePoints[(i + 1) % 3];
  61.             boolean valueIsNegative = checkTheValueOrientationOfGivenPointAndOneSideOfTriangle(
  62.                     givenPoint, pointA, pointB);
  63.             if (valueIsNegative) {
  64.                 return false;
  65.             }
  66.         }
  67.  
  68.         return true;
  69.     }
  70.  
  71.     private static boolean checkTheValueOrientationOfGivenPointAndOneSideOfTriangle(
  72.             Point givenPoint, Point p1, Point p2) {
  73.         double result = ((Point.minusX(p2, p1) * (Point.minusY(givenPoint, p1))) - (Point
  74.                 .minusY(p2, p1) * Point.minusX(givenPoint, p1)));
  75.  
  76.         return result < 0 ? true : false;
  77.     }
  78.  
  79. }
  80.  
  81. // User defining class Point
  82.  
  83. public class Point {
  84.     private double x, y;
  85.  
  86.     public Point(double x, double y) {
  87.         this.x = x;
  88.         this.y = y;
  89.     }
  90.  
  91.     public double getX() {
  92.         return x;
  93.     }
  94.  
  95.     public void setX(double x) {
  96.         this.x = x;
  97.     }
  98.  
  99.     public double getY() {
  100.         return y;
  101.     }
  102.  
  103.     public void setY(double y) {
  104.         this.y = y;
  105.     }
  106.  
  107.     public static double minusX(Point p1, Point p2) {
  108.         return p1.x - p2.x;
  109.     }
  110.  
  111.     public static double minusY(Point p1, Point p2) {
  112.         return p1.y - p2.y;
  113.     }  
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement