Advertisement
dimipan80

3.10 Point Inside the Circle and Outside of Rectangle

Jun 5th, 2014
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.57 KB | None | 0 0
  1. // Write an expression that checks for given point (x, y) if it is within the circle K({1, 1}, 1.5) and out of the rectangle R(top=1, left=-1, width=6, height=2).
  2.  
  3. namespace _10.PointInsideCircleButOutsideOfRectangle
  4. {
  5.     using System;
  6.  
  7.     public class PointInsideCircleButOutsideOfRectangle
  8.     {        
  9.         private const double CenterX = 1d;
  10.         private const double CenterY = 1d;
  11.         private const double Radius = 1.5d;
  12.         private const double PointXOnLeftTopAngle = -1d;
  13.         private const double PointYOnLeftTopAngle = 1d;
  14.         private const double WidthRect = 6d;
  15.         private const double HeightRect = 2d;
  16.  
  17.         public static void Main(string[] args)
  18.         {
  19.             checked
  20.             {
  21.                 Console.Write("Enter value to X of given point: ");
  22.                 double pointX = double.Parse(Console.ReadLine());
  23.                 Console.Write("Enter value to Y of given point: ");
  24.                 double pointY = double.Parse(Console.ReadLine());
  25.  
  26.                 bool pointIsInsideCircle = CheckGivenPointIsInsideTheCircle(pointX, pointY);
  27.                 bool pointIsOutsideOfRectangle = CheckGivenPointIsOutsideOfTheRectangle(pointX, pointY);
  28.                 bool pointIsInsideCircleButIsOutsideRectangle = pointIsInsideCircle && pointIsOutsideOfRectangle;
  29.  
  30.                 Console.WriteLine("Given point is Inside the Circle, but is Outside of the Rectangle: {0} !", pointIsInsideCircleButIsOutsideRectangle);
  31.             }
  32.         }
  33.  
  34.         private static bool CheckGivenPointIsOutsideOfTheRectangle(double pointX, double pointY)
  35.         {
  36.             bool isOutside = false;
  37.             if (pointX < PointXOnLeftTopAngle || pointX > PointXOnLeftTopAngle + WidthRect)
  38.             {
  39.                 isOutside = true;
  40.             }
  41.             else if (pointY < PointYOnLeftTopAngle - HeightRect || pointY > PointYOnLeftTopAngle)
  42.             {
  43.                 isOutside = true;
  44.             }
  45.  
  46.             return isOutside;
  47.         }
  48.  
  49.         private static bool CheckGivenPointIsInsideTheCircle(double pointX, double pointY)
  50.         {
  51.             // Here will use Gauss circle equation: (x-a)^2 + (y-b)^2 <= r^2 :
  52.             bool isInside = true;
  53.             double squareDiffX = (pointX - CenterX) * (pointX - CenterX);
  54.             double squareDiffY = (pointY - CenterY) * (pointY - CenterY);
  55.             double sqrtResult = Math.Sqrt(squareDiffX + squareDiffY);
  56.             if (sqrtResult > Radius)
  57.             {
  58.                 isInside = false;
  59.             }
  60.  
  61.             return isInside;
  62.         }
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement