Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2014
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. using System;
  2.  
  3. class PointInFigures
  4. {
  5. private static bool IsInsideCircle(double x, double y)
  6. {
  7. bool isInsideCircle = false;
  8. double radius = Math.Pow(1.5, 2); // using a^2 + b^2 = c^2
  9. double num = (Math.Pow(x - 1, 2) + Math.Pow(y - 1, 2)); // using a^2 + b^2 = c^2
  10. //x and y - 1 is because the centre of the circle is 1, 1.
  11. //other way is to add to the radius
  12. if (num <= radius)
  13. {
  14. isInsideCircle = true;
  15. }
  16. return isInsideCircle;
  17. }
  18.  
  19. private static bool IsInsideRectangle(double x, double y)
  20. {
  21. bool isInsideRectangle = false;
  22. if (x >= -1 && x <= 5 && y <= 1 && y >= -1) // rectangle has coordinats:
  23. // x is in range -1 to 5 and y is 1 to -1
  24. // than the point is outside it.
  25. {
  26. isInsideRectangle = true;
  27. }
  28. return isInsideRectangle;
  29. }
  30.  
  31. private static bool Condition(double x, double y)
  32. {
  33. bool isInsideCircle = IsInsideCircle(x, y);
  34. bool isInsideRectangle = IsInsideRectangle(x, y);
  35. if (isInsideCircle == true && isInsideRectangle == true)
  36. {// if it is inside the rectangle and the circle it is not allowed,
  37. //however in that case true && true == true, so we need to return false.
  38. return false;
  39. }
  40. else
  41. {
  42. return isInsideCircle && !isInsideRectangle; // ! is used, if the first is
  43. //true than no matter what is the second it will be true, however if the second
  44. //is true, than acording to the condition of the problem the return value has to
  45. //be false. So the ! makes the second value false in all the times when the first
  46. //is false.
  47. }
  48.  
  49. }
  50.  
  51. private static void PrintCondition(double x, double y)
  52. {
  53. Console.WriteLine("Point x = " + x + "y = " + y + "is inside K & outside of R: " +
  54. (Condition(x, y)));
  55. }
  56.  
  57. static void Main(string[] args)
  58. {
  59. Console.WriteLine("x = ");
  60. double num1 = double.Parse(Console.ReadLine());
  61. Console.WriteLine("y = ");
  62. double num2 = double.Parse(Console.ReadLine());
  63. //the point has to be within the circle and out of the rectangle
  64. PrintCondition(num1, num2);
  65. Console.WriteLine("Condition numbers:");
  66. //test with condition numbers.
  67. PrintCondition(1, 2);
  68. PrintCondition(2.5, 2);
  69. PrintCondition(0, 1);
  70. PrintCondition(2.5, 1);
  71. PrintCondition(2, 0);
  72. PrintCondition(4, 0);
  73. PrintCondition(2.5, 1.5);
  74. PrintCondition(2, 1.5);
  75. PrintCondition(1, 2.5);
  76. PrintCondition(-100, -100);
  77.  
  78. }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement