Advertisement
nadiahristova

Prob9_PointsInsideTheHouse

Jan 24th, 2015
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.65 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Prob9_PointsInsideTheHouse {
  4.     public static void main(String[] args) {
  5.         Scanner input = new Scanner(System.in);
  6.        
  7.         System.out.print("Please enter the coordinates of the point in question: ");
  8.         Point givenPoint = new Point(input.nextDouble(), input.nextDouble());      
  9.         Point[] leftRec = new Point[]{new Point(12.5, 8.5),new Point(12.5, 13.5),
  10.                 new Point(17.5, 13.5), new Point(17.5, 8.5)};
  11.         Point[] rigthRec = new Point[]{new Point(20, 8.5),new Point(20, 13.5),
  12.                 new Point(22.5, 13.5), new Point(22.5, 8.5)};
  13.         Point[] triangle = new Point[]{new Point(12.5, 8.5),new Point(22.5, 8.5),
  14.                 new Point(17.5, 3.5)};
  15.         boolean isInside = Point.isInsideFig(leftRec,givenPoint) || Point.isInsideFig(rigthRec,givenPoint) ||
  16.                  Point.isInsideFig(triangle,givenPoint);
  17.        
  18.         if (isInside){
  19.             System.out.println("Inside");
  20.         } else {
  21.             System.out.println("Outside");
  22.         }
  23.     }  
  24. }
  25.  
  26. class Point{
  27.     private double x;
  28.     private double y;
  29.    
  30.     public Point(double x, double y){
  31.         this.x = x;
  32.         this.y = y;
  33.     }
  34.    
  35.     public double getX(){
  36.         return this.x;
  37.     }
  38.    
  39.     public void setX(double x){
  40.         this.x = x;
  41.     }
  42.    
  43.     public double getY(){
  44.         return this.y;
  45.     }
  46.    
  47.     public void setY(double y){
  48.         this.y = y;
  49.     }
  50.    
  51.     public static boolean isInsideFig(Point[] fig, Point pointInQ){
  52.         boolean isIn = true;
  53.        
  54.         for (int i = 0; i < fig.length; i++) {
  55.             int index = i+1;
  56.             if (i == fig.length-1) {
  57.                 index =0;
  58.             }
  59.             double findSign = (fig[index].x - fig[i].x)*(pointInQ.y - fig[i].y) -  
  60.                     (fig[index].y - fig[i].y)*(pointInQ.x - fig[i].x);
  61.             if (findSign > 0) {
  62.                 isIn = false;
  63.             }
  64.         }
  65.         return isIn;
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement