There is a two-dimensional array array[n][m] . From any angle let the ball, which will move at an angle of 45 degrees. After how many moves will the ball be in another corner?

How to implement repulsion from the walls of the array?

Closed due to the fact that the essence of the question is not clear to the participants of Kromster , tutankhamun , post_zeew , aleksandr barakin , HamSter 24 Oct '16 at 8:13 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • What is an "angle" and what is another angle? - Vlad from Moscow
  • 2
    ... For a minute, Alice looked thoughtfully at the mushroom, trying to determine where he had one side, and where the other ... - tutankhamun

3 answers 3

Well, we check for boundary indices, that is, something like this:

 if(row == 0 || column != 0) // верхняя стенка { ++row; column = (двигался вправо)? column + 1 : column - 1; //надо знать, понятно, куда летел } 

Well, and so on

    If the boundaries from which the rebound should be strictly vertical and horizontal, then the algorithm and mechanics are quite simple, generally speaking for arbitrary angles.

    Immediately we introduce two variables defining the motion vector; for movement at 45 degrees, the values ​​of displacements in integer coordinates will be as follows:

     int dx = 1;// 1 -1 -1 int dy = 1;//-1 1 -1 

    For an arbitrary angle, dx = cos (a), dy = sin (a).

    Now the cycle of movement will look extremely simple:

     while(!stop)//подставьте условие завершения движения { ball.X += dx; ball.Y += dy; } 

    No checks and other delights, and also works for arbitrary coordinates and offsets.

    Now the reflection. The main thing is to observe the laws of physics, namely: the angle of incidence equals the angle of reflection for elastic collisions. Nothing is easier. Add a reflection condition to the loop.

     while(!stop)//подставьте условие завершения движения { if(ball.X+dx > m-1 || ball.X+dx < 0) dx = -dx; if(ball.Y+dy > n-1 || ball.Y+dy < 0) dx = -dx; ball.X += dx; ball.Y += dy; } 

    Now the ball will be reflected from the encountered walls and angles until the condition for exiting the cycle is fulfilled. When colliding with a wall, the corresponding component of the vector changes sign, which ensures that the angle is preserved for any real values ​​of the components of the motion vector. That's all. Simple, transparent, does not require complex calculations.


    For a case with arbitrary obstacles and an arbitrary wall angle, everything is a bit more complicated, but if you have an idea about the transition from one coordinate system to another, then it is also not very difficult and will not blow the algorithm too much, although you will need to think carefully about an effective implementation.

      Let the current speed of the ball x, y be integer variables, which are measured in a cell / move. When our ball is near the border, for example (pseudocode): if (ball.x >= width - 1) ball.velX = -ball.velX - when the ball is in the last x-position - we invert its speed by х . Similarly for y and the position of the ball in 0 coordinates.