title: Algorithm for pool ball movement calcualtion
body: <p>I need help or idea how to make an algorithm for a pool game/program. </p>
  
  <p>I need this as a project for my collage so this needs to be written as simply as possible using only basic and loops,sadly I can't use arrays or functions.</p>
  
  <p>The whole point of the program is to calculate how many steps/hits on the corner of the table will the ball need to reach its starting point (Assumed that ball has infinite velocity and that there are no other balls etc.. Simply we assume that its possible)</p>
  
  <p>We need to look at the table as a coordinate system with (m x n) dimensions, and we need to input starting point of the ball (x1,y1) and the point where the ball will hit the table for the first time (x2,y2) based on that info we need to make an algorithm that will calculate how many hits/steps will the ball need to reach its starting point.</p>
  
  <p>I had an idea to use the fact that ball will have same angle on impact which means that if the ball hits the table on 30 degrees angle it will repel off the table with same angle to the other side. So because the tables is set as a coordinate system I could use atan to calculate the angle and then with that info to calculate the distance from the other point. I made a program with the info I know but this won't work because this is only one calculation (I guess that I will need to use for loop(s) to calculate everything. And I have a big problem in understanding how to set the edge of the table because some of my calculations goes outside the (m x n) space.</p>
  
  <p>I will attach 2 pictures that were given to us as examples and part of the code that I use. I hope that someone can help me and I'm sorry if I made a lot of grammar mistakes, English is not my primary language. </p>
  
  <p>Thanks in advance.</p>
  
  <p>PICTURES :</p>
  
  <p>Orange dot is the starting point, orange line is the first hit that we input,everything else needs to be calculated:</p>
  
  <p><img src="https://i.stack.imgur.com/7UPqw.jpg" alt="Orange dot is the starting point, orange line is the first hit that we input,everything else needs to be calculated"> </p>
  
  <p>Same as picture one but more complex example:</p>
  
  <p><img src="https://i.stack.imgur.com/cRLx0.jpg" alt="Same as picture one but more complex example"></p>
  
  <p>MY CODE: </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;math.h&gt;
  
  #define PI 3.14159265359
  
  int main() {
  
      int i;
      double x1,y1,x2,y2;
      int n,m;
      double angle;
      double xPrime,yPrime;
      double distance;
      double m1;
  
  
      printf("Enter the staring point (x1,y1) and first hit point(x2,y2) ");
      scanf("%lf %lf %lf %lf",&amp;x1,&amp;y1,&amp;x2,&amp;y2);
      printf("Enter the dimensions (n x m ) ");
      scanf("%d %d",&amp;n,&amp;m);
  
  
          m1=(y2-y1)/(x2-x1);
          angle=atan(m1);
  
          distance =sqrt( pow((x2-x1),2) + pow((y2-y1),2));
  
          xPrim = x2 + (-distanca * cos(ugao));
          yPrim = y2 + (distanca * sin(ugao));
  
  
  
  
      return 0;
  }
  </code></pre>