## single_cell.cc – Constructing a single random Voronoi cell

This example demonstrates the voronoicell class, creating a single Voronoi cell by making cuts by randomly oriented planes. All the planes are a distance of 0.5 away from the center of the cell.

On line 7, the source code for the library is included, and on line 11, a
small function is defined that makes use of the `rand()`

standard
routine to return a floating point number between zero and one. When the main
loop starts, several variables are declared and a `voronoicell`

class is created, although it remains empty. On line 19, it is initialized to
be a cube of side length 2 centered at the origin.

In lines 23 to 32, the cell is cut by choosing 250 random points with
*x*, *y*, and *z* coordinates in the range from -1 to 1. For
each point the squared radius of the point is computed. If this is between 0.01
and 1, then the point is rescaled so that it has radius 1. A plane cut is then
applied at each of these points.

On line 35, the computed cell is output to the file “single_cell.gnu” in format that can be read by the freeware plotting program Gnuplot. The file is split into groups of lines of the form:

`<x_0> <y_0> <z_0>`

<x_1> <y_1> <z_1>

<x_2> <y_2> <z_2>

(blank line)

(blank line)

<x_1> <y_1> <z_1>

<x_2> <y_2> <z_2>

(blank line)

(blank line)

Each group of lines corresponds to a subset of contiguous edges of the
Voronoi cell. For the above example, two lines are drawn from
(*x_0*,*y_0*,*z_0*) to (*x_1*,*y_1*,*z_1*), and
(*x_1*,*y_1*,*z_1*) to (*x_2*,*y_2*,*z_2*). The
output file can be visualized in Gnuplot using the following command:

`splot "single_cell.gnu" with lines`

The Gnuplot output is shown above. It should be noted that all of the vertices in the cell shown above have order 3, so that they are connected to exactly three other vertices. For cases when a cell is created by planes in random orientations, this will almost always be the case. Higher order vertices require that a plane cut intersect an existing vertex. The code supports this behavior, and it is discussed in the degenerate example.

Increasing the number of vertices results in a more complex shape, that tends towards being a circle. The volume of the cell tends the volume of a sphere with unit diameter.

## Code listing

1: // Single Voronoi cell example code 2: // 3: // Author : Chris H. Rycroft (LBL / UC Berkeley) 4: // Email : chr@alum.mit.edu 5: // Date : August 30th 2011 6: 7: #include "voro++.hh" 8: using namespace voro; 9: 10: // This function returns a random floating point number between 0 and 1 11: double rnd() {return double(rand())/RAND_MAX;} 12: 13: int main() { 14: double x,y,z,rsq,r; 15: voronoicell v; 16: 17: // Initialize the Voronoi cell to be a cube of side length 2, centered 18: // on the origin 19: v.init(-1,1,-1,1,-1,1); 20: 21: // Cut the cell by 250 random planes which are all a distance 1 away 22: // from the origin, to make an approximation to a sphere 23: for(int i=0;i<250;i++) { 24: x=2*rnd()-1; 25: y=2*rnd()-1; 26: z=2*rnd()-1; 27: rsq=x*x+y*y+z*z; 28: if(rsq>0.01&&rsq<1) { 29: r=1/sqrt(rsq);x*=r;y*=r;z*=r; 30: v.plane(x,y,z,1); 31: } 32: } 33: 34: // Output the Voronoi cell to a file, in the gnuplot format 35: v.draw_gnuplot(0,0,0,"single_cell.gnu"); 36: }