Back to Problem Solutions forum
Hello everyone.
I've been working on problem 133(Billiard Ball) and I stuck with bouncing from bottom and right borders, as I may suggest by wrong output.
Here is the code of bounces(bottom, right)
Bounce from bottom border: if(Y >= H-R) { Y += 2 * (H - R - Y); Vy *= -1; }
Bounce from right border: if(X >= W-R) { X += 2 * (W - R - X); Vx *= -1; }
I'm thinking I made it right, did I?
I don't write code for top and left borders, because it can be found in the problem.
Hi,
I'm not saying your code is correct, as I haven't tried it, but nothing sticks out as being wrong.
My code had ">" instead of ">=", but I can't see that making it fail.
If you "submit" your full code, on the problem page, we'll be able to see it.
Here it is:
#include <iostream>
using std::cout;
using std::endl;
#include <fstream>
using std::ifstream;
#include <cmath>
using std::sqrt;
using std::pow;
#include <utility>
using std::pair;
constexpr double dt = 0.0001;
typedef pair<double, double> coord_type;
typedef const size_t c_size_t;
coord_type find_final_position(c_size_t &W, c_size_t &H, double &X,
double &Y, c_size_t &R, double &Vx,
double &Vy, c_size_t &A)
{
double V = sqrt(pow(Vx, 2) + pow(Vy, 2));
while(V > 0.0)
{
X += Vx * dt;
if(X < R)
{
X += 2 * (R - X);
Vx *= -1;
}
if(X >= W-R)
{
X += 2 * (W - R - X);
Vx *= -1;
}
Y += Vy * dt;
if(Y < R)
{
Y += 2 * (R - Y);
Vy *= -1;
}
if(Y >= H-R)
{
Y += 2 * (H - R - Y);
Vy *= -1;
}
V -= A * dt;
}
return {X, Y};
}
int main()
{
size_t W{}, H{}, R{}, A{};
double X{}, Y{}, Vx{}, Vy{};
ifstream fin("data.txt");
fin >> W >> H >> X >> Y >> R >> Vx >> Vy >> A;
coord_type final_position =
find_final_position(W, H, X, Y, R, Vx, Vy, A);
cout << final_position.first << " " << final_position.second;
return 0;
}
Didn't want to post all code first, because of those who didn't solve this problem yet, to let them solve it by themselves.
Hi,
What I meant for you to do, was to copy your code into the "your solution" area of the problem page, then press the submit button. That way, only people who have solved the problem can see your code.
Anyway, Vx & Vy are the velocities in the x & y direction, while V is the full velocity. Your code decreases V, but not Vx or Vy.
What do the {} do in the first two lines of main?
Try to read input from stdin so that your code can be run unchanged on the problem page using the C++ button. There should be somewhere in your compiler where you can tell it to take stdin from data.txt. On my compiler it's Debugging...Command Arguments...<data.txt
Hi, Quandray.
Sorry for delay.
1) My code decreases only V (not Vx and Vy), because it says in problem statement in "Slowing down" section V -= A * dt
2) {} those brackets just initialize variables with default-initialization, so I can be sure that they are not uninitialize.
3) I was sure, that all variables were initialize correctly, but I did what you said and read input from stdin. Nothing changed.
BTW, I copy my code into the "your solution" area and run it, just in case.
Any more suggestions?
1) Although the problem statement doesn't say that Vx & Vy need to be decreased, they are the horizontal & vertical component of the overall velocity V. So, if the overall velocity V changes, so must Vx & Vy.
2) Regarding the initialization done with {}, thanks, I've not seen that before.
1) Damn, it's logical.
But how can I calculate a - angle between OX asix and vector of velocity?
And why I should calculate this angle to use it in Vx = V * cos(a) and Vy = V * sin(a) if I can simpy do this Vx = sqrt(pow(V, 2) - pow(Vy, 2)) and Vy = sqrt(pow(V, 2) - pow(Vx, 2)) isn't this one correct?
I just worked out the percentage that V was reduced, then reduced Vx & Vy by the same percentage.
Admin, please delete source code above if posible.