lmarkov

Point Within Circle And Out Of Rectangle

Nov 27th, 2012
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.00 KB | None | 0 0
  1. /*
  2.  * Write an expression that checks for given point (x, y) if it is within the circle K( (1,1), 3) and out of the rectangle R(top=1, left=-1, width=6, height=2).
  3. */
  4.  
  5. using System;
  6.  
  7. class PointWithinCircleOutOfRectangle
  8. {
  9.     static void Main()
  10.     {
  11.         double pointCoordinateX, pointCoordinateY;
  12.         int circleCenterCoordinateX = 1;
  13.         int circleCenterCoordinateY = 1;
  14.         int circleRadius = 3;
  15.         int rectangleLeftX = -1;
  16.         int rectangleTopY = 1;
  17.         int rectangleWidth = 6;
  18.         int rectangleHeidht = 2;
  19.         string checkResultCircle, checkResultRectangle;
  20.         string error01 = "Invalid input! Please enter a number between " + double.MinValue + " and " + double.MaxValue + "\n";
  21.  
  22.         Console.WriteLine("Enter coordinates of the point which you want to check");
  23.        
  24.        
  25.         /* get coordinates of the point */
  26.         Console.Write("X: ");
  27.         if (!(double.TryParse(Console.ReadLine(), out pointCoordinateX) && pointCoordinateX > double.MinValue && pointCoordinateX <= double.MaxValue))
  28.         {
  29.             Console.WriteLine(error01);
  30.             Main();
  31.         }
  32.         Console.Write("Y: ");
  33.         if (!(double.TryParse(Console.ReadLine(), out pointCoordinateY) && pointCoordinateY > double.MinValue && pointCoordinateY <= double.MaxValue))
  34.         {
  35.             Console.WriteLine(error01);
  36.             Main();
  37.         }
  38.  
  39.         /* point within circle */
  40.         if (Math.Sqrt(Math.Pow((pointCoordinateX - circleCenterCoordinateX), 2) + Math.Pow((pointCoordinateY - circleCenterCoordinateY), 2)) <= circleRadius)
  41.         {
  42.             checkResultCircle = "inside";
  43.         }
  44.         else
  45.         {
  46.             checkResultCircle = "outside";
  47.         }
  48.  
  49.         /* point within rectangle */
  50.         if ((pointCoordinateX <= (rectangleLeftX + rectangleWidth) && pointCoordinateX >= rectangleLeftX) && (pointCoordinateY >= (rectangleTopY - rectangleHeidht) && pointCoordinateY <= rectangleTopY))
  51.         {
  52.             checkResultRectangle = "inside";
  53.         }
  54.         else
  55.         {
  56.             checkResultRectangle = "outside";
  57.         }
  58.        
  59.         /* show result information */
  60.         Console.Write("The point P({0},{1}) is within the circle K(({2},{3}),{4}) and outside the rectangle R(top = {5}, left = {6}, width = {7}, height = {8})? -> ", pointCoordinateX, pointCoordinateY, circleCenterCoordinateX, circleCenterCoordinateY, circleRadius, rectangleTopY, rectangleLeftX, rectangleWidth, rectangleHeidht);
  61.         Console.WriteLine((checkResultCircle == "inside" && checkResultRectangle == "outside") ? "TRUE" : "FALSE");
  62.  
  63.         Console.WriteLine("The point P({0},{1}) is {2} the circle K(({3},{4}),{5}) and {6} the rectangle R(top = {7}, left = {8}, width = {9}, height = {10})\n", pointCoordinateX, pointCoordinateY, checkResultCircle, circleCenterCoordinateX, circleCenterCoordinateY, circleRadius, checkResultRectangle, rectangleTopY, rectangleLeftX, rectangleWidth, rectangleHeidht);
  64.        
  65.         Main();
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment