Advertisement
Razhagal

PointInCircleOutRectangle

Mar 14th, 2014
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.94 KB | None | 0 0
  1. using System;
  2. class PointInsideACircleAndOutsideOfRectangle
  3. {
  4.     static void Main()
  5.     {
  6.         Console.Write("Input coodinate \"x\": ");
  7.         double x = double.Parse(Console.ReadLine());
  8.         double xCircle = 1;
  9.         Console.Write("Input coordinate \"y\": ");
  10.         double y = double.Parse(Console.ReadLine());
  11.         double yCircle = 1;
  12.         double r = 1.5; //circle radius
  13.  
  14.         bool inCircle = false;
  15.         bool inRectangle = false;
  16.  
  17.         //Rectangle borders
  18.         double left = -1;
  19.         double top = 1;
  20.         double width = 6;
  21.         double height = 2;
  22.  
  23.         double rectTop = top;
  24.         double rectLeft = left;
  25.         double rectBottom = top - height;
  26.         double rectRight = left + width;
  27.        
  28.         if (((x - xCircle) * (x - xCircle)) + ((y - yCircle) * (y - yCircle)) <= r * r)
  29.         {
  30.             inCircle = true;
  31.  
  32.             if ((x >= rectLeft && x <= rectRight) && (y >= rectBottom && y <= rectTop))
  33.             {
  34.                 inRectangle = true;
  35.             }
  36.             else if (x < rectLeft || x > rectRight)
  37.             {
  38.                 inRectangle = false;
  39.             }
  40.             else if (y < rectBottom || y > rectTop)
  41.             {
  42.                 inRectangle = false;
  43.             }
  44.         }
  45.         else
  46.         {
  47.             inCircle = false;
  48.  
  49.             if ((x >= rectLeft && x <= rectRight) && (y >= rectBottom && y <= rectTop))
  50.             {
  51.                 inRectangle = true;
  52.             }
  53.             else if (x < rectLeft || x > rectRight)
  54.             {
  55.                 inRectangle = false;
  56.             }
  57.             else if (y < rectBottom || y > rectTop)
  58.             {
  59.                 inRectangle = false;
  60.             }
  61.         }
  62.  
  63.         if (inCircle && !inRectangle)
  64.         {
  65.             Console.WriteLine("Inside the circle and outside the rectangle? Yes");
  66.         }
  67.         else
  68.         {
  69.             Console.WriteLine("Inside the circle and outside the rectangle? No");
  70.         }
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement