Advertisement
Ludmil

C# Operators Expression 10

Mar 15th, 2014
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.47 KB | None | 0 0
  1. using System;
  2. /*Problem 10.   Point Inside a Circle & Outside of a Rectangle
  3. Write an expression that checks for given point (x, y) if it is
  4.  * within the circle K({1, 1}, 1.5) and out of the rectangle R(top=1, left=-1, width=6, height=2).
  5. */
  6. class Program
  7. {
  8.     static void Main()
  9.     {
  10.         bool inCircle = true;
  11.         bool inRectangle = true;
  12.         decimal circleXX = 1;//define cirgle center
  13.         decimal circleYY = 1;//define circle center
  14.         decimal radius = 1.5m;
  15.  
  16.         decimal rectLeftX = -1;// ima se predwid most left stranata po x
  17.         decimal rectTopY = 1; //ima se predvid nai wisokata 4ast t.e. cqlata strana
  18.         decimal rertWidhtX = 6;// po X dostigashta koordinati (rertWidhtX -rectLeftX)
  19.         decimal rectHeightY = 2;// po Y (rectTopY-rectHeightY)
  20.  
  21.         decimal[] dotX = new decimal[] { 1, 2.5m, 0, 2.5m, 2, 4, 2.5m, 1, -100 };
  22.         //{ 0, -2, -1, 1.5, -1.5, 100, 0, 0.2, 0.9, 1 };
  23.         decimal[] dotY = new decimal[] { 2, 2, 1, 1, 0, 0, 1.5m, 2.5m, -100 };
  24.         //{ 1, 0, 2, -1, -1.5, -30, 0, -0.8, -1.93, 1.655 };
  25.         for (int i = 0; i < dotX.Length; i++)
  26.         {
  27.             inCircle = IsDotInCircle(circleXX, circleYY, radius, dotX, dotY, i);
  28.             inRectangle = IsDotOutRectangle(rectLeftX, rectTopY, rertWidhtX, rectHeightY, dotX, dotY, i);
  29.             if (inCircle == true && inRectangle == false)
  30.             {
  31.                 Console.WriteLine("yes");
  32.             }
  33.             else
  34.             {
  35.                 Console.WriteLine("no");
  36.             }
  37.         }
  38.     }
  39.     //###
  40.     static bool IsDotInCircle(decimal circleXX, decimal circleYY, decimal radius,
  41.             decimal[] dotX, decimal[] dotY, int i)
  42.     {
  43.         double circleCheck = Math.Pow((double)(dotX[i] - circleXX), 2) + Math.Pow((double)(dotY[i] - circleYY), 2);//(x-a)^2 + (y-b)^2
  44.         double radiusCheck = Math.Pow((double)(radius), 2);
  45.         if (circleCheck <= radiusCheck)
  46.         {
  47.             return true;
  48.         }
  49.         else
  50.         {
  51.             return false;
  52.         }
  53.     }
  54.  
  55.  
  56.     static bool IsDotOutRectangle(decimal rectLeftX, decimal rectTopY, decimal rertWidhtX,
  57.        decimal rectHeightY, decimal[] dotX, decimal[] dotY, int i)
  58.     {
  59.         if ((dotX[i] >= rectLeftX && dotX[i] <= rertWidhtX + rectLeftX) && (dotY[i] <= rectTopY && rectTopY - rectHeightY <= dotY[i]))
  60.         {
  61.             return true;//IN
  62.         }
  63.         else
  64.         {
  65.             return false;//OUT
  66.         }
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement