Advertisement
Guest User

03.CircleIntersection

a guest
Nov 2nd, 2016
496
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.51 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _03.CircleIntersection
  8. {
  9.     public class Circle
  10.     {
  11.         public double X { get; set; }
  12.         public double Y { get; set; }
  13.         public double Radius { get; set; }
  14.     }
  15.     public class Program
  16.     {
  17.         static void Main()
  18.         {
  19.             string[] firstCircleInput = Console.ReadLine().Split(' ');
  20.             string[] secondCircleInput = Console.ReadLine().Split(' ');
  21.  
  22.             //create the first center point and set the first circle properties
  23.  
  24.             Circle firstCircle = new Circle();
  25.             firstCircle.Radius = double.Parse(firstCircleInput[2]);
  26.             firstCircle.X = double.Parse(firstCircleInput[0]);
  27.             firstCircle.Y = double.Parse(firstCircleInput[1]);
  28.             //create the second center point and set the second circle properties
  29.  
  30.             Circle secondCircle = new Circle();
  31.             secondCircle.Radius = double.Parse(secondCircleInput[2]);
  32.             secondCircle.X = double.Parse(secondCircleInput[0]);
  33.             secondCircle.Y = double.Parse(secondCircleInput[1]);
  34.  
  35.             bool circlesIntersect = Intersect(firstCircle, secondCircle);
  36.             if (circlesIntersect)
  37.             {
  38.                 Console.WriteLine("Yes");
  39.             }
  40.             else
  41.             {
  42.                 Console.WriteLine("No");
  43.             }
  44.  
  45.            
  46.         }
  47.  
  48.         static public bool Intersect(Circle firstCircle, Circle secondCircle)
  49.         {
  50.             //find the distance between the points
  51.             double deltaX = Math.Abs(firstCircle.X - secondCircle.X);
  52.             double deltaY = Math.Abs(firstCircle.Y - secondCircle.Y);
  53.             double distance = Math.Sqrt(Math.Pow(deltaX, 2) + Math.Pow(deltaY, 2));
  54.             double sumRadiuses = firstCircle.Radius + secondCircle.Radius;
  55.             //if the distance between the points is greater than the sum of the radiuses AND
  56.             //if the radius of the smaller circle + the distance between the points is NOT
  57.             //less than the radius of the larger circle, the circles intersect
  58.             /* !(Math.Min(firstCircle.Radius, secondCircle.Radius) +
  59.                 distance < Math.Max(firstCircle.Radius, secondCircle.Radius)) */
  60.             if (sumRadiuses >= distance)
  61.             {
  62.                 return true;
  63.             }
  64.             else
  65.             {
  66.                 return false;
  67.             }
  68.         }
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement