Advertisement
Guest User

Problem 9. Points inside the House

a guest
May 15th, 2014
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3.  
  4. public class PointsInsideTheHouse {
  5.  
  6. public static void main(String[] args) {
  7. Scanner input = new Scanner(System.in);
  8. //Point
  9. System.out.print("Enter point: ");
  10. float x = input.nextFloat();
  11. float y = input.nextFloat();
  12.  
  13. //If A=A1+A2+A3 => point is inside of triangle
  14. float A= area(12.5f, 8.5f, 22.5f, 8.5f, 17.5f, 3.5f);
  15. float A1= area(x, y, 22.5f, 8.5f, 17.5f, 3.5f);
  16. float A2= area(12.5f, 8.5f, x, y, 17.5f, 3.5f);
  17. float A3= area(12.5f, 8.5f, 22.5f, 8.5f, x, y);
  18.  
  19. //Check triangle
  20. if (A==A1+A2+A3) {
  21. System.out.println("Inside");
  22. }
  23. //Check first rectangle
  24. else if(x>=12.5&&x<=17.5&&y>=8.5&&y<=13.5){
  25. System.out.println("Inside");
  26. }
  27. //Check second rectangle
  28. else if (x>=20&&x<=22.5&&y>=8.5&&y<=13.5) {
  29. System.out.println("Inside");
  30. }
  31. else {
  32. System.out.println("Outside");
  33. }
  34.  
  35. }
  36.  
  37. private static float area(float x1, float y1, float x2, float y2, float x3, float y3) {
  38.  
  39. float area= Math.abs((x1*(y2-y3) + x2*(y3-y1)+ x3*(y1-y2))/2.0f);
  40. return area;
  41.  
  42. }
  43.  
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement