Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.ArrayList;
  5. import java.util.Arrays;
  6.  
  7. public class Points {
  8. private static ArrayList<ArrayList<Double>> shapes = new ArrayList<>();
  9.  
  10. public static void main(String[] args) throws IOException {
  11. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  12. String brIn;
  13.  
  14. while ((brIn = br.readLine()) != null && !brIn.equals("*")) {
  15. String[] tokens = brIn.split(" ");
  16.  
  17. ArrayList<Double> curShape = new ArrayList<>();
  18. Arrays.stream(tokens).skip(1).forEach(x -> curShape.add(Double.parseDouble(x)));
  19. shapes.add(curShape);
  20. }
  21.  
  22. int curPoint = 1;
  23.  
  24.  
  25. String checkIn;
  26. while ((checkIn = br.readLine()) != null && !checkIn.equals("9999.9 9999.9")) {
  27. int curIndex = 1;
  28. ArrayList<Double> tokens = new ArrayList<>();
  29. Arrays.stream(checkIn.split(" ")).forEach(x -> tokens.add(Double.parseDouble(x)));
  30. boolean hasFoundOnce = false;
  31. for (ArrayList<Double> cur : shapes) {
  32. boolean isWithin;
  33. switch (cur.size()) {
  34. case 4:
  35. isWithin = withinRectangle(cur.get(0), cur.get(1), cur.get(2), cur.get(3), tokens.get(0), tokens.get(1));
  36. break;
  37. case 3:
  38. isWithin = withinCircle(cur.get(0), cur.get(1), cur.get(2), tokens.get(0), tokens.get(1));
  39. break;
  40. default:
  41. isWithin = withinTriangle(cur.get(0), cur.get(1), cur.get(2), cur.get(3), cur.get(4), cur.get(5), tokens.get(0), tokens.get(1));
  42. break;
  43. }
  44.  
  45. if (isWithin) {
  46. System.out.println(String.format("Point %d is contained in figure %d", curPoint, curIndex));
  47. hasFoundOnce = true;
  48. }
  49. curIndex++;
  50. }
  51.  
  52. if (!hasFoundOnce) {
  53. System.out.println(String.format("Point %d is not contained in any figure", curPoint));
  54. }
  55. curPoint++;
  56. }
  57.  
  58.  
  59. }
  60.  
  61.  
  62. public static boolean withinTriangle(double x1, double y1, double x2, double y2, double x3, double y3, double xP, double yP) {
  63. double initialArea = Math.abs((x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)) / 2);
  64.  
  65. // x1 y1
  66. double firstSmallTriangle = Math.abs((xP * (y2 - y3) + x2 * (y3 - yP) + x3 * (yP - y2)) / 2);
  67.  
  68. // x2 y2
  69. double secondSmallTriangle = Math.abs((x1 * (yP - y3) + xP * (y3 - y1) + x3 * (y1 - yP)) / 2);
  70.  
  71. // x3 y3
  72. double thirdSmallTriangle = Math.abs((x1 * (y2 - yP) + x2 * (yP - y1) + xP * (y1 - y2)) / 2);
  73.  
  74. return firstSmallTriangle != 0.0 && secondSmallTriangle != 0.0 && thirdSmallTriangle != 0.0 && (initialArea * 1.000001 > firstSmallTriangle + secondSmallTriangle + thirdSmallTriangle && initialArea * 0.999999 < firstSmallTriangle + secondSmallTriangle + thirdSmallTriangle);
  75. }
  76.  
  77. public static boolean withinCircle(double x1, double y1, double r, double xP, double yP) {
  78. return Math.pow(Math.pow((xP - x1), 2) + Math.pow((yP - y1), 2), 0.5) < r;
  79. }
  80.  
  81. public static boolean withinRectangle(double x1, double y1, double x2, double y2, double xP, double yP) {
  82. return xP > x1 && xP < x2 && yP < y1 && yP > y2;
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement