Strip extra spaces from code.
[voro++.git] / branches / 2d / examples / walls / polygon.cc
blob76811a38c5827c27357456f3db6584974cdca08a
1 #include <cmath>
2 using namespace std;
4 #include "voro++_2d.hh"
5 using namespace voro;
7 const double pi=3.1415926535897932384626433832795;
8 const double radius=0.5;
9 const int n=5;
11 // This function returns a random floating point number between 0 and 1
12 double rnd() {return double(rand())/RAND_MAX;}
14 int main() {
15 int i=0;double x,y,arg;
17 // Initialize the container class to be the unit square, with
18 // non-periodic boundary conditions. Divide it into a 10 by 10 grid, with
19 // an initial memory allocation of 8 particles per grid square.
20 container_2d con(-1,1,-1,1,10,10,false,false,8);
22 // Add circular wall object
23 wall_plane_2d *wc[n];
24 for(i=0,arg=0;i<n;i++,arg+=2*pi/n) {
25 wc[i]=new wall_plane_2d(sin(arg),-cos(arg),radius);
26 con.add_wall(wc[i]);
29 // Add 1000 random points to the container
30 while(i<1000) {
31 x=2*rnd()-1;
32 y=2*rnd()-1;
33 if(con.point_inside(x,y)) {
34 con.put(i,x,y);
35 i++;
39 // Output the particle positions to a file
40 con.draw_particles("polygon.par");
42 // Output the Voronoi cells to a file, in the gnuplot format
43 con.draw_cells_gnuplot("polygon.gnu");
45 // Sum the Voronoi cell areas and compare to the circle area
46 double carea=n*radius*radius*tan(pi/n),varea=con.sum_cell_areas();
47 printf("Total polygon area : %g\n"
48 "Total Voronoi cell area : %g\n"
49 "Difference : %g\n",carea,varea,varea-carea);
51 // Since they were dynamically allocated, delete the wall_plane_2d
52 // objects
53 for(i=0;i<n;i++) delete wc[i];