Advertisement
Booster

Points inside the house

Jan 26th, 2015
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.21 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class PointsInsideTheHouse {
  4.  
  5.     public static void main(String[] args) {
  6.        
  7.         Scanner input = new Scanner(System.in);
  8.  
  9.         // Point coordinates
  10.         System.out.println("Enter point coordinates:");
  11.         float pX = input.nextFloat();
  12.         float pY = input.nextFloat();
  13.  
  14.         // triangle vertices
  15.         float leftTipX = 12.5f;
  16.         float leftTipY = 8.5f;
  17.         float rightTipX = 22.5f;
  18.         float rightTipY = leftTipY;
  19.         float topX = 17.5f;
  20.         float topY = 3.5f;
  21.  
  22.         // big rectangle borders
  23.         float bigRectXmin = 12.5f;
  24.         float bigRectXmax = 17.5f;
  25.         float bigRectYmin = 8.5f;
  26.         float bigRectYmax = 13.5f;
  27.  
  28.         // small rectangle borders;
  29.         float smallRectXmin = 20f;
  30.         float smallRectXmax = 22.5f;
  31.         float smallRectYmin = bigRectYmin;
  32.         float smallRectYmax = bigRectYmax;
  33.         String result = "Outside";
  34.         /*
  35.          * The point must be on the inner side of all triangle lines, or to lie
  36.          * on some of them to belong to the triangle
  37.          */
  38.         float pointPosition1 = calcPointPositionToLine(leftTipX, leftTipY,
  39.                 rightTipX, rightTipY, pX, pY);
  40.         float pointPosition2 = calcPointPositionToLine(leftTipX, leftTipY,
  41.                 topX, topY, pX, pY);
  42.         float pointPosition3 = calcPointPositionToLine(rightTipX, rightTipY,
  43.                 topX, topY, pX, pY);
  44.        
  45.         if (pointPosition1 <= 0 && pointPosition2 >= 0 && pointPosition3 <=0 ) {
  46.             result = "Inside";
  47.         } else if (pX >= bigRectXmin && pX <= bigRectXmax && pY >= bigRectYmin
  48.                 && pY <= bigRectYmax) {
  49.             result = "Inside";
  50.  
  51.         } else if (pX >= smallRectXmin && pX <= smallRectXmax
  52.                 && pY >= smallRectYmin && pY <= smallRectYmax) {
  53.             result = "Inside";
  54.         }
  55.         System.out.println(result);
  56.  
  57.     }
  58.  
  59.     /**
  60.      *
  61.      * @param aX, aY, bX, bY - coordinates of the ending points of the line AB
  62.      * @param cX, cY - coordinates of the point we are checking for
  63.      * @return - if value is positive the point is on the right side of the line
  64.      * if value is negative the point is on the left side of the line
  65.      * and if the result is 0 the point lies on the line.
  66.      */
  67.     public static float calcPointPositionToLine(float aX, float aY, float bX,
  68.             float bY, float cX, float cY) {
  69.         float result = (aX * (bY - cY) + bX * (cY - aY) + cX * (aY - bY)) / 2;
  70.         return result;
  71.     }
  72.  
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement