There are multiple algorithm whichalgorithms we can use to generate such point locations and radii. Some of which are iterative, other thatothers are generative but(but expensive), and others that are "approximative""approximative." We don't really care about execution time because the output is readily cached. Perfect packing is not needed as well becauseeither since smaller circles will nearly never be seen. From that, we are going to construct a simple algorithm to generate the spheres locations and radiiusually go unnoticed.
- Initialize an empty vector and float lists for locations and radii.
- Define a function that generates a random location, preferably uniform in density.
- For some arbitrary amountnumber:
- Generate a random point $p$.
- Loop over the point and radii lists and compute the smallest signed distance between the point $p$ and the nearest sphere surface point.
- If the signed distance is positive, that is, (meaning the point is outside all of the existing spheres), append thethat point $p$ and and the radii which(which is defined as any scalar that is smaller that or equal the positive distance.
- End loop).
The smallest signed distance between the point $p$ and the nearest surface point is equal to the distance between $p$ and the nearest sphere minus its radius. This can be illustarted by looking at this simple diagram:
If the distance is smaller than the radius, then the signed distance will be negative, telling us that the point $p$ is inside of the spheres, and that's why we don't append it. Moreover, the radius of the sphere at point $p$ has to be smaller thatthan the positive distance so as not to intersect the other circle.
The implementation is simple, toTo find the smallest signed distance, we loop over the point and radii lists and reassign an initially infinity float parameter if the current signed distance is smaller than the value of the parameter. This is is the equivalent ofto a KD Tree search except we are subtracting some value defined as the radius. The implementation is as follows:
The rest of the implementation is easy. WeNext, we create a loop that havehas an arbitrary number of iterations controlling the maximum possible amountnumber of spheres, however. However, it does not strictly define the amountnumber of spheres itself ! The loop has two main parameters,: (1) a vector list that will include the spheres locations and (2) a float list that will contain the radii locations, both. Both inputs are not definedundefined, meaning that they are initially empty lists that we will append to later. In this loop, we generate a random point (Wewe will talk about that algorithm later on), compute the smallest signed distance using the loop we created before and append the sphere location if and only if it is positive. The append condition is implemented as a form of reassign condition for both lists, it. It is important that we inforce copyingcopy the lists at each iteration thus making sure, ensuring the append doesn't overwrite the original list rendering our reassign condition useless. I chose to define the radius as the positive distance bounded by some arbitrary constant called the Max Radius, though any other positive value less than the distance will work. The implementation is as follows: