Write a program that determines whether the point with the given coordinates falls into the area shaded in gray in the figure. The result of the work output in a text message.

For this graph, I have no ideas, more precisely, I do not know what equations to describe the shaded area and how to compose them.

Technical implementation will not be a problem, only the analytical form or the pseudo-code is of interest, how to make the necessary equation and (or) to check the necessary area?

  • Guide a ray from a point outside the graph through a given point. Consider the intersection of the beam with the borders of the shaded area. If the number of intersections from the starting point to the given point is odd, then, most likely, the specified point is inside the area. If possible, the beam should preferably be parallel to the x-axis or ordinate axis. - alexlz
  • @xHunter, If you are given an exhaustive answer, mark it as correct (click on the check mark next to the selected answer). - sinedsem

4 answers 4

My guess is the following: if a point falls into a square, we will further check the circles using the formula

if sqr(x-x0)+sqr(y-y0)<=sqr(r) then count:=true else count:=false 

Check whether a point falls into one circle, and then check the other by the same way. Therefore, if a point falls into one of the circles, it does not fall into the shaded area.

    Break the task into simple subtasks.

    1. A point belongs to a shaded area if it belongs to a square and NOT belongs to circles.
    2. A point belongs to a square if its X-coordinate lies in ( what? ) Limits, and the Y-coordinate lies in ( what? ) Limits.
    3. A point belongs to a circle if its distance from the center of the circle does not exceed the radius
    4. The distance between two points is considered according to the Pythagorean theorem.

    ( Yes, you can optimize by going to the squares of distances, this is not important at this stage. )

       class Point { private int x; private int y; // сгенерируйте сами геттеры, сеттеры и конструктор } Boolean withinCircle(Point arg, Point circleCenter,int radius) { return sqr(arg.getX()-circleCenter.getX())+sqr(arg.getY()-circleCenter.getY())<=sqr(radius) } Boolean withinSquare(Point arg, Point leftUp, Point rightDown) { return (arg.getX()>=leftUp.getX()&&arg.getY()>=leftUp.getY())&& (arg.getX()<=rightDown.getX()&&arg.getY()<=rightDown.getY()) } Point yourPoint=...blah-blah if( !withinCircle(yourPoint,...) && !withinCircle(yourPoint,...) && withinSquare(yourPoint,...) ) 

      Either it is possible to move the functions of checking the belonging of areas to the classes of points, or to make some kind of interface.

         function inD(x,y:double):boolean; begin if (-R<=x) and (x<=R) and (-R<=y) and (y<=R) // в квадрате and ((-Rx)*(-Rx)+(Ry)*(Ry)>R*R) // вне круга and ((Rx)*(Rx)+(-Ry)*(-Ry)>R*R) then // вне круга result:=true else result:=false; end;