M
M
MrFlatman2017-10-04 19:23:01
C++ / C#
MrFlatman, 2017-10-04 19:23:01

Calculate whether a point falls within the shaded area?

Hello everyone, there is a code that does not work correctly in the condition, could you point out errors

Picture
59d50ab619e5f487281898.jpeg
#include "stdafx.h"
#include "iostream"
#include <conio.h>
using namespace std;

int main()
{
    int x, y, R=7, K=30;

    cout << "x=";
    cin >> x;
    cout << "y=";
    cin >> y;
    if ((x >= -K && y >= -K && (x + K)*(x + K) + (y + K)*(y + K) <= 4 * K*K)
        && !(y > -x && y*y + x*x < R*R));
    else
        cout << "Toshka ne popadaet";
    return 0;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kirill H, 2017-10-07
@KirillHelm

Separate the scope of the logical if block explicitly with curly braces.
Divide the conditions into component parts, but do it better with separate variables, i.e.

bool isXPosRightThanLine1 = (x >= -K);
bool isYPosUpperThanLine1 = (y >= -K);
...

Also, put them in a separate function like:
And already in the if block you will get:
if (isPointInArea(x,y))
{
   //do something
}
else
{
   //do something else
}

This will make your code more readable and easier to maintain.
Just as already said in the comments, use a better math.hlibrary for mathematical calculations,

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question