Back to General discussions forum
So,my code solves the problem from the example correctly, here is a screenshot of the trajectory drawn using sfml
But there are problems with real tasks
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);
}
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.
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