Advertisement
G_Burlakova

PointInsideACircleAndOutsideOfARectangle

Mar 14th, 2014
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.09 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.  
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             // Coordinates of a specified point
  13.             double xCoordinate = -100;
  14.             double yCoordinate = -100;
  15.             // The following section checks if a point is in a circle
  16.             double centreXcoordinate = 1;
  17.             double centreYcoordinate = 1;
  18.             double radius = 1.5;
  19.  
  20.             // The next two lines calculate the distence from the centre to each coordinate
  21.             //Follow the link to read about the test whether a point is in a circle
  22.             //http://math.stackexchange.com/questions/198764/how-to-know-if-a-point-is-inside-a-circle
  23.             double xDistance = xCoordinate - centreXcoordinate;
  24.             double yDistance = yCoordinate - centreYcoordinate;
  25.             bool isInCircle = (xDistance * xDistance) + (yDistance * yDistance) <= (radius * radius);
  26.             if (!isInCircle)
  27.             {
  28.                 Console.WriteLine("No.");
  29.                 return;
  30.             }
  31.  
  32.             // The following section checks if a point is in a rectangle
  33.             double top = 1;
  34.             double left = -1;
  35.             double width = 6;
  36.             double height = 2;
  37.             double xMin = left;
  38.             double xMax = left + width;
  39.             double yMin = top - height;
  40.             double yMax = top;
  41.  
  42.             // X Out Of Range
  43.             bool isXOutRangleRectangle = ((xCoordinate < xMin) || (xCoordinate > xMax));
  44.  
  45.             // Y Out Of Range
  46.             bool isYOutRangleRectangle = ((yCoordinate < yMin) || (yCoordinate > yMax));
  47.  
  48.             // Point outside of a rectangle
  49.             bool isOutsideRectangle = isXOutRangleRectangle || isYOutRangleRectangle;
  50.  
  51.             if (isInCircle && isOutsideRectangle)
  52.             {
  53.                 Console.WriteLine("Yes.");
  54.             }
  55.             else
  56.             {
  57.                 Console.WriteLine("No.");
  58.             }
  59.            
  60.         }
  61.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement