III.2.3 Implementation of basic image-source algorithm
The basic algorithm to compute the image source locations can be efficiently represented in recursive form as written below in pseudo-code.

Algorithm: Recursive computation of image sources up to given order

The algorithm goes through all the surfaces except the one that was used to create the current image-source, and creates new image-sources against those. The function is recursively called immediately for each new image-source until the reflection order set in the initial call is reached. It is possible to write the same algorithm in an iterative form as well, and then it would look as in the following.

Algorithm: An iterative formulation of image-source computation

In both of these cases the computation of reflection is the only required geometric operation. In pure geometric terms it corresponds to reflection of a point against a surface.

In algorithmic terms the difference between these two implementations is in how they construct the image-source tree. The recursive one performs it in a depth-first manner in which each branch of the tree is built to the bottom before construction of the next branch is started. On the contrary, the iterative approach performs the construction in a breath-first style such that each level of the image-source tree is finalized before progressing to the next level. This means, for example, that all the third order image sources are constructed before starting with the fourth order reflections. From viewpoint of computational efficiency the recursive depth-first manner is more efficient as it has smaller memory consumption. At any given time, there is need to store only one branch of the tree whereas in the breath-first construction all the image-sources of a reflection order need to be stored before construction of the next order will start. In some applications, there is a need to store all the image sources and in such cases this difference will vanish.

The two algorithms for the image-source method shown above are straightforward to implement and both of them provide correct results that are equal to each other. However, there are two basic improvements that can render those computationally more efficient. First, the reflectors should always be in front of the previous reflector, and, secondly, they should be facing that reflector. Please, remember the assumption that only the front-faces of surfaces are reflective. Taking these into account makes the algorithm slightly more complicated as presented here in recursive form again.

Algorithm: Full image-source computation algorithm 

The main change is on the line 4 that contains two additional operations. The first one ensures that the new surface is at least partially in front of the previous reflector. The most straightforward way to test that is to check that at least one of the vertices of the surface is in front of the previous reflector, and that function is called as inFrontOf in the algorithm above. The other required operation is the dot product (external link) of the normal vectors. If that returns a negative value, it means that the normal vectors of the surfaces, and thus the actual surfaces, are facing each other as they should to be able to produce valid reflection paths.