maymunskoa

Cirlces Intersection (Archive Objects & Classes)

Apr 1st, 2020
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.   let data = input.slice(0).filter(x => x !== "");
  3.  
  4.   class Circle {
  5.     constructor(center, radius) {
  6.       this.center = center;
  7.       this.radius = radius;
  8.     }
  9.   }
  10.  
  11.   class Point {
  12.     constructor(x, y) {
  13.       this.x = x;
  14.       this.y = y;
  15.     }
  16.   }
  17.  
  18.   const intersect = (circle1, cirlce2) => {
  19.     let d = 0;
  20.     let x1 = circle1.center.x;
  21.     let y1 = circle1.center.y;
  22.     let x2 = cirlce2.center.x;
  23.     let y2 = circle2.center.y;
  24.     if (x1 === x2) {
  25.       d = Math.abs(y1 - y2);
  26.     } else if (y1 === y2) {
  27.       d = Math.abs(x1 - x2);
  28.     } else {
  29.       let a = Math.abs(x1 - x2);
  30.       let b = Math.abs(y1 - y2);
  31.       d = Math.sqrt(a ** 2 + b ** 2);
  32.     }
  33.     return d <= circle1.radius + circle2.radius;
  34.   };
  35.  
  36.   let [x1, y1, r1] = data[0].split(" ").map(Number);
  37.   let circle1 = new Circle(new Point(x1,y1), r1);
  38.   let [x2, y2, r2] = data[1].split(" ").map(Number);
  39.   let circle2 = new Circle(new Point(x2,y2), r2);
  40.  
  41.   console.log(intersect(circle1, circle2) ? "Yes" : "No");
  42. }
Advertisement
Add Comment
Please, Sign In to add comment