Advertisement
Klaxon

[C# Operators] Circle and Rectangle Point

Jul 9th, 2013
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.69 KB | None | 0 0
  1. // Write an expression that checks for given point (x, y) if it is within the circle K( (1,1), 3)
  2. // and out of the rectangle R(top=1, left=-1, width=6, height=2).
  3.  
  4. using System;
  5.  
  6. class CircleAndRectanglePoint
  7. {
  8.     static void Main()
  9.     {
  10.         // Get the point X
  11.         Console.Write("Enter the point X: ");
  12.         float pointX = float.Parse(Console.ReadLine());
  13.  
  14.         // Get the point Y
  15.         Console.Write("Enter the point Y: ");
  16.         float pointY = float.Parse(Console.ReadLine());
  17.  
  18.         Console.Write("The point ({0}, {1}) is ", pointX, pointY);
  19.  
  20.         // Checks if given point (X, Y) is within the circle
  21.         bool isInCirle = (pointX - 0) * (pointX - 0) + (pointY - 0) * (pointY - 0) <= 5 * 5;
  22.  
  23.         // If the point is inside the circle..
  24.         if (isInCirle)
  25.         {
  26.             // ..print on the console
  27.             Console.Write("inside the circle and ");
  28.         }
  29.  
  30.         // If the point is NOT inside the circle..
  31.         else
  32.         {
  33.             // ..print on the console
  34.             Console.Write("outside the circle and ");
  35.         }
  36.  
  37.         // Checks if given point (X, Y) is within the rectangle
  38.         bool isInRectangleX = pointX <= 5 && pointX >= -1;
  39.         bool isInRectangleY = pointY <= 1 && pointY >= -1;
  40.  
  41.         // If the point is inside the rectangle..
  42.         if (isInRectangleX && isInRectangleY)
  43.         {
  44.             // ..print on the console
  45.             Console.WriteLine("inside of the rectangle!");
  46.         }
  47.  
  48.         // If the point is NOT inside the rectangle..
  49.         else
  50.         {
  51.             // ..print on the console
  52.             Console.WriteLine("outside of the rectangle!");
  53.         }
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement