svetlozar_kirkov

Points Inside The House (Homework)

Jan 22nd, 2015
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.33 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Problem9_PointsInsideTheHouse {
  4.  
  5.     public static void main(String[] args) {
  6.         Scanner input = new Scanner(System.in);
  7.         double pointX = input.nextDouble();
  8.         double pointY = input.nextDouble();
  9.         double triangleCx = 12.5;
  10.         double triangleCy = 8.5;
  11.         double triangleAx = 17.5;
  12.         double triangleAy = 3.5;
  13.         double triangleBx = 22.5;
  14.         double triangleBy = 8.5;
  15.         if(isInside(triangleAx,triangleAy,triangleBx,triangleBy,triangleCx,triangleCy,pointX,pointY)){
  16.             System.out.println("Inside");
  17.         }
  18.         else if (pointX >= 12.5 && pointX <= 17.5 && pointY <= 13.5 && pointY >=8.5){
  19.             System.out.println("Inside");
  20.         }
  21.         else if (pointX >= 20 && pointX <= 22.5 && pointY <= 13.5 && pointY >=8.5){
  22.             System.out.println("Inside");
  23.         }
  24.         else{
  25.             System.out.println("Outside");
  26.         }
  27.     }
  28.     static boolean isInside(double x1, double y1, double x2, double y2, double x3, double y3, double x, double y)
  29.     {  
  30.        double A = area (x1, y1, x2, y2, x3, y3);
  31.      
  32.        double A1 = area (x, y, x2, y2, x3, y3);
  33.      
  34.        double A2 = area (x1, y1, x, y, x3, y3);
  35.      
  36.        double A3 = area (x1, y1, x2, y2, x, y);
  37.        
  38.        return (A == A1 + A2 + A3);
  39.     }
  40.     static double area(double x1, double y1, double x2, double y2, double x3, double y3)
  41.     {
  42.        return Math.abs((x1*(y2-y3) + x2*(y3-y1)+ x3*(y1-y2))/2.0);
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment