I need help with problem 133

Back to General discussions forum

Artyom Tsatinyan     2022-10-05 20:43:28
User avatar

So,my code solves the problem from the example correctly, here is a screenshot of the trajectory drawn using sfml

Screen 1

But there are problems with real tasks

Screen 2

Screen 3

And i don't really get why it doesn't work correctly

Help me please

This is my code:

#include <iostream>
#include <cmath>

using namespace std;

int sign(double x) {
    if (x >= 0) {
        return 1;
    }
    else return -1;
}

int main() {
    double W, H, X, Y, R, Vx, Vy, A, dt = 0.00001, V;

    cin >> W >> H >> X >> Y >> R >> Vx >> Vy >> A;

    V = sqrt(Vx * Vx + Vy * Vy);

    while (V > 0) {
        if (X + Vx * dt < R || X + Vx * dt > W - R) Vx = -Vx;
        if (Y + Vy * dt < R || Y + Vy * dt > H - R) Vy = -Vy;
        X += Vx * dt;
        Y += Vy * dt;
        V -= A * dt;
        double cs = cos(atan(Vx / Vy));
        double sn = sin(atan(Vx / Vy));
        Vx = V * abs(cs) * sign(Vx);
        Vy = V * abs(sn) * sign(Vy);
    }

    cout << round(X) << " " << round(Y);
}
zelevin     2022-10-06 04:51:04

Not sure what's going on with your code (I suspect atan; I generally try to use atan2), but the trajectories do not look correct: the balls in all three cases bounce at what looks like the angle of 45 degrees. This is correct for the problem example (where the speed components are 50, 50), but wrong for the tasks with the speeds of (-41, 75) and (12, -108). In these cases, the speed components differ in their absolute magnitude, so the angle should be different from 45 degrees.

Artyom Tsatinyan     2022-10-06 14:58:10
User avatar

Thank you very much zelevin, I was able to find the mistake thanks to your remark

Here are an examples of a trajectory drawn by a properly working algorithm

example1

example2

Please login and solve 5 problems to be able to post at forum