In the hard disk gas program, the state of the gas is stored in an array. This array includes x and y positions as velocities for each molecule. For any time before the next collision, the position of a given molecule can be expressed as a function of time. Each molecule has a diameter of 1, so collisions occur when the distance between the midpoints of two molecules is 1. The time at which any two molecules collide is therefore given by the solution of the following set of equations:
where and are the coordinates of the molecules at the time of collision, t is the time until the next collision, and are the velocities of the molecules before the collision, and and are the current positions of the molecules. These equations reduce to a quadratic,
If the solution for t is negative or imaginary, the molecules will not collide at all.
All walls are stationary, and either horizontal or vertical. Therefore, if there is any collision between a molecule and a wall at all, it will occur when the edge of the molecule crosses the line of the wall. For example, if the wall is vertical,
where t is the time of collision, and are the x coordinates of the wall and molecule. If the value of t calculated is positive, the y position of the molecule at the time of collision can then be calculated to see if the molecule misses the wall.
Separate collision time calculations are necessary for cases when the molecule collides with the end of a wall. This calculation is very similar to the calculation of the time of collision between molecules, except that the end of the wall is stationary, and the collision occurs when the molecule is 0.5 units away from the end, not 1.0.