Dear forum users!
I found the following problem on the Internet: to determine whether the entered coordinate (X,Y) falls into the shaded area.
My guesses
The entered X coordinate must be in the range [-5,5] and the Y coordinate in the range [-1,1] . But, there is a circle and, as I think, it is necessary to define a certain interrelation between X and Y
Please help me in this task. Thank you
- 2C ++ has nothing to do with it, it is a school-level math based on school formulas of a line and a circle - andreymal
2 answers
The entered X coordinate must be in the range [-5.5] and the Y coordinate in the range [-1,1].
This observation can be used as a precondition for this task. That is, if the point is inside a rectangle, then it may fall into the shaded area. If there are a lot of points, then you can increase the performance by tilting exactly those that do not fall. 
Point p; // Каким-то образом получили точку и сохранили в p if (abs(px) <= 5.0 && abs(py) <= 1.0) { // Проверяем попадает ли точка в заштрихованную область } else { // Точка точно не попадает в заштрихованную область } Next, you need to determine in which quarter point p lies. If in the second and fourth, then you need to check whether the point falls into a circle with radius 1. To do this, substitute the coordinates of the point in the equation of the circle x^2 + y^2 < 1^2 (or <= if you are interested in the border).
if (px * px + py * py <= 1.0) { // Точка попадает в окружность } For the first quarter, the equation of the line y = 1 - 0.2x . Therefore, a point falls into a triangle if the conditions px >= 0.0 && py >= 0.0 && py + 0.2 * px <= 1.0 . But px >= 0.0 && py >= 0.0 can be removed, because this is essentially a check on the belonging of a point to the first quarter.
For the third quarter, everything is the same, but you can see that the triangles are similar. Therefore, you can simplify the check to this type:
// Работает для 1 и 3 четверти if (abs(py) + 0.2 * abs(px) <= 1.0) { // Точка попадает в треугольник } Putting it all together we get:
Point p; // Каким-то образом получили точку и сохранили в p if (abs(px) <= 5.0 && abs(py) <= 1.0) { // Проверяем попадает ли точка в заштрихованную область if ((/*Точка в 2 или 4 четверти?*/) && px * px + py * py <= 1.0) { // Точка попадает в окружность } else if ((/*Точка в 1 или 3 четверти?*/) && abs(py) + 0.2 * abs(px) <= 1.0) { // Точка попадает в треугольник } else { // Точка не попадает в заштрихованную область. } } else { // Точка точно не попадает в заштрихованную область } PS For such tasks, it is better to add tags Mathematics , Geometry, and so.
In the beginning, you need to know what our quarter. For example: when x> 0 && y> 0, the first one, etc. Then, in 2 and 4 quarters, we have a circle with a radius of 1, and a center at the origin. In order for a point to enter it, we check the condition (x ^ 2 + y ^ 2 <= 1). If yes - then the point is in this circle. For 1 quarter we have a graph of y = -x / 5 + 1; Ie (y + x / 5 <= 1) - is included. And for 3 - (y + x / 5> = -1) - is included. Like so.
