Advertisement
AJMitev

Intersection of Circles

Aug 6th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4. public class Point {
  5. private int x;
  6. private int y;
  7.  
  8. public Point(int x, int y) {
  9. this.x = x;
  10. this.y = y;
  11. }
  12.  
  13. public int getX() {
  14. return x;
  15. }
  16.  
  17. public void setX(int x) {
  18. this.x = x;
  19. }
  20.  
  21. public int getY() {
  22. return y;
  23. }
  24.  
  25. public void setY(int y) {
  26. this.y = y;
  27. }
  28. }
  29.  
  30.  
  31. public class Circle {
  32. private Point center;
  33. private int radius;
  34.  
  35. public Circle(Point center, int radius) {
  36. this.center = center;
  37. this.radius = radius;
  38. }
  39.  
  40. public Point getCenter() {
  41. return center;
  42. }
  43.  
  44. public void setCenter(Point center) {
  45. this.center = center;
  46. }
  47.  
  48. public int getRadius() {
  49. return radius;
  50. }
  51.  
  52. public void setRadius(int radius) {
  53. this.radius = radius;
  54. }
  55. }
  56.  
  57.  
  58. public class IntersectionOfCircles {
  59. public static void main(String[] args) {
  60. Scanner scanner = new Scanner(System.in);
  61.  
  62. int[] cirleOneProps = Arrays.stream(scanner.nextLine().split("\\s"))
  63. .mapToInt(Integer::parseInt).toArray();
  64.  
  65. int[] cirleTwoProps = Arrays.stream(scanner.nextLine().split("\\s"))
  66. .mapToInt(Integer::parseInt).toArray();
  67.  
  68. Point centerOne = new Point(cirleOneProps[0],cirleOneProps[1]);
  69. Point centerTwo = new Point(cirleTwoProps[0],cirleTwoProps[1]);
  70.  
  71. Circle circleOne = new Circle(centerOne,cirleOneProps[2]);
  72. Circle circleTwo = new Circle(centerTwo,cirleTwoProps[2]);
  73. double distance = CalculateDistance(centerOne, centerTwo);
  74. boolean areIntersect = Intersect(circleOne, circleTwo, distance);
  75.  
  76. if (areIntersect){
  77. System.out.println("Yes");
  78. }else {
  79. System.out.println("No");
  80. }
  81.  
  82. }
  83.  
  84. public static boolean Intersect(Circle c1, Circle c2, double distance){
  85. boolean areIntersect = false;
  86.  
  87. if (distance <= c1.getRadius() + c2.getRadius()){
  88. areIntersect = true;
  89. }
  90.  
  91. return areIntersect;
  92.  
  93. }
  94.  
  95. public static double CalculateDistance(Point p1, Point p2){
  96.  
  97. double differenceOfY = p1.getY() - p2.getY();
  98. double differenceOfX = p1.getX() - p2.getX();
  99.  
  100. double powX = Math.pow(differenceOfX,2);
  101. double powY = Math.pow(differenceOfY,2);
  102.  
  103. return Math.sqrt(powX+powY);
  104. }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement