File Exchange

image thumbnail

Random close packing (RCP) on arbitrary distribution of circle sizes

version (1.01 MB) by Ti Xu
An algorithm for RCP based on electromagnetic theory


Updated 29 Jun 2017

From GitHub

View license on GitHub

# Input / Output
function [NWP,NWR] = pack_v(f) ... outputs positions of circles (NWP) and corresponding radii (NWR) given an input vector (f) of radii size.
# Result Example

Packing result for N=3332 circles, Gamma distributed in size with shape factor 3.81, is shown in result_v1.png

# Motivation & Theory

Updated March 09 2016.

This algorithm produces random close packing or RCP on an input of N radii following any arbitrary distribution of size. The script was developed as part of my PhD project, which involves modelling white matter microstructure. White matter bundles in the human brain consist of axons (or hollow cylinders) running uni-directionally. A 2-dimensional cross section of such a cylindrical bundle can be represented by a packing of circles. In human anatomy, axon size follow a Gamma distribution. Conventional software for biophysical modeling (for example, UCL's Camino Diffusion MRI toolkit) generates packing densities up to 65%. However, axon density in white matter regions such as the corpus callosum can reach densities up to 85%, based on literature values.

This algorithm is developed with motivation from electromagnetic theory. Given N charged particles, the task cast is to produce the configuration of maximum potential energy by minimizing the sum of distances of the circles - the assumption being that such a configuration is equivalent to or at least close to that of maximum packing density.
This is performed iteratively. A pool of N circles is generated. Next, a circle is selected at random without replacement. The placement or position of the selected circle is determined such that its distance to the average center of the circles already placed is minimized. This process is iterated.

For small N (~50) algorithm takes ~2 minutes with 8GB of RAM. For large N (~3000), algorithm takes ~10 hours. To confirm the method's robustness on first pass, if the input is circles of uniform size, it converges to hexgonal close packing (HCP).

Cite As

Ti Xu (2021). Random close packing (RCP) on arbitrary distribution of circle sizes (, GitHub. Retrieved .

Comments and Ratings (11)

James Murray

Using this amazing code to simulate powder layers but wondering if the circle edges can touch when plotted

Kaitlyn Casulli

I am wondering if this code can be adapted for three-dimensional shapes, e.g. if I wanted to pack spheres into a rectangular container.

Chris Yeung

I had to remove line 80 in pack_v.m for it to work. Is this line important? It seems to work as intended even without the line.

Liangwen Cao

Harish Charan

Thanks a lot for this awesome and very useful code. It is very helpful. However, I just need one help from you that, can you kindly tell me how can I get the two arrays of x-coordinates and y-coordinates. I actually want to use this randomly packed coordinates to use in one of my own simulations.

Mohammad Amin Kazemi

Very useful. Thank you.
Looking forward to seeing a 3D one!


ujwol palanchoke

can you please provide the bibliography for this project? It would be great!

yu gu

Tom Mingasson


Hope that the code can be used for arbitrary specified shape. And the speed is too slow. Anyway, it works!

MATLAB Release Compatibility
Created with R2015a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!