-1

I’m new in this. I try to learn the basics of C# and that is my first language. I have found a task in internet that drives me crazy. It is mostly the mathematical part that is posing me troubles I think. I hope anyone can help me. Here is the task:

Write a program that reads a pair of coordinates x and y and uses an expression to 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).

Input

You will receive the pair of coordinates on the two lines of the input - on the first line you will find x, and on the second - y.

Output

Print inside circle if the point is inside the circle and outside circle if it's outside. Then print a single whitespace followed by inside rectangle if the point is inside the rectangle and outside rectangle otherwise. See the sample tests for a visual description. Constraints

The coordinates x and y will always be valid floating-point numbers in the range [-1000, 1000].

I have done this up to now:

static void Main()
{
    double x = double.Parse(Console.ReadLine());
    double y = double.Parse(Console.ReadLine());
    double centerx = 1;
    double centery = 1;
    double r = 1.5;

    double widthR = 6;
    double HighR = 2;
    double topY = 0 + (HighR / 2);
    double rightX = 0 + (widthR / 2);
    double bottomY = 0 - (HighR / 2);
    double leftX = 0 - (widthR / 2);
    double rectanglePointX = x - (-1);
    double rectanglePointY = y - 1;

    bool IsInsideC = (x - centerx) * (x - centerx) + (y - centery) * (y - centery) <= r * r;
    bool IsInsideR = ((rectanglePointY< topY) && (rectanglePointY > bottomY) && (rectanglePointX < rightX) && (rectanglePointX > leftX));
    if (IsInsideC ==false && IsInsideR== false)
    {
        Console.WriteLine("outside circle outside rectangle");
    }
    else if (IsInsideC == true && IsInsideR == true)
    {
        Console.WriteLine("inside circle inside rectangle");
    }
    else if (IsInsideC == false && IsInsideR == true)
    {
        Console.WriteLine("otside circle inside rectangle");
    }
    else
    {
        Console.WriteLine("inside circle outside rectangle");
    }
}

Thank's in advance!

juanferrer
  • 1,192
  • 1
  • 16
  • 29
Julian Docev
  • 119
  • 9

3 Answers3

2

Please, extract methods, keep your solution simple, do not repeat yourself:

private static bool IsWithinRectangle(double x, double y, 
    double left, double top, double width, double height) {

    // y should be below the top - y <= top and above the bottom: y >= top - height 
    return (x >= left) && (x <= left + width) && 
           (y <= top) && (y >= top - height); 
}

private static bool IsWithinCircle(double x, double y,
                                    double centerX, double centerY, double r) {
  return (x - centerX) * (x - centerX) + (y - centerY) * (y - centerY) <= r * r;
}

Then use these methods:

double pointX = ...
double pointY = ...

if (!IsWithinRectangle(pointX, pointY, ...) && 
     IsWithinCircle(pointX, pointY, ...)) {
     ...
}

Test:

  // Circle:
  double centerx = 1;
  double centery = 1;
  double r = 1.5;

  // Rectangle:
  double left = -1;
  double top = 1;
  double width = 6;
  double height = 2;

  // Point to test:
  double x = 2.5;
  double y = 2.0;

  string rectTest = IsWithinRectangle(x, y, left, top, width, height) 
    ? "within rect" 
    : "outside rect";

  string circleTest = IsWithinCircle(x, y, centerx, centery, r) 
    ? "within circle" 
    : "outside circle";

  Console.Write(string.Join(" & ", circleTest, rectTest));

Outcome:

  outside circle & outside rect
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • It defines well whether the coordinates are in the cercle but it makes some errors within the rectangle. I had the same problem. – Julian Docev Jun 30 '17 at 15:03
  • @Julian Docev: please, provide a *counter example* ("... it makes *some errors* within the rectangle..."); I suggest that a point (`x, y`) is within the rectangle if and only if `x` is within `[left..left+with]` range and `y` is within `[top..top+height]` range – Dmitry Bychenko Jun 30 '17 at 15:19
  • In the task this is left to check the result. The cercle is ok but the rectangle not always. ;x=2.5 y =2 - outside circle outside rectangle; x=0 y = 1 - inside circle inside rectangle; x= 2.5 y = 1 - inside circle inside rectangle; x= 1 y = 2 - inside circle outside rectangle. – Julian Docev Jun 30 '17 at 15:25
  • @Julian Docev: I see, we have to *subract* from the `top`: `(y <= top) && (y >= top - height)`: y should be *below* the `top` – Dmitry Bychenko Jun 30 '17 at 15:49
  • can you explain a little bit better the entire equation because i`m not sure that i understand ;) – Julian Docev Jun 30 '17 at 18:01
  • ahh ok I understood. I had to change the x line too. So i made the equation like this: bool IsInsideR = ((y <= topY) && (y >= topY -HighR) && (x <= rightX) && (x >= leftX - widthR)); – Julian Docev Jun 30 '17 at 18:12
1

First you have to understand this is not a programming specific question. To create the program, follow these steps:

  • Manually write every step to solve this question in maths, on paper.
  • Check for repetitive steps and put them together.
  • Write a simple pseudo-code(simple representation of program in English).
  • Convert the pseudo-code in any programming language.

By this method, you can convert almost every maths problem in program in any language.

Requirements:

  1. Understanding of concept of problem(here how to solve the same in maths on paper)
  2. Basic programming knowledge

Hope this will help. :-)

juanferrer
  • 1,192
  • 1
  • 16
  • 29
Aman
  • 735
  • 1
  • 6
  • 19
0

I think this is what you need - Equation for testing if a point is inside a circle

If this doesn't help here is another one - Check if point is inside a circle

And here is how to check about rectangle - Finding whether a point lies inside a rectangle or not

Pavlin Petkov
  • 1,022
  • 3
  • 17
  • 38